ann
          冰是沒有未來的,因?yàn)樗挠篮?/span>
          posts - 107,comments - 34,trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿

          隨筆分類(105)

          隨筆檔案(97)

          文章分類(16)

          文章檔案(20)

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          1.jms接收的客戶端

             首先,創(chuàng)建jms Listener 
            
          /*
           * To change this template, choose Tools | Templates
           * and open the template in the editor.
           
          */

          package com.test.jms;



          import com.sun.messaging.ConnectionConfiguration;
          import com.sun.messaging.ConnectionFactory;
          import com.sun.messaging.Queue;
          import com.sun.messaging.Topic;
          import java.util.logging.Level;
          import java.util.logging.Logger;
          import javax.jms.Destination;
          import javax.jms.JMSException;
          import javax.jms.Message;
          import javax.jms.MessageConsumer;
          import javax.jms.MessageListener;
          import javax.jms.QueueConnection;
          import javax.jms.Session;
          import javax.jms.TextMessage;
          import javax.jms.TopicConnection;


          /**
           *
           * 
          @author ann
           
          */
          public class JMSListener  implements MessageListener {

              String topicName 
          = "mytopic" ;     //要監(jiān)聽的topic名字
              String queueName = "myqueue" ;     //要監(jiān)聽的queue的名字
              String brokerHost = "localhost" ;  //OpenMQ server (broker)的ip
              String brokerPort = "7676" ;  //OpenMQ server (broker)的port
              String username = "test";          //test賬號(hào)必須有可以接受此queue或者topic的權(quán)限
              String password = "test";
              ConnectionFactory       connectionFactory 
          = null;
             
          // TopicConnection              connection = null;
              QueueConnection              connection = null;
              Destination  destination 
          = null;
              Session                 session 
          = null;
              MessageConsumer               consumer 
          = null;
              TextMessage                  message 
          = null;

              
          public JMSListener(){
              }

              
          public void onMessage(Message msg){
                  
          if(msg instanceof TextMessage){
                      TextMessage txt 
          = (TextMessage)msg;
                      
          try {
                          System.out.println(
          "msg : " + txt.getText());
                      } 
          catch (JMSException ex) {
                          Logger.getLogger(JMSListener.
          class.getName()).log(Level.SEVERE, null, ex);
                      }
                  }
              }

              
          private void init() throws Exception{
                  connectionFactory 
          =  new ConnectionFactory();
                  connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostName, brokerHost);
                  connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostPort, brokerPort);
           
          //       connectionFactory.setProperty(ConnectionConfiguration.imqBrokerServiceName,brokerName);
                  connectionFactory.setProperty(ConnectionConfiguration.imqDefaultUsername,username);
                  connectionFactory.setProperty(ConnectionConfiguration.imqDefaultPassword,password);
          //        connection = connectionFactory.createTopicConnection();
                  connection = connectionFactory.createQueueConnection();
                  session 
          =  connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                  
          //destination =  new Topic(topicName);
                  destination =  new Queue(queueName);
                  consumer 
          = session.createConsumer(destination);
                  connection.start();
              }

             
          // 消費(fèi)消息
              public void consumeMessage() throws JMSException, Exception {
                  init();
                  connection.start();

                  System.out.println(
          "Consumer:->Begin listening");
                  
          // 開始監(jiān)聽
                  consumer.setMessageListener(this);
                  
          // Message message = consumer.receive();
              }

              
          public void destory() throws JMSException{
                  consumer.close();
                  session.close();
                  connection.close();
                  System.out.println(
          "Consumer:->stop listening");
              }
          //啟動(dòng)listen
          public static void main(String[] args) throws Exception {
                  JMSListener listen 
          = new JMSListener();
                  listen.consumeMessage();
                  
          //五秒后關(guān)閉監(jiān)聽
                  try {
                      Thread.sleep(
          5000);
                  } 
          catch (Exception e) {
                  }
                  listen.destory();

              }
          }


          2. 創(chuàng)建jms發(fā)送端
            
          /*
           * To change this template, choose Tools | Templates
           * and open the template in the editor.
           
          */

          package com.test.jms;

          import com.sun.messaging.ConnectionConfiguration;
          import com.sun.messaging.ConnectionFactory;
          import com.sun.messaging.Queue;
          import com.sun.messaging.Topic;
          import java.util.logging.Level;
          import java.util.logging.Logger;
          import javax.jms.Destination;
          import javax.jms.JMSException;
          import javax.jms.MessageProducer;
          import javax.jms.QueueConnection;
          import javax.jms.Session;
          import javax.jms.TextMessage;
          import javax.jms.TopicConnection;

          /**
           *
           * 
          @author ann
           
          */
          public class Send {
              
              String topicName 
          = "mytopic" ;     //要監(jiān)聽的topic名字
              String queueName = "myqueue" ;     //要監(jiān)聽的queue的名字
              String brokerHost = "localhost" ;  //OpenMQ server (broker)的ip
              String brokerPort = "7676" ;  //OpenMQ server (broker)的port
              String username = "customer";          //customer賬號(hào)必須有可以發(fā)送此queue或者topic的權(quán)限
              String password = "customer";
              ConnectionFactory       connectionFactory 
          = null;
              
          //TopicConnection              connection = null;
              QueueConnection              connection = null;
              Destination  destination 
          = null;
              Session                 session 
          = null;
              MessageProducer               producer 
          = null;
              TextMessage                  message 
          = null;

              
          public Send(){
                  
          try {
                      init();
                  } 
          catch (Exception ex) {
                     ex.printStackTrace();
                  }

              }
               
          private void init() throws Exception{
                  connectionFactory 
          =  new ConnectionFactory();
                  
          //connectionFactory.setProperty("imqAddressList","localhost:7676");
                  connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostName, brokerHost);
                  connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostPort, brokerPort);
                  connectionFactory.setProperty(ConnectionConfiguration.imqDefaultUsername,username);
                  connectionFactory.setProperty(ConnectionConfiguration.imqDefaultPassword,password);
                  
          //connection = connectionFactory.createTopicConnection();

                  connection 
          = connectionFactory.createQueueConnection();

                 session 
          =  connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                 
          // destination =  new Topic(topicName);
                 destination =  new Queue(queueName);
                  
          //session.createTopic(topicName);
                  producer = session.createProducer(destination);

                  connection.start();
              }
               
          public void send(String msg) throws JMSException{
                  
          try {
                      message 
          = session.createTextMessage();
                      message.setText(msg);
                      System.out.println(
          "Producer:->Sending message: " + msg);
                      producer.send(message);
                      System.out.println(
          "Producer:->Message sent complete!");
                     
                  } 
          catch (JMSException ex) {
                      ex.printStackTrace();
                  }
          finally{
                       connection.close();
                  }
               }

               
          public static void main(String[] args)throws JMSException {
                  
                  Send send 
          = new Send();
                  send.send(
          "test user : ann");
              }



          }



            

          當(dāng)下,把心放下 放下如果是可能的,那一定是在當(dāng)下,
          不在過去,也不在未來。
          當(dāng)下放下。唯有活在當(dāng)下,你的問題才能放下。

          主站蜘蛛池模板: 修文县| 丰都县| 尖扎县| 南充市| 广安市| 封丘县| 灵寿县| 德兴市| 汉中市| 达日县| 镇沅| 丽水市| 微山县| 古蔺县| 阳东县| 台北县| 祁连县| 通江县| 宾阳县| 邵阳县| 汶川县| 青岛市| 孝义市| 金平| 河池市| 永和县| 兴仁县| 宁国市| 体育| 鄂州市| 汉阴县| 上虞市| 新宁县| 寿阳县| 罗山县| 安康市| 延庆县| 建阳市| 安徽省| 浦县| 衡阳市|