沉睡森林@漂在北京

          本處文章除注明“轉(zhuǎn)載”外均為原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            152 隨筆 :: 4 文章 :: 114 評(píng)論 :: 0 Trackbacks
           

          第一: 在applicationContext.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>  
           <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
             <property name="brokerURL">
               <value>tcp://192.168.3.48:61616</value>
             </property>
           
           </bean>
           
           <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
              <property name="connectionFactory">
                <ref bean="connectionFactory"/>
              </property>
           </bean>
           
           <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
             <constructor-arg index="0">
               <value>HelloWorldQueue</value>
             </constructor-arg>
           </bean>
          </beans>

          2.寫發(fā)送方

          package ch13.JMS;

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

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.springframework.jms.core.JmsTemplate;
          import org.springframework.jms.core.MessageCreator;

          public class HelloWorldSender
          {
           public static void main(String args[]) throws Exception
           {
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "ch13/JMS/applicationContext.xml" });
            JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
            Destination destination = (Destination) context.getBean("destination");
            jmsTemplate.send
            (
             destination, new MessageCreator()
             {
              public Message createMessage(Session session) throws JMSException
              {
               return session.createTextMessage("大家好這個(gè)是測(cè)試!");
              }
             }
            );
           }

          }
          3.寫接收方

          package ch13.JMS;

          import javax.jms.Destination;
          import javax.jms.TextMessage;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.springframework.jms.core.JmsTemplate;

          public class HelloWorldReciver
          {

           public static void main(String args[]) throws Exception
           {
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "ch13/JMS/applicationContext.xml" });
            JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
            Destination destination = (Destination) context.getBean("destination");
            System.out.println("will wait:" + jmsTemplate.getReceiveTimeout()+ " seconds for message");
            TextMessage msg = (TextMessage) jmsTemplate.receive(destination);
            System.out.println("reviced msg is:" + msg.getText());
           }

          }

          4.啟動(dòng)activemq中bin 下的activemq.bat

          5.先運(yùn)行 HelloWorldSender

          6.再運(yùn)行 HelloWorld

          7.結(jié)果:

          will wait:-1 seconds for message
          reviced msg is:大家好 這個(gè)是測(cè)試!

          posted on 2010-03-31 20:57 王總兵 閱讀(4895) 評(píng)論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 犍为县| 龙川县| 和顺县| 宁化县| 林口县| 台南市| 仙游县| 伊金霍洛旗| 延边| 呼玛县| 长顺县| 错那县| 密云县| 长海县| 砀山县| 巨鹿县| 富川| 手机| 策勒县| 云南省| 寿光市| 全南县| 宁武县| 略阳县| 铜川市| 江西省| 万安县| 渭南市| 凌源市| 赫章县| 乐业县| 杭锦后旗| 韶关市| 霍林郭勒市| 丘北县| 克东县| 九江县| 姜堰市| 灵山县| 牙克石市| 耒阳市|