【ActiveMQ】2.spring Boot下使用ActiveMQ

时间:2021-05-31 15:01:09

在spring boot下使用ActiveMQ,需要一下几个条件

  1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.html

  2.搭建了spring boot项目,参考:http://www.cnblogs.com/sxdcgaq8080/p/7712874.html

======================================================================================================================

好了下面正式开始吧:

  1.pom.xml文件添加maven依赖

    <!--ActiveMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

  2.application.properties配置文件配置

#activeMQ
#61616为消息代理接口 ,8161 为管理界面
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false

  3.消息生产者Producer.java

package com.sxd.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import javax.jms.Destination; @Service("producer1")
public class Producer1 { @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息 public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
} }

  4.消息消费者Consumer.java

package com.sxd.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.TextMessage; @Component(value = "consumer1")
public class Consumer1 { @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsTemplate; public String receive(String queueName){
GenericMessage textMessage = (GenericMessage)jmsTemplate.receive(queueName);
try {
System.out.println("消费者1收到消息为:"+textMessage.toString());
return textMessage.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

  5.控制层调用Controller【localhost:8080/log消息生产】【localhost:8080/log2消息消费】

package com.sxd.controller;

import com.sxd.jms.Consumer1;
import com.sxd.jms.Producer1;
import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource;
import javax.jms.Destination; @Controller
public class MainController { @Resource(name = "producer1")
private Producer1 producer1; @Resource(name = "consumer1")
private Consumer1 consumer1; @RequestMapping("/toLogin")
public String hello(){
return "login";
} Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/log")
@ResponseBody
public void logTest(){
Destination destination = new ActiveMQQueue("jms.queue");
producer1.sendMessage(destination,"您有一条新消息,请注意查收!");
} @RequestMapping("/log2")
@ResponseBody
public void log2Test(){
String queueName = "jms.queue";
consumer1.receive(queueName);
}
}

=============================================================================================================================

上面已经可以使用了,但是消息队列中对于消息的消费,如果能实现自动监听,自动处理那就更好了,给【消息消费者Consumer】修改一下

Consumer.java

package com.sxd.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class Consumer1 { @JmsListener(destination = "jms.queue")
public void receive(String msg){
System.out.println(msg);
}
}

消息生产者依旧 不变,然后Controller.java往队列中存放消息即可。

@RequestMapping("/log")
@ResponseBody
public void logTest(){
Destination destination = new ActiveMQQueue("jms.queue");
producer1.sendMessage(destination,"您有一条新消息,请注意查收!");
}

============================================================================================================================

最后,如果想要实现双向消息传输呢?

什么意思呢,就是【消息消费者Consumer】处理完消息之后,可以带回给【消息生产者Producer 】一些有用的信息.

这样的话,Consumer.java就该这么写:

这样在监听到jms.queue消息队列中的消息之后,处理完成,将返回的消息放入reback.queue消息队列中

package com.sxd.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component; @Component
public class Consumer1 { @JmsListener(destination = "jms.queue")
@SendTo("reback.queue")
public String receive(String msg){
System.out.println(msg);
return "已经接收到消息,且处理完成!!";
}
}

而Producer.java应该再多加一个监听,监听返回消息队列中的消息:

package com.sxd.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import javax.jms.Destination; @Service("producer1")
public class Producer1 { @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息 public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
} @JmsListener(destination = "reback.queue")
public void receiveConsumer(String msg){
System.out.println(msg); }
}

最后Controller.java中访问测试一下:

 @RequestMapping("/log")
@ResponseBody
public void logTest(){
Destination destination = new ActiveMQQueue("jms.queue");
producer1.sendMessage(destination,"您有一条新消息,请注意查收!");
}

【ActiveMQ】2.spring Boot下使用ActiveMQ

====================================================================================

结束!!!