spring integration actionMQ (a)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zajin/article/details/91821546

Some time ago rabbitMQ configured primarily for companies to send bulk e-mail and services, the results of koi said to use actionMQ, what else to say, certainly played a actionMq go.

In view of the first configuration, use ActionMQ, so the detailed configuration record. If a more recent usage, or with detailed, more in-depth use, I will update from time to time,

Small partners and look forward to progress together.

Add rely on MAVEN

<!--添加activemq的依赖-->
<!--ActiveMQ-->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.14.5</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>5.14.5</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
    <version>5.14.5</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>artemis-jms-client</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

  Some here may depend on a small partner

<dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-all</artifactId>
 <version>5.9.0</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
 

After all, a lot of the top of the above, and can also be the problem when caught off guard. slf4j conflict. Most programs will have a slf4j, if you are being given, obediently with a lot of the above. Or you can consider Maven packet collisions are excluded from the program. Specifically how exclusion can be a Baidu.

 

Profiles (for me personally, the first step is to get the configuration file, go to class or something written in during configuration)

 

Press Ctrl + C to copy the code

 

Press Ctrl + C to copy the code

  The first step is certainly linked to the configuration of the MQ service. (ActionMQ link farms, there are many attributes that can be added, such as username, password ...)

  Step Two: Configure the connection pool provides us with the spring jms

  Step Three: Configure queue (queue is my pattern, that is, 1-on-1 consumption mode (a message once consumption), there is a publish-subscribe model (a message can be subscribed to multiple clients consumption))

  Step Four: Configure Templates

  Step five: declaration injection Post Owner (this is not necessary, normal classes will be available, in which the introduction of @Autowired private JmsTemplate jmsTemplate; template class, you can send a message)

Sending a message class

Copy the code

 1 package mq;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 import org.springframework.jms.core.JmsTemplate;
 4 import org.springframework.jms.core.MessageCreator;
 5 import javax.annotation.Resource;
 6 import javax.jms.*;
 7 
 8 /**
 9  * @ Author     :tian
10  * @ Date       :Created in 下午 2:21 2019/6/13 0015
11  * @ Description:生产者的实现类
12  */
13 public class ProduceServiceImpl{
14     @Autowired
15     private JmsTemplate jmsTemplate;
16     @Resource(name = "queueDestination")
17     private Destination destination;
18 
19     /**
20      * 发送消息
21      * @param msg
22      */
23     public void sendMessage(final String msg) {
24         jmsTemplate.convertAndSend("SpringActiveMQMsg",msg);
25 //        jmsTemplate.send(destination , new MessageCreator() {
26 //            @Override
27 //            public Message createMessage(Session session) throws JMSException {
28 //                TextMessage textMessage = session.createTextMessage(msg);
29 //                return textMessage;
30 //            }
31 //        });
32         System.out.println("现在发送的消息为: " + msg);
33     }
34 
35     /**
36      * 接收消息
37      */
38     public TextMessage receive() {
TextMessage (TM) = 39 (TextMessage) jmsTemplate.receive ( "SpringActiveMQMsg"); 
40 {the try 
41 is System.out.println ( "message received from the queue SpringActiveMQMsg: \ T" 
42 is tm.getText + ()); 
43 is the catch} ( E a JMSException) { 
44 is e.printStackTrace (); 
45} 
46 is 
47 (TM) return; 
48 
49} 
50}

Copy the code

Can send messages using the above two ways (not just two, but the two easiest.)

Mode 1:

jmsTemplate.convertAndSend ( "SpringActiveMQMsg", msg) // a parameter: the name of the queue you declare (the queue can not declare directly to the ActionMQ console can also be created to simplify configuration, only time will have to want to change the server Creating a parameter two): message sent

Option 2:

The above procedure of the comment

 

        jmsTemplate.send (Where do you want, the MessageCreator new new () { 
            @Override 
            public the createMessage the Message (the session the Session) throws a JMSException { 
                TextMessage textMessage = session.createTextMessage (MSG); 
                return textMessage; 
            } 
        }); 
it is the correct message can be sent to enlarge the transmission method has two parameters (one parameter: the parameter declaration beanId two queues: a message sent by the creator) 
can be seen here TextMessage a class, this is a type of message, actionMQ message in a variety of type JMS specification message types TextMessage, MapMessage, ObjectMessage, BytesMessage, and other five StreamMessage. There are also corresponding implementation ActiveMQ

By the way tell us about the message of the manual reception TextMessage tm = (TextMessage) jmsTemplate.receive ( "SpringActiveMQMsg"); use receive jmsTemplate template ( "queue name"), a consumer may be a message. (This is certainly more than one way, the specific little friends can explore on their own)

Consumption message type corresponding to the type of transmission. If you want to automatically receive news consumption, it would have to configure a message listener.

Guess you like

Origin blog.csdn.net/zajin/article/details/91821546