JMS--ACTIVEMQ and Spring Integration (II.)

Source: Internet
Author: User
Tags gettext

Original address: http://blog.csdn.net/haoxingfeng/article/details/9167895

When we integrate with spring, for consumers we have three different types of listeners to choose from, they are messagelistener,sessionawaremessagelistener, Messagelisteneradapter, let's talk about the difference between them.

1, MessageListener

MessageListener is the most primitive message listener and is the interface defined in the JMS specification. The OnMessage (Message message) interface for processing messages is defined, and the method accepts only one parameter of type message, and in Jms--activemq and Spring (a) we use MessageListener.

2, Sessionawaremessagelistener

Sessionawaremessagelistener is not the original message listener. It's spring for us, it's not a standard JMS messagelistener. The original MessageListener is purely for receiving messages, and if we are using MessageListener to process the received messages we need to send a message notifying the other party that we have received the message. Then we need to get a connection or session back in the code. Sessionawaremessagelistener is designed to make it easier for us to send a reply message after receiving a message, and it also provides us with a onmessage method to handle the received message, but this method can receive two parameters at the same time. One is the message that is currently received, and the other is the session object that can be used to send the message.

XML configuration:

<bean id= "Sessionawarequeue" class= "Org.apache.activemq.command.ActiveMQQueue" >
<constructor-arg>
<value>sessionAwareQueue</value>
</constructor-arg>
</bean>

<bean id= "Sessionawareconsumermessagelistener" class= "Com.ghq.activemq.SessionAwareConsumer" >
<property name= "Destination" ref= "queue" ></property>
</bean>

<bean id= "Sessionawaremessagelistenercontainer" class= " Org.springframework.jms.listener.DefaultMessageListenerContainer ">
<property name= "Destination" ref= "Sessionawarequeue" ></property>
<property name= "MessageListener" ref= "Sessionawareconsumermessagelistener" ></property>
<property name= "ConnectionFactory" ref= "ConnectionFactory" ></property>
</bean>

Java code:

public class Sessionawareconsumer implements sessionawaremessagelistener<message> {

Private Destination Destination;

public void onMessage (Message message, Session session) throws JMSException {
TextMessage tmessage = (textmessage) message;
System.out.println ("Sessionawareconsumer Receive Message:" +tmessage.gettext ());
MessageProducer producer = Session.createproducer (destination);
Producer.send (Session.createtextmessage (Tmessage.gettext ()));
}

Public Destination getdestination () {
return destination;
}

public void Setdestination (Destination Destination) {
This.destination = destination;
}

}

Producer sends a message to Sessionawarequeue, Sessionawaremessagelistenercontainer receives the message to pass the message to the true processing of this sessionawareconsumer, Sessionawareconsumer out the message and outputs a change message to the queue and sends a change message to the queue. Messagelistenercontainer processed the change message.

3, Messagelisteneradapter

The Messagelisteneradapter implements the Sessionawaremessagelistener and MessageListener interfaces. Its main function is to convert the received message into a type

It is then presented to a common Java class for processing by reflection.

The Messagelisteneradapter will convert the received message as follows:

TextMessage converted to a string object;

Bytesmessage converted to a byte array;

Mapmessage converted to map object;

The objectmessage is converted to the corresponding Serializable object.

Now that's said, Messagelisteneradapter will make a type conversion of the received message, then use reflection to give it to the real target processor-- A common Java class for processing (if the real target processor is a messagelistener or a sessionawaremessagelistener, Then spring will call their OnMessage method directly using the received message object as a parameter, and no longer use reflection to invoke it, then we need to specify such a target class when defining a messagelisteneradapter. This target class can be specified by the Messagelisteneradapter constructor parameter, such as:

<bean id= "Messagelisteneradapter" class= " Org.springframework.jms.listener.adapter.MessageListenerAdapter ">

<constructor-arg><bean class= "Com.ghq.activemq.Consumer"/></constructor-arg>

</bean>

It can also be specified by its Delegate property, such as:

Xml:

<bean id= "Adapterqueue" class= "Org.apache.activemq.command.ActiveMQQueue" >
<constructor-arg>
<value>adapterqueue</value>
</constructor-arg>
</ Bean>

<bean id= "Messagelisteneradapter" class= "    Org.springframework.jms.listener.adapter.MessageListenerAdapter ",
<property name=" delegate ";
<bean class= "Com.ghq.activemq.AdapterRealClass" ></BEAN>
</property>
<!--default call The Handlemessage method of the Com.ghq.activemq.AdapterRealClass class can also be set by Defaultlistenermethod property to--> the method name;
<!--< Property Name= "Defaultlistenermethod" value= "Myhandlemessage" ></property>-->
</bean>


<bean id= "Adaptermessagelistenercontainer" class= " Org.springframework.jms.listener.DefaultMessageListenerContainer ">
<property name= "Destination" ref= "Adapterqueue" ></property>
<property name= "MessageListener" ref= "Messagelisteneradapter" ></property>
<property name= "ConnectionFactory" ref= "ConnectionFactory" ></property>
</bean>

In addition to automatically messagelisteneradapter an ordinary Java class as a messagelistener to handle the received message, the other main function is to automatically send a return message .

When the return value of the method that we use to process the received message is not empty, spring automatically encapsulates it as a JMS message and then replies automatically. So where will this reply message be sent at this time? There are two main ways to specify this.
First, you can specify the destination of the reply message that corresponds to the message by sending the Setjmsreplyto method of the messages. Here we make a change to the code that our producer sends the message, specify the reply destination of the message to be a queue destination called ResponseQueue before sending the message, the code is as follows:

In the Send this code, add the following line:

TextMessage tmessage = session.createtextmessage (message);
Tmessage.setjmsreplyto (ResponseQueue);

XML configuration (remove the comment above calling the default method Handlemessage, call our own myhandlemessage):

<bean id= "ResponseQueue" class= "Org.apache.activemq.command.ActiveMQQueue" >
<constructor-arg>
<value>responseQueue</value>
</constructor-arg>
</bean>
<bean id= "Responsequeuelistener" class= "Com.ghq.activemq.ResponseQueueListener"/>

<bean id= "Adaptermessagelistenercontainer" class= " Org.springframework.jms.listener.DefaultMessageListenerContainer ">
<property name= "Destination" ref= "ResponseQueue" ></property>
<property name= "MessageListener" ref= "Responsequeuelistener" ></property>
<property name= "ConnectionFactory" ref= "ConnectionFactory" ></property>
</bean>

Simple Java class:

public class Adapterrealclass {

public void Handlemessage (String message) {
System.out.println ("Adapterrealclass handlemessage ():" +message);
}

public string Myhandlemessage (String message) {
System.out.println ("Adapterrealclass myhandlemessage ():" +message);
Return "Myhandlemessage" +message;
}
}

Second, it is specified by Messagelisteneradapter's Defaultresponsedestination property. Here we also do a test, first of all, to maintain the code that the producer sends the message, that is, the message's reply destination is not specified by the Setjmsreplyto method of message before sending it.

Now that we can specify the destination of the messagelisteneradapter automatically sending the reply message in two ways, how will it be sent when both of our methods are specified and their destination is different? When both methods specify a reply destination for a message, the destination specified by using the Setjmsreplyto method that sends the message will have a higher priority, and Messagelisteneradapter will only send a reply message to the message reply destination specified by the method.

JMS--ACTIVEMQ and Spring Integration (II.)

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.