Package Com.mq.consumer;
Import javax.jms.JMSException;
Import Javax.jms.Message;
Import Javax.jms.MessageListener;
Import Javax.jms.TextMessage;
public class Consumermessagelistener implements messagelistener{
@Override
public void OnMessage (message message) {
TextMessage TM = (textmessage) message;
try {
SYSTEM.OUT.PRINTLN ("---------Message consumption---------");
System.out.println ("subscriber: \ t consumerClient1");
SYSTEM.OUT.PRINTLN ("message content: \ t" + tm.gettext ());
SYSTEM.OUT.PRINTLN ("message id:\t" + Tm.getjmsmessageid ());
SYSTEM.OUT.PRINTLN ("message destination:\t" + tm.getjmsdestination ());
/* SYSTEM.OUT.PRINTLN ("---------More information---------");
SYSTEM.OUT.PRINTLN (Tostringbuilder.reflectiontostring (tm)); */
System.out.println ("-------------------------");
} catch (JMSException e) {
E.printstacktrace ();
}
}
}
The corresponding spring configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!--Configuring the JMS Connection Factory--
<bean id= "Consumerconnectionfactory" class= "Org.apache.activemq.ActiveMQConnectionFactory" >
<property name= "Brokerurl" value= "Failover: (tcp://10.31.88.166:61616)"/>
<property name= "Useasyncsend" value= "true"/>
<property name= "ClientID" value= "Consumerclienctconnect"/>
</bean>
<!--defining Messages destination
<bean id= "topicdestination" class= "Org.apache.activemq.command.ActiveMQTopic" >
<constructor-arg value= "Testspringtopic"/>
</bean>
<!--Configure message consumption listener-->
<bean id= "Consumermessagelistener" class= " Com.mq.consumer.ConsumerMessageListener "/>
<bean id=" ConsumerMessageListener2 "class=" Com.mq.consumer.ConsumerMessageListener2 "/>
<!--message Subscription Client 1--
<bean id= "ConsumerListenerClient1" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" >
<property name= "ConnectionFactory" ref= "Consumerconnectionfactory"/>
<!--turn on subscription mode--
<property name= "Pubsubdomain" value= "true"/>
<property name= "Destination" ref= "Topicdestination"/>
<property name= "subscriptiondurable" value= "true"/>
<!---here is the ID to set the receive client, when persisted, but the client is not in the line, the message exists in the database until it is consumed by the client of this ID.
<property name= "ClientId" value= "ConsumerClient1"/>
<property name= "MessageListener" ref= "Consumermessagelistener"/>
<!--message Answering methods
Session.auto_acknowledge Message Auto Sign
Session.client_acknowledge Client call acknowledge method manual sign-off
Session.dups_ok_acknowledge does not have to be signed, messages may be sent repeatedly
-
<property name= "Sessionacknowledgemode" value= "1"/>
</bean>
</beans>
ACTIVEMQ Consumer Integration Spring