俊星的BLOG

          ActiveMQ初探

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

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

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

          4、編寫(xiě)消息接收類(lèi):
          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的控制臺(tái)中創(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、編寫(xiě)消息產(chǎn)生類(lèi):
          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、通過(guò)訪(fǎng)問(wèn)http://localhost:8161/admin/,在queue中創(chuàng)建testqueue,先執(zhí)行消息發(fā)送類(lèi),后執(zhí)行消息接收類(lèi),借看看到消息接收類(lèi)的如下執(zhí)行結(jié)果:
          收到消息:你好 ActiveMQ!
          收到消息:你好 ActiveMQ!

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

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

          評(píng)論

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

          測(cè)試通過(guò)。多謝  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 化隆| 安西县| 长治县| 南京市| 德格县| 竹北市| 开鲁县| 江阴市| 西畴县| 泰安市| 德州市| 即墨市| 舞钢市| 驻马店市| 长葛市| 贵溪市| 黎城县| 清流县| 满洲里市| 宜良县| 萨嘎县| 三穗县| 新余市| 杂多县| 襄城县| 远安县| 富平县| 伊宁县| 建昌县| 百色市| 镇坪县| 延安市| 雷波县| 万宁市| 绿春县| 景德镇市| 平度市| 渭源县| 钟祥市| 黄骅市| 汽车|