Spring JMS combined with ACTIVEMQ

Source: Internet
Author: User

First, download ACTIVEMQ

The version used in this example is 5.9.0

Links: http://download.csdn.net/download/hpw90333/6652367

An error occurred after the 5.14.2 operation was downloaded first:

Javax.jms.JMSException:Cannot send, channel has already failed:tcp://127.0.0.1:61616

Transport Connection to:tcp://127.0.0.1:50941 failed:java.io.EOFException | Org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport:tcp:///127.0.0.1:[email protected]

I haven't found out why.

Guess it's possible to add a new configuration or a spring version issue

Second, unzip the Activemq-broker-5.9.0.jar Activemq-client-5.9.0.jar into the spring project note the matching of versions and jar packages running on the machine

Third, configure the ACTIVEMQ bean Activemqconnectionfactory in the spring configuration file to inject the ACTIVEMQ address Brokerurl to be connected, for example: tcp://localhost:61616 ( 61616 Port for ACTIVEMQ)

Configure spring Jmstemplate Injection Properties Mqconnectionfactory

Iv. Testing

Test the production side:

New Classpathxmlapplicationcontext ("Applicationcontext.xml");         = App.getbean (jmstemplate.  Class);        Jmstemplate.convertandsend ("Gamelife.alert.queue", "test Message");

Get jmstemplate and then call Convertandsend to send a message to the specified channel

To test the consumer end:

New Classpathxmlapplicationcontext (                "Applicationcontext.xml");         = (jmstemplate) context.getbean ("Jmstemplate");         // System.out.println (jmstemplate = = null);        String message = (string) jmstemplate                . Receiveandconvert ("Gamelife.alert.queue");        SYSTEM.OUT.PRINTLN (message);

Get Jmstemplate Call Recieveandconvert method to read messages from a specified channel

Monitoring can be configured directly on the consumer side:

Write a listening bean to implement the JMS MessageListener interface

 Package COM.HY.JMS; Import Javax.jms.Message; Import Javax.jms.MessageListener;  Public class Implements messagelistener{    @Override    publicvoid  onMessage (Message message) {        System.out.println ("received message:" + message);}    }

Configuring the bean to join the bean in the JMS Listener listening container

<Beanname= "Jmshandler"class= "Com.hy.jms.JmsHandler"></Bean>    <Jms:listener-container>        <Jms:listenerDestination= "Gamelife.alert.queue2"ref= "Jmshandler"/>    </Jms:listener-container>

Description

JMS is an asynchronous message

The structure diagram is as follows:

Spring JMS combined with ACTIVEMQ

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.