Turn: spring boot + ActiveMQ to implement message service

First of all, I found a good demo on github, here is for everyone to see:

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-activemq

 

It is indeed possible to achieve the function, but when I query the queue on the default admin port of 8161, I find that there is no github-queue. Although the specific reason is not clear, the solution has been found. Here is my own implementation:

pox.xml :

Java code   Favorite code
  1. <!-- ActiveMQ -->  
  2.         <dependency>  
  3.             <groupId>org.springframework</groupId>  
  4.             <artifactId>spring-jms</artifactId>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.apache.activemq</groupId>  
  8.             <artifactId>activemq-all</artifactId>  
  9.             <version>5.13.2</version>  
  10.         </dependency>  

application.properties:

Java code   Favorite code
  1. spring.activemq.in-memory=true  
  2. spring.activemq.pooled=false  

 

 

Next is the configuration of jms, the first is the ActiveMQ4Config file:

Java code   Favorite code
  1. @EnableJms  
  2. @Configuration  
  3. public class ActiveMQ4Config {  
  4.   
  5.     @Bean  
  6.     public Queue queue() {  
  7.         return new ActiveMQQueue("github-queue");  
  8.     }  
  9.   
  10.     @Bean  
  11.     public ActiveMQConnectionFactory activeMQConnectionFactory (){  
  12.         ActiveMQConnectionFactory activeMQConnectionFactory =  
  13.                 new ActiveMQConnectionFactory(  
  14.                         ActiveMQConnectionFactory.DEFAULT_USER,  
  15.                         ActiveMQConnectionFactory.DEFAULT_PASSWORD,  
  16. //                        "tcp://192.168.0.100:61616");  
  17.                         ActiveMQConnectionFactory.DEFAULT_BROKER_URL);  
  18.         return activeMQConnectionFactory;  
  19.     }  
  20.   
  21. }  

注释掉的那行,可以用来指定activemq的broker地址。

 

接下来的Producer和Consumer与github上一样:

Java代码   Favorite code
  1. @Component  
  2. public class Producer implements CommandLineRunner{  
  3.   
  4.     @Autowired  
  5.     private JmsMessagingTemplate jmsMessagingTemplate;  
  6.   
  7.     @Autowired  
  8.     private Queue queue;  
  9.   
  10.     @Override  
  11.     public void run(String... args) throws Exception {  
  12.         send("this message is send on begining!");  
  13.         System.out.println("Message was sent to the Queue");  
  14.     }  
  15.   
  16.     public void send(String msg) {  
  17.         this.jmsMessagingTemplate.convertAndSend(this.queue, msg);  
  18.     }  
  19.   
  20. }  
Java代码   Favorite code
  1. @Component  
  2. public class Consumer {  
  3.   
  4.     @JmsListener(destination = "github-queue")  
  5.     public void receiveQueue(String text) {  
  6.         System.out.println(text);  
  7.     }  
  8.   
  9. }  

 

This completes the configuration, and when the 8161 default admin queries, we can query our github-queue queue.

The specific test can be carried out by yourself, and the test case is no longer posted here.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038627&siteId=291194637