⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csci4134subscriber.java

📁 jms主题方式接收端代码
💻 JAVA
字号:
package csci4134.web.jms.topic;

import java.io.InputStreamReader;

import java.util.Hashtable;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.jms.TextMessage;

import javax.naming.InitialContext;


/**
 * This class demonstrates a simple message subscriber in a
 * topic/subsriber JMS application.
 * @author Jeff Tassin
 */
public class CSCI4134Subscriber
{
   public static void main(String[] args)
   {
      try
      {
	 System.out.println( "CSCI4134Subscriber started" );
	 Properties props = new Properties();
	 props.put( "java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory" );
	 props.put( "java.naming.provider.url", "localhost:1099" );
	 props.put( "java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces" );
         InitialContext ic = new InitialContext( props );
	 
	 TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory)ic.lookup("ConnectionFactory");


	 /** note that this topic must be defined in the $JBOSS_HOME/server/default/deploy/\jbossmq-destinations-service.xml file */
	 String topicName = "topic/csci4134/demoTopic";
	 Topic topic = (Topic)ic.lookup(topicName);
	 
	 TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
	 TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
	 TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
	 TextListener topicListener = new TextListener();
	 topicSubscriber.setMessageListener(topicListener);
	 topicConnection.start();
	 
	 System.out.println("To end program, enter Q or q, " + "then return" );
	 InputStreamReader reader = new InputStreamReader(System.in);
	 char answer = 0;
	 while (!((answer == 'q') || (answer == 'Q'))) 
	 {
	    answer = (char)reader.read();
	 }	 
	 topicConnection.close();
      }
      catch( Exception e )
      {
	 e.printStackTrace();
      }
   }

   /**
    * Implementation of MessageListener so we can demonstrate getting
    * messages asynchronously
    */
   public static class TextListener implements MessageListener
   {
      public void onMessage(Message message)
      {
	 TextMessage msg = null;
	 try
	 {
	    if (message instanceof TextMessage) 
	    {
	       msg = (TextMessage) message;
	       System.out.println("Reading message: " + msg.getText());
	    } 
	    else
	    {
	       System.out.println("Message of wrong type: " + message.getClass().getName());
	    }
	 }
	 catch (JMSException e)
	 {
	    System.out.println("JMSException in onMessage(): " + e.toString());
	 } 
	 catch (Throwable t) 
	 {
	    System.out.println("Exception in onMessage():" + t.getMessage());
	 }
      }
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -