少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(22)

          我參與的團(tuán)隊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          1,項目中所引的包

           

          2,發(fā)送和接受消息的代碼,每個都是一個Junit函數(shù)

          package test;

          import javax.jms.Destination;
          import javax.jms.JMSException;
          import javax.jms.Message;
          import javax.jms.Session;
          import javax.jms.TextMessage;

          import junit.framework.TestCase;

          import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
          import org.junit.Test;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.FileSystemXmlApplicationContext;
          import org.springframework.jms.core.JmsTemplate;
          import org.springframework.jms.core.MessageCreator;

          public class ActivemqTest extends TestCase {
           @Test
           public void testJmsTemplateSend() {      

            ApplicationContext ctx = new FileSystemXmlApplicationContext(
              "WebRoot/WEB-INF/applicationContext.xml");
            JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
            Destination destination = (Destination) ctx.getBean("destination");

            template.send(destination, new MessageCreator() {
             public Message createMessage(Session session) throws JMSException {
              return session.createTextMessage("發(fā)送消息:Hello ActiveMQ Text Message!");
             }
            });
            System.out.println("成功發(fā)送了一條JMS消息");
           }

           @Test
           public void testJmsTemplateReceive() throws JMSException {
            ApplicationContext ctx = new FileSystemXmlApplicationContext(
              "WebRoot/WEB-INF/applicationContext.xml");
            JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
            Destination destination = (Destination) ctx.getBean("destination");
            while (true) {
             TextMessage txtmsg = (TextMessage) template.receive(destination);
             if (null != txtmsg)
              System.out.println("收到消息內(nèi)容為: " + txtmsg.getText());
             else
              break;
            }
           }

           public static void main(String[] args) throws Exception{
            new ActivemqTest().testJmsTemplateSend();
            
            new ActivemqTest().testJmsTemplateReceive();
           }
          }

           

          3,applicationContext.xml文件內(nèi)容

          <?xml version="1.0" encoding="UTF-8"?>  
          <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
                 xmlns:context="http://www.springframework.org/schema/context
                 xsi:schemaLocation="http://www.springframework.org/schema/beans  
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
              http://www.springframework.org/schema/context  
              http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
           
              <!-- 配置JMS連接工廠 -->  
              <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">  
                  <property name="brokerURL" value="tcp://localhost:61616"/>  
              </bean>  
           
              <!-- 配置JMS模版 -->  
              <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
                  <property name="connectionFactory" ref="connectionFactory"/>  
              </bean>  
           
              <!-- 發(fā)送消息的目的地(一個隊列) -->  
              <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
                  <!-- Set the Queue Name -->  
                  <constructor-arg index="0" value="HelloWorldQueue"/>  
              </bean>  
          </beans>




          http://blog.csdn.net/hnzhangshilong/article/details/6629780

          posted on 2012-08-25 21:34 abin 閱讀(1064) 評論(0)  編輯  收藏 所屬分類: ActiveMQ

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新乐市| 岳西县| 尤溪县| 梁河县| 紫阳县| 三原县| 原平市| 平舆县| 金堂县| 景泰县| 常州市| 武冈市| 什邡市| 玛纳斯县| 兴和县| 崇仁县| 高淳县| 郧西县| 永丰县| 偃师市| 讷河市| 北票市| 长兴县| 乐清市| 崇礼县| 忻州市| 子洲县| 利津县| 康保县| 临夏县| 昌乐县| 皮山县| 玉环县| 措勤县| 长泰县| 大足县| 大港区| 饶河县| 阳信县| 贺州市| 澄江县|