First of all, I found a good demo on github, here is for everyone to see:
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 :
- <!-- ActiveMQ -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jms</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-all</artifactId>
- <version>5.13.2</version>
- </dependency>
application.properties:
- spring.activemq.in-memory=true
- spring.activemq.pooled=false
Next is the configuration of jms, the first is the ActiveMQ4Config file:
- @EnableJms
- @Configuration
- public class ActiveMQ4Config {
- @Bean
- public Queue queue() {
- return new ActiveMQQueue("github-queue");
- }
- @Bean
- public ActiveMQConnectionFactory activeMQConnectionFactory (){
- ActiveMQConnectionFactory activeMQConnectionFactory =
- new ActiveMQConnectionFactory(
- ActiveMQConnectionFactory.DEFAULT_USER,
- ActiveMQConnectionFactory.DEFAULT_PASSWORD,
- // "tcp://192.168.0.100:61616");
- ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
- return activeMQConnectionFactory;
- }
- }
注释掉的那行,可以用来指定activemq的broker地址。
接下来的Producer和Consumer与github上一样:
- @Component
- public class Producer implements CommandLineRunner{
- @Autowired
- private JmsMessagingTemplate jmsMessagingTemplate;
- @Autowired
- private Queue queue;
- @Override
- public void run(String... args) throws Exception {
- send("this message is send on begining!");
- System.out.println("Message was sent to the Queue");
- }
- public void send(String msg) {
- this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
- }
- }
- @Component
- public class Consumer {
- @JmsListener(destination = "github-queue")
- public void receiveQueue(String text) {
- System.out.println(text);
- }
- }
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.