activeMQ with Spring Integration

Original link: http://www.cnblogs.com/bkxyq/p/9548678.html

All activemq port injection are used by the default port (without modification)

activeMQ and Spring integration dependent jar

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.9.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.0</version>
        </dependency>

Message transmission side configuration

Plus the following configuration profile in the spring

 1 <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
 2           destroy-method="stop">
 3         <property name="connectionFactory">
 4             <bean class="org.apache.activemq.ActiveMQConnectionFactory">
 5                 <property name="brokerURL">
 6                     <value>tcp://localhost:61616</value>
 7                 </property>
 8             </bean>
 9         </property>
10         <property name="maxConnections" value="100"></property>
11     </bean>
12 
13     <!--使用缓存可以提升效率-->
14     <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
15         <property name="targetConnectionFactory" ref="jmsFactory"/>
16         <property name="sessionCacheSize" value="1"/>
17     </bean>
18 
19     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
20         <property name="connectionFactory" ref="cachingConnectionFactory"/>
21         <property name="messageConverter">
22             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
23         </property>
24     </bean>
25 
26     <!--测试Queue,队列的名字是spring-queue-->
27     <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
28         <!--<constructor-arg index="0" value="spring-queue"/>-->
29         <constructor-arg name="name" value="spring-queue"/>
30     </bean>
31 
32     <!--测试Topic-->
33     <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
34         <constructor-arg index="0" value="spring-topic"/>
35     </bean>
Configuration message sending end point

 

The sending end point

 1 import javax.annotation.Resource;
 2 import javax.jms.Destination;
 3 import javax.jms.JMSException;
 4 import javax.jms.Message;
 5 import javax.jms.Session;
 6 
 7 import org.slf4j.Logger;
 8 import org.slf4j.LoggerFactory;
 9 import org.springframework.jms.core.JmsTemplate;
10 import org.springframework.jms.core.MessageCreator;
11 import org.springframework.stereotype.Service;
12 
13 importcom.alibaba.fastjson.JSON;
 14  Import com.spr.plat.modules.jysb.retentionaward.entity.RetentionAwardUser;
 15  
16  @Service
 . 17  public  class AMQSenderServiceImpl {
 18 is      Private  static  Final Logger Logger = LoggerFactory.getLogger (AMQSenderServiceImpl. class ) ;
 . 19  
20 is       @Resource (name = "JmsTemplate" )
 21 is      Private the JmsTemplate JmsTemplate;
 22 is  
23 is      // destination queue proof, we would like to send a message queue 
24      @Resource (name = "DestinationQueue" )
 25      PrivateWhere do you want the Destination;
 26 is  
27      // to send a specific message queue (RetentionAwardUser custom java object) 
28      public  void SENDMSG (RetentionAwardUser v_RAU) {
 29          String MSG = JSON.toJSONString (v_RAU);
 30          the try {
 31 is              logger.info ( "will be } to {queue message transmitted MSG: {} " , Where do you want, MSG); 
 32              jmsTemplate.send (Where do you want, new new the MessageCreator () {
 33 is                  @Override
 34 is                  public the message the createMessage (the session the Session) throws a JMSException {
 35                      returnsession.createTextMessage (MSG);
 36                  }
 37 [              });
 38 is          } the catch (Exception EX) {
 39              ex.printStackTrace ();
 40              logger.error ( "} {Send a message to the queue fails, the message is: {}" , Where do you want , MSG);
 41 is          }
 42 is      }
 43 is }
Transmitting side code point

Configuration message receiving end

1 <bean id="jmsContainer"
2           class="org.springframework.jms.listener.DefaultMessageListenerContainer">
3         <property name="connectionFactory" ref="cachingConnectionFactory"/>
4         <property name="destination" ref="destinationQueue"/>
5         <property name="messageListener" ref="messageListener"/>
6 </bean>
Configuration message receiving end

Receiving end point

 1 import javax.jms.JMSException;
 2 import javax.jms.Message;
 3 import javax.jms.MessageListener;
 4 import javax.jms.TextMessage;
 5 
 6 
 7 /**
 8  * MQ 消息监听器
 9  * 
10  * @author     *****
11  * 
12  * @date    2018-8-28 14:08:30
13  *
14  */
15 public class MQMessageListener implements MessageListener{
16 
17     
18     /**
19      * Message Listener
 20 is       * @author     *****
 21 is       * @date 2018-8-28 14:13:00
 22 is       * @version     1.0
 23 is       * / 
24      @Override
 25      public  void the onMessage (the Message MSG) {
 26 is          IF ( MSG the instanceof TextMessage) {
 27              the try {
 28                  String message = ((TextMessage) MSG) .getText ();
 29                  System.err.println "the MQ message \ r \ n received:" ( );
 30                  System.err.println (Message);
 31 is             } catch (JMSException e) {
32                 e.printStackTrace();
33             }
34         } else {
35             throw new IllegalArgumentException("Message must be of type TextMessage");
36         }
37     }
38 
39 }
Receiving end point

Finally, add the following configuration in the spring configuration file

 <bean id="messageListener" class="com.spr.plat.modules.jysb.retentionaward.service.MQMessageListener"></bean>

 

Reproduced in: https: //www.cnblogs.com/bkxyq/p/9548678.html

Guess you like

Origin blog.csdn.net/weixin_30256901/article/details/94789602