Reprinted from: http://zld406504302.iteye.com/blog/1909751
Recently with spring3.2 JMS and activemq5.8 integration, the implementation of asynchronous send, asynchronous receive function, and attached the test code
1) UML as follows
[img]
Message acceptance is fully managed in Org.springframework.jms.listener.DefaultMessageListenerContainer, sending the client without concern for message acceptance
[/IMG]
2) Applicationcontext.xml Fragment
Java code <bean id= "Taskexecutor" class= " Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor "> <! -- number of core threads, default to 1 --> <property name= "CorePoolSize" Value= "5" /> <!-- Maximum number of threads, default is Integer.max_value --> <property name= "Maxpoolsize" value= "5" /> <!-- Queue maximum length, you generally need to set the value >=notifyScheduledMainExecutor.maxNum; default is Integer.max_ value --> <property name= "queueCapacity" value= "1000" /> <!-- thread pool maintenance threads allow idle time, default is 60s --> <property name= "Keepaliveseconds" value= " /> " <!-- ThreadsPool-to-Reject task (wireless path available) processing policy, currently only support AbortPolicy, Callerrunspolicy; default is the latter --> < Property name= "Rejectedexecutionhandler" > <!-- abortpolicy: Throwing java.util.concurrent.RejectedExecutionException exceptions directly --> <!-- callerrunspolicy: The main thread executes the task directly, and then tries to add the next task to the thread pool after execution. Can effectively reduce the speed of adding tasks to the thread pool --> <!-- Discardoldestpolicy: Discard old tasks, temporarily unsupported; causes discarded tasks to not be executed again --> <!-- discardpolicy: Discard the current task, temporarily unsupported, cause the discarded task to not be executed again --> <bean class= "Java.util.concurrent.threadpoolexecutor$callerrunspolicy" /> </property> </bean> <!--jms Connection pool-- optimizedackscheduledackinterval: Message Confirmation cycle --> <bean id= "jmsconnectionfactory" class= "Org.apache.activemq.pool.PooledConnectionFactory" > <property name= "ConnectionFactory" > <bean class= "Org.apache.activemq.ActiveMQConnectionFactory" > <property name= " Brokerurl " value=" tcp://localhost:61616 " /> <property name= "Closetimeout" value= "60000" /> <property name= "UserName" value= "admin" /> <property name= "Password" value= "admin" /> <!--<property name= "Optimizeacknowledge" value= "true" /> --> <property Name= "Optimizedackscheduledackinterval" value= "10000" /> </bean> </property> </ bean> <!-- Spring JMS Template --> <bean id= "Jmstemplate" class= "Org.springframework.jms.core.JmsTemplate" > <property nAme= "ConnectionFactory" > <ref local= " Jmsconnectionfactory " /> </property> </bean> <!--Queue channel--> <bean id= "Asyncqueue" class= " Org.apache.activemq.command.ActiveMQQueue "> <constructor-arg index= "0" > <value>asyncQueue</value> </constructor-arg> </bean> <!--topic channel--> <bean id= "Asynctopic" name= " Asynctopic " class=" Org.apache.activemq.command.ActiveMQTopic "> &NBSP;&NBSP;&NBSP;&NBSp;<constructor-arg index= "0" > <value >asyncTopic</value> </constructor-arg> </bean > <!--Message acceptance container, multithreading asynchronously accepts messages--> < Bean id= "Jmscontainer" class= " Org.springframework.jms.listener.DefaultMessageListenerContainer "> < Property name= "ConnectionFactory" ref= "Jmsconnectionfactory" /> <property name= "Destination" ref= "Asynctopic" /> <property name= "MessageListener" ref= "MessageListener" /> <property name= "sessiontransacted" value= "false" /> </ bean> <!--Message accepts pojo--> <bean id= "Messagereceiver" class= " Com.cn.ld.modules.jms.worker.JmsReceiver " /> <!--message sent pojo--> <bean id= "Messagesender" class= " Com.cn.ld.modules.jms.worker.JmsSender " /> <!--message listeners received asynchronously--> <bean id= "MessageListener" class= "Org.springframework.jms.listener.adapter.MessageListenerAdapter" > <constructor-arg> <ref Bean= "Messagereceiver" /> </constructor-arg> </ bean>
3) Java-related class code
MessageHandler the interface that the message accepts
Java code package Com.cn.ld.modules.jms.handler; Import java.io.Serializable; public interface MessageHandler {void receive (TextMessage message); void Handlemessage (String message); void Handlemessage (map<string, object> message); void Handlemessage (byte[] message); void Handlemessage (Serializable message); }
Jmsreceiver Message Acceptance Implementation class
Java code package com.cn.ld.modules.jms.worker; Import java.io.Serializable; public class jmsreceiver implements messagehandler { private collection<string> collection ; @Override public void receive (textmessage message) { try { if (Collection == null) { this.collection = new ArrayList<String> (); } & nbsp; collection.add (Message.gettext ()); } catch (jmsexception e) { e.printstacktrace (); } } @Override public void handlemessage (string message) { /* * if (collection == NULL) { this.collection = new arraylist<string> (); } * collection.add (message); */ } @Override public void handlemessage (map<string, object> message) { Set<String> keySet = Message.keyset (); iterator<string> keys = keyset.iterator (); while ( Keys.hasnext ()) { String key = keys.next (); system.out.println (Message.get (key)); } } @ override &nbsP; public void handlemessage (byte[] message) { } @Override public void handlemessage (serializable message) { } public collection<string> getcollection () { return collection; } public void setcollection ( collection<string> collection) { this.collection = collection; } }
Jmssender message sending supports asynchronous send
Java code package com.cn.ld.modules.jms.worker; import java.util.collection; import javax.jms.destination; import javax.jms.jmsexception; import javax.jms.message; import javax.jms.Session; import org.apache.activemq.command.activemqtopic; import org.springframework.beans.factory.annotation.autowired; import org.springframework.core.task.taskexecutor; import org.springframework.jms.core.jmstemplate; import org.springframework.jms.core.messagecreator; import org.springframework.util.assert; import com.cn.ld.modules.annotation.methodmonitorcount; public class jmssender { @Autowired &NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP;JMstemplate jmstemplate; @Autowired private TaskExecutor taskExecutor; private Destination destination; private boolean isSendAsync = false; public jmssender () {} public jmssender ( destination destination) { if (null == destination) This.destination = new activemqtopic ("topic"); else This.destination = destination; } public void sendsingle (string message,destination Destination) { sendmessage (message,destination ); } public void Sendbatch (collection<?> messages,destination destination) { assert.notnull (messages, "param ' Messages ' can ' t be Null ! "); assert.notempty (messages, "param " Message ' can ' t be empty ! '); for (object message : messages ) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; if (null != message && message instanceof string) { sendsingle (string.valueof (message), destination); } } } private void SendMessage (final string message,destination destination) { final Destination sendDest = destination ; if (issendasync) { taskexecutor.execute (new runnable () { &NBSP;&NBSP;&NBSp; @Override public void run () { send (message,senddest); } }); } else { send ( Message,destination);