Spring2.5 integrated ActiveMQ 5.2

Source: Internet
Author: User
Tags message queue

ActiveMQ5.2

All the packages used are included in ActiveMQ. The referenced package is shown in the following figure:

PackageStujms. p2ptxt;

ImportOrg. springframework. context. ApplicationContext;
ImportOrg. springframework. context. support. ClassPathXmlApplicationContext;
ImportOrg. springframework. jms. core. JmsTemplate;
ImportOrg. springframework. jms. core. MessageCreator;

ImportJavax. jms. Destination;
ImportJavax. jms. JMSException;
ImportJavax. jms. Message;
ImportJavax. jms. Session;

/**
* Message sender
*
* @ Author leizhimin 2009-8-13 17:01:48
*/
Public ClassMySender {
        Public Static VoidMain (String [] args ){
ApplicationContext ctx =NewClassPathXmlApplicationContext ("/ApplicationContext. xml");
JmsTemplate template = (JmsTemplate) ctx. getBean ("JmsTemplate");
Destination destination = (Destination) ctx. getBean ("Destination");

Template. send (destination,NewMessageCreator (){
                        PublicMessage createMessage (Session session)ThrowsJMSException {
                                ReturnSession. createTextMessage ("Send Message: Hello ActiveMQ Text Message! ");
                        }
});
System. out. println ("A jms message is successfully sent");
        }
}

 

PackageStujms. p2ptxt;

ImportOrg. springframework. context. ApplicationContext;
ImportOrg. springframework. context. support. ClassPathXmlApplicationContext;
ImportOrg. springframework. jms. core. JmsTemplate;

ImportJavax. jms. Destination;
ImportJavax. jms. JMSException;
ImportJavax. jms. TextMessage;

/**
* Message recipient
*
* @ Author leizhimin 2009-8-13 17:02:04
*/
Public ClassMycycler {
        Public Static VoidMain (String [] args)ThrowsJMSException {
ApplicationContext ctx =NewClassPathXmlApplicationContext ("/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 ("Received message content :"+ Txtmsg. getText ());
                        Else
                                Break;
                }
        }
}

<?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">

<! --Configure the JMS connection factory-->
        <Bean Id= "ConnectionFactory" Class= "Org. apache. activemq. spring. ActiveMQConnectionFactory">
                <Property Name= "BrokerURL" Value= "Tcp: // localhost: 61616"/>
        </Bean>

<! --Configure the JMS Template-->
        <Bean Id= "JmsTemplate" Class= "Org. springframework. jms. core. JmsTemplate">
                <Property Name= "ConnectionFactory" Ref= "ConnectionFactory"/>
        </Bean>

<! --Message sending destination (one queue)-->
        <Bean Id= "Destination" Class= "Org. apache. activemq. command. ActiveMQQueue">
<! --Set the name of the message queue-->
                <Constructor-arg Index= "0" Value= "HelloWorldQueue"/>
        </Bean>
</Beans>

Run the sender three times:

A jms message is sent successfully.

Process finished with exit code 0

Run the acceptor again:

The Message content is: Send Message: Hello ActiveMQ Text Message!
The Message content is: Send Message: Hello ActiveMQ Text Message!
The Message content is: Send Message: Hello ActiveMQ Text Message!

ContinueTestIt is found that the receiving end does not exit the program after receiving a message, but continues to wait. Once a message is sent, it is obtained and output!

Send a picture to see:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.