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