SPRINGFRAMEWORK4 Series SPRINGJMS: (iv) receiving messages asynchronously 2. Building listeners

Source: Internet
Author: User

Building listeners that implement interfacesWriting listener classes and implementing listener interfaces
public class Listenerdemo implements MessageListener {public void onMessage (Message message) {System.out.print    ln (message.tostring ()); }}

Sign up for MDP
<!--This is the Message driven POJO (MDP)--><bean id= "MessageListener1" class= "Spring.jms.ListenerDemo"/>

Configure Listenercontainerfactory
<bean id= "Jmscontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > < Property Name= "ConnectionFactory" ref= "cachingconnectionfactory"/> <property name= "Destination" ref= " Jmsdestination "/> <property name=" MessageListener "ref=" MessageListener1 "/></bean>

Test

Send message to queue for specific reference SPRINGFRAMEWORK4 series of SPRINGJMS: (a) Build JMS

Then initialize spring, which is very simple

If the same spring config is used, the spring-config is initialized when the message is sent, and this time the listener is working without having to initialize the spring again.

ApplicationContext context = new Classpathxmlapplicationcontext ("Spring-config.xml");

Build adapter-Managed listenersWriting Listener Classes

No need to implement interfaces or register into spring's context

public class ListenerDemo2 {public void onMessage (Message message) {SYSTEM.OUT.PRINTLN (message.tostring ()); }

Configure the adapter
<bean id= "Messageadapter" class= "Org.springframework.jms.listener.adapter.MessageListenerAdapter" > <cons tructor-arg> <bean class= "Spring.jms.ListenerDemo2"/> </constructor-arg> <property name= "de Faultlistenermethod "value=" OnMessage "/></bean>

Configure Listenercontainerfactory

Configuring the adapter as a listener in container

<bean id= "Jmscontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > < Property Name= "ConnectionFactory" ref= "cachingconnectionfactory"/> <property name= "Destination" ref= " Jmsdestination "/> <property name=" MessageListener "ref=" Messageadapter "/></bean>

Test

Send message to queue for specific reference SPRINGFRAMEWORK4 series of SPRINGJMS: (a) Build JMS

Then initialize spring, which is very simple.

It is recommended to send message and listener using different spring config to avoid conflicts.

If the same spring config is used, the spring-config is initialized when the message is sent, and this time the listener is working without having to initialize the spring again.

ApplicationContext context = new Classpathxmlapplicationcontext ("Spring-config.xml");

Building annotation ListenersWriting Listener Classes

Use note @componet to declare that this is a component class

Use note @jmslistener to declare that this is a listener method, where destination represents the queue name of the listener, and containerfactory represents the listenercontainerfactory used.

@Component ("ListenerDemo3") public class ListenerDemo3 {@JmsListener (destination = "Testqueue", containerfactory = "JMS Listenercontainerfactory ") public void ReceiveMessage (String message) {System.out.println (" Received < "+ mes    Sage + ">"); }}

Configure Listenercontainerfactory

There is no need to define listeners in the listener container at this time. Here I have an additional definition of attribute concurrency, which indicates that several consumer will be generated at the same time.

Specifically, spring is automatically generated.

<bean id= "Jmscontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > < Property Name= "ConnectionFactory" ref= "cachingconnectionfactory"/> <property name= "Destination" ref= " Jmsdestination "/> <property name=" concurrency "value=" 3-10 "/></bean>

Configuring the JMS Driver

In the spring config file, you need to define the JMS note driver, which automatically puts the listener class of annotations into the listener container, and it begins to work.

<jms:annotation-driven/>

Here the author has done an experiment, in no configuration Component-scan, the listener can also work. It appears that this annotation driver should automatically scan the @componet annotation symbols.

<context:component-scan base-package= "Spring"/>


SpringFramework4 Series SPRINGJMS: (iv) receive messages asynchronously 2. Build Listener

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.