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