Springboot and ActiveMQ integration, springbootactivemq
Preface
Many projects are not completed by a system. Instead, many systems work together to complete functions. Then, it is impossible for systems to be completely independent of each other?
For example, the management system used by the school includes the student system, the asset system, and the dormitory system. after the end of the semester, do you need to archive the completed period. if the archiving function is in the student system, after you click archive, Do students have to worry about whether the dormitory is over or whether all the assets received by the students are returned?
Obviously, this is not a good method. The coupling between systems is too strong, which is not conducive to system expansion. Moreover, one-step operation may take a long time to complete. can users wait?
Since synchronous archiving is impossible, is there a way to Implement Asynchronous archiving? How to Implement Asynchronous archiving?
In fact, we can implement asynchronous archiving through message queues. Students click archive and send a message to the queue. Other systems read the message themselves and then complete the work that their systems should do.
Download and install ActiveMQ
: Http://activemq.apache.org/download.html
The installation process is relatively simple. In centos, extract the package, even if it is installed.
Running method:
After running, you can use ip: 8161 to check whether the operation is successful.
Click the link in the red box. The logon dialog box is displayed. The account and password are admin by default.
Integrate activemq with springboot
I. directory structure
Producer: Message producer
Consumer-a: Message consumer
Consumer-B: Message consumer
Pom file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId></dependency>
If you use a pool, you need to add the following dependencies to the pom:
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.14.5</version></dependency>
Ii. producer
1. directory structure
2. yml file:
server: port: 8080 context-path: /prospring: activemq: user: admin password: admin broker-url: tcp://192.168.153.129:61616 pool: enabled: true max-connections: 10queueName: publish.queuetopicName: publish.topic
The connection pool is enabled, Which is disabled by default.
Note that the port is not the 8161 port.
2. The configuration file ActiveMQConfig
/*** @ Author: elvin */@ Configurationpublic class ActiveMQConfig {@ Value ("$ {queueName}") private String queueName; @ Value ("$ {topicName }") private String topicName; @ Value ("$ {spring. activemq. user} ") private String usrName; @ Value (" $ {spring. activemq. password} ") private String password; @ Value (" $ {spring. activemq. broker-url} ") private String brokerUrl; @ Bean public Queue queue () {return new ActiveMQQu Eue (queueName) ;}@ Bean public Topic topic () {return new ActiveMQTopic (topicName) ;}@ Bean public ActiveMQConnectionFactory connectionFactory () {return new ActiveMQConnectionFactory (usrName, password, brokerUrl);} @ Bean public JmsListenerContainerFactory <?> JmsListenerContainerQueue (ActiveMQConnectionFactory connectionFactory) {DefaultJmsListenerContainerFactory bean = new connector (); bean. setConnectionFactory (connectionFactory); return bean ;}@ Bean public JmsListenerContainerFactory <?> JmsListenerContainerTopic (ActiveMQConnectionFactory connectionFactory) {DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory ();
// Set to publish/subscribe mode. By default, the production consumer mode bean. setPubSubDomain (true); bean. setConnectionFactory (connectionFactory); return bean;} is used ;}}
There is no need to configure so much here, but it will also be used in the consumer, so we will get the same copy for the moment and copy it to the end.
3. PublishController
/*** @ Author: elvin */@ RestController @ RequestMapping ("/publish") public class PublishController {@ Autowired private JmsMessagingTemplate jms; @ Autowired private Queue queue; @ Autowired private Topic topic; @ RequestMapping ("/queue") public String queue () {for (int I = 0; I <10; I ++) {jms. convertAndSend (queue, "queue" + I);} return "queue sent successfully" ;}@ JmsListener (destination = "out. queue ") public void consumerMsg (String msg) {System. out. println (msg) ;}@ RequestMapping ("/topic") public String topic () {for (int I = 0; I <10; I ++) {jms. convertAndSend (topic, "topic" + I);} return "topic sent successfully ";}}
Iii. consumer
1. directory structure
A and B are the same, but different information is displayed.
2. Configuration File
The yml configuration file is the same, but the port and context-path are modified.
The content of the ActiveMQConfig file is the same.
3. listener
/*** @ Author: elvin */@ Componentpublic class QueueListener {@ JmsListener (destination = "publish. queue ", containerFactory =" jmsListenerContainerQueue ") @ SendTo (" out. queue ") public String receive (String text) {System. out. println ("QueueListener: consumer-a receives a message:" + text); return "consumer-a received:" + text ;}}
SendTo writes the data returned by this method to queue: out. queue.
/*** @ Author: elvin */@ Componentpublic class TopicListener {@ JmsListener (destination = "publish. topic ", containerFactory =" jmsListenerContainerTopic ") public void receive (String text) {System. out. println ("TopicListener: consumer-a receives a message:" + text );}}
In this example, different factory types are passed in to send different types of information.
Iv. Test
1. queue Test
Access in the browser: http: // localhost: 8080/pro/publish/queue
Then let's take a look at the information that the users in the console receive.
As shown in the above two figures, a and B cannot receive data at the same time. This is the queue method, point-to-point.
What should I do if I want to click the opposite?
2. topic Test
Browser access page: http: // localhost: 8080/pro/publish/topic
User a has completely received the information. Let's look at user B.
No problem, and the data is also received.
By default, a topic does not store data. That is to say, a consumer cannot receive information that has not been received before.
While queue is OK.
However, the topic does not support that function. You only need to configure it.