1 依赖导入和配置
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
application.yml 配置文件中,对 activemq 做一下配置:
spring:
activemq:
# activemq url
broker-url: tcp://localhost:61616
in-memory: true
pool:
# 如果此处设置为true,需要添加activemq-pool的依赖包,否则会自动配置失败,无法注入JmsMessagingTemplate
enabled: false
2 Queue 和 Topic 的创建
创建 Queue 和 Topic 两种消息,分别使用 new ActiveMQQueue
和 new ActiveMQTopic
来创建
/**
* activemq的配置
*/
@Configuration
public class ActiveMqConfig {
/**
* 发布/订阅模式队列名称
*/
public static final String TOPIC_NAME = "activemq.topic";
/**
* 点对点模式队列名称
*/
public static final String QUEUE_NAME = "activemq.queue";
@Bean
public Destination topic() {
return new ActiveMQTopic(TOPIC_NAME);
}
@Bean
public Destination queue() {
return new ActiveMQQueue(QUEUE_NAME);
}
}
3 消息的发送接口
/**
* 消息发送者
*/
@Service
public class MsgProducer {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(Destination destination, String msg) {
//convertAndSend方法中第一个参数是消息发送的目的地,第二个参数是具体的消息内容。
jmsMessagingTemplate.convertAndSend(destination, msg);
}
}
4 点对点消息生产与消费
4.1 点对点消息的生产
**
* ActiveMQ controller
*/
@RestController
@RequestMapping("/activemq")
public class ActiveMqController {
private static final Logger logger = LoggerFactory.getLogger(ActiveMqController.class);
@Resource
private MsgProducer producer;
@Resource
private Destination queue;
@GetMapping("/send/queue")
public String sendQueueMessage() {
logger.info("===开始发送点对点消息===");
producer.sendMessage(queue, "Queue: hello activemq!");
return "success";
}
}
4.2 点对点消息的消费
@JmsListener`注解来指定要监听的目的地,在消息接收方法内部,我们可以根据具体的业务需求做相应的逻辑处理就可以。
/**
* 消息消费者
*/
@Service
public class QueueConsumer {
/**
* 接收点对点消息
* @param msg
*/
@JmsListener(destination = ActiveMqConfig.QUEUE_NAME)
public void receiveQueueMsg(String msg) {
System.out.println("收到的消息为:" + msg);
}
}
5 发布/订阅消息的生产和消费
5.1 发布/订阅消息的生产
@RestController
@RequestMapping("/activemq")
public class ActiveMqController {
private static final Logger logger = LoggerFactory.getLogger(ActiveMqController.class);
@Resource
private MsgProducer producer;
@Resource
private Destination topic;
@GetMapping("/send/topic")
public String sendTopicMessage() {
logger.info("===开始发送订阅消息===");
producer.sendMessage(topic, "Topic: hello activemq!");
return "success";
}
}
5.2 发布/订阅消息的消费
需要在配置文件 application.yml 中添加一个配置:
spring:
jms:
pub-sub-domain: true
ActiveMqConfig 配置类中添加:
/**
* activemq的配置
*/
@Configuration
public class ActiveMqConfig {
// 省略其他内容
/**
* JmsListener注解默认只接收queue消息,如果要接收topic消息,需要设置containerFactory
*/
@Bean
public JmsListenerContainerFactory topicListenerContainer(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
// 相当于在application.yml中配置:spring.jms.pub-sub-domain=true
factory.setPubSubDomain(true);
return factory;
}
}
@JmsListener 注解中指定这个容器工厂即可消费 topic 消息
/**
* Topic消息消费者
*/
@Service
public class TopicConsumer1 {
/**
* 接收订阅消息
* @param msg
*/
@JmsListener(destination = ActiveMqConfig.TOPIC_NAME, containerFactory = "topicListenerContainer")
public void receiveTopicMsg(String msg) {
System.out.println("收到的消息为:" + msg);
}
}