俊星的BLOG

          ActiveMQ初探

          Apache ActiveMQ是一個高性能的消息代理,實現(xiàn)了JMS 1.1。
          目前的最新版本是5.4.1
          1、下載相關(guān)包,并將activemq-all-5.4.1.jar加入到工程中。

          2、運行bin目錄下的actionmq.bat文件,啟動broker,在conf下是相關(guān)的配置文件,例如activemq.xml

          3、可通過訪問http://localhost:8161/admin/來查看啟動情況,有可通過netstat -an|find "61616" 來查看端口,因為通過查看activemq.xml中即可發(fā)現(xiàn)傳輸連接器即配置的占用61616端口:
          <transportConnectors>
                  <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
           </transportConnectors>

          4、編寫消息接收類:
          import javax.jms.Connection;
          import javax.jms.ConnectionFactory;
          import javax.jms.Destination;
          import javax.jms.JMSException;
          import javax.jms.MessageConsumer;
          import javax.jms.Session;
          import javax.jms.TextMessage;

          import org.apache.activemq.ActiveMQConnection;
          import org.apache.activemq.ActiveMQConnectionFactory;

          public class JmsReceiver {
              
          public static void main(String[] args) throws JMSException {
                  ConnectionFactory connectionFactory 
          = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                          ActiveMQConnection.DEFAULT_PASSWORD, 
          "tcp://localhost:61616");
                  Connection connection 
          = connectionFactory.createConnection();
                  connection.start();
                  Session session 
          = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
                  
          // 需要在activemq的控制臺中創(chuàng)建queue:testqueue
                  Destination destination = session.createQueue("testqueue");
                  MessageConsumer consumer 
          = session.createConsumer(destination);
                  
                  
          while (true{
                      TextMessage message 
          = (TextMessage) consumer.receive(1000);
                      
          if (null != message)
                          System.out.println(
          "收到消息:" + message.getText());
                      
          else
                          
          break;
                  }

                  session.close();
                  connection.close();
              }

          }


          5、編寫消息產(chǎn)生類:
          import javax.jms.Connection;
          import javax.jms.ConnectionFactory;
          import javax.jms.DeliveryMode;
          import javax.jms.Destination;
          import javax.jms.JMSException;
          import javax.jms.MessageProducer;
          import javax.jms.Session;
          import javax.jms.TextMessage;

          import org.apache.activemq.ActiveMQConnection;
          import org.apache.activemq.ActiveMQConnectionFactory;

          public class JmsSender {
              
          public static void main(String[] args) throws JMSException {
                  ConnectionFactory connectionFactory 
          = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                          ActiveMQConnection.DEFAULT_PASSWORD, 
          "tcp://localhost:61616");
                  Connection connection 
          = connectionFactory.createConnection();
                  connection.start();
                  Session session 
          = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
                  Destination destination 
          = session.createQueue("testqueue");
                  MessageProducer producer 
          = session.createProducer(destination);
                  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                  sendMsg(session, producer);
                  session.commit();
                  connection.close();
              }


              
          public static void sendMsg(Session session, MessageProducer producer) throws JMSException {
                  
          // 創(chuàng)建一條文本消息
                  TextMessage message = session.createTextMessage("你好 ActiveMQ!");
                  producer.send(message);
                  System.out.println(
          "");
              }

          }


          6、通過訪問http://localhost:8161/admin/,在queue中創(chuàng)建testqueue,先執(zhí)行消息發(fā)送類,后執(zhí)行消息接收類,借看看到消息接收類的如下執(zhí)行結(jié)果:
          收到消息:你好 ActiveMQ!
          收到消息:你好 ActiveMQ!

          (執(zhí)行多次消息發(fā)送類,則會接受到多條“你好 ActiveMQ!”消息)

          posted on 2010-10-28 17:53 俊星 閱讀(2906) 評論(1)  編輯  收藏

          評論

          # re: ActiveMQ初探 2011-04-21 16:10 71569973

          測試通過。多謝  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 房产| 达州市| 宁国市| 新兴县| 阿拉善右旗| 綦江县| 浪卡子县| 成武县| 嫩江县| 和龙市| 武川县| 沙湾县| 蕲春县| 永康市| 沙雅县| 于都县| 涟水县| 南郑县| 那坡县| 伊通| 巴东县| 石嘴山市| 岳西县| 唐河县| 区。| 永修县| 洪江市| 宜川县| 白水县| 平谷区| 锡林郭勒盟| 卫辉市| 赣州市| 金湖县| 招远市| 泰州市| 额敏县| 田东县| 高淳县| 晋州市| 冕宁县|