熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring2.5整合ActiveMQ 5.2(P2P文本消息)

2022-06-13   來源: Java開源技術 

  項目環境

  JDK

  ActiveMQ

  所用的包都是ActiveMQ自帶的引用的包如下圖

  

   package stujmspptxt;

import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;
import orgsprreJmsTemplate;
import orgsprreMessageCreator;

import javaxjmsDestination;
import javaxjmsJMSException;
import javaxjmsMessage;
import javaxjmsSession;

/**
* 消息發送者
*
* @author leizhimin ::
*/
public class MySender {
        public static void main(String[] args) {
                ApplicationContext ctx = new ClassPathXmlApplicationContext(/applicationContextxml);
                JmsTemplate template = (JmsTemplate) ctxgetBean(jmsTemplate);
                Destination destination = (Destination) ctxgetBean(destination);

                templatesend(destination new MessageCreator() {
                        public Message createMessage(Session session) throws JMSException {
                                return sessioncreateTextMessage(發送消息Hello ActiveMQ Text Message!);
                        }
                });
                Systemoutprintln(成功發送了一條JMS消息);
        }
}

   package stujmspptxt;

import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;
import orgsprreJmsTemplate;

import javaxjmsDestination;
import javaxjmsJMSException;
import javaxjmsTextMessage;

/**
* 消息接收者
*
* @author leizhimin ::
*/
public class MyReceiver {
        public static void main(String[] args) throws JMSException {
                ApplicationContext ctx = new ClassPathXmlApplicationContext(/applicationContextxml);
                JmsTemplate template = (JmsTemplate) ctxgetBean(jmsTemplate);
                Destination destination = (Destination) ctxgetBean(destination);
                while (true) {
                        TextMessage txtmsg = (TextMessage) templatereceive(destination);
                        if (null != txtmsg)
                                Systemoutprintln(收到消息內容為: + txtmsggetText());
                        else
                                break;
                }
        }
}

   <?xml version= encoding=UTF?>
<beans xmlns= xmlns:xsi=instance
             xmlns:context=
             xsi:schemaLocation=
        beansxsd
        
        contextxsd>

        <! 配置JMS連接工廠 >
        <bean id=connectionFactory class=orgapacheactivemqspringActiveMQConnectionFactory>
                <property name=brokerURL value=tcp://localhost:/>
        </bean>

        <! 配置JMS模版 >
        <bean id=jmsTemplate class=orgsprreJmsTemplate>
                <property name=connectionFactory ref=connectionFactory/>
        </bean>

        <! 發送消息的目的地(一個隊列) >
        <bean id=destination class=orgmandActiveMQQueue>
                <! 設置消息隊列的名字 >
                <constructorarg index= value=HelloWorldQueue/>
        </bean>
</beans>

  運行發送端三次

   成功發送了一條JMS消息

Process finished with exit code

  然後再運行接收端一次

   收到消息內容為: 發送消息Hello ActiveMQ Text Message!
收到消息內容為: 發送消息Hello ActiveMQ Text Message!
收到消息內容為: 發送消息Hello ActiveMQ Text Message!

  繼續測試發現接收端接收一條消息後不退出程序而是繼續等待一旦有消息發送過來就獲取到然後輸出!

  發一張圖看看

  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28780.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.