- 添加pom依赖
<!-- ActiveMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- ActiveMQ线程池 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
- 添加application.properties相关配置
#整合ActiveMQ
spring.activemq.broker-url=tcp://127.0.0.1:61616
#集群配置
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)
spring.activemq.user=admin
spring.activemq.password=admin
#ActiveMQ线程池配置
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100#开启发布订阅模式,默认点对点
#spring.jms.pub-sub-domain=true
- 修改Application类
package com.example.demo;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;@SpringBootApplication
@EnableJms //开启支持JMS
public class DemoApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
@Bean
public Queue queue(){
return new ActiveMQQueue("common.queue");
}
@Bean
public Topic topic(){
return new ActiveMQTopic("video.topic");
}}
- 消息发送或发布Service接口
package com.example.demo.service;
import javax.jms.Destination;
public interface ProducerService {
/**
* 功能描述: 发送消息到指定队列
* @param destination
* @param message
*/
public void sendMessage(Destination destination, final String message);
/**
* 功能描述: 发送消息到公共队列
* @param message
*/
public void sendMessage(final String message);
/**
* 功能描述: 发布消息
* @param message
*/
public void pushMessage(final String message);
}
- 消息发送或发布Service接口实现
package com.example.demo.service.impl;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;import com.example.demo.service.ProducerService;
@Service
public class ProducerServiceImpl implements ProducerService{@Autowired
private JmsMessagingTemplate jmsTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Override
public void sendMessage(Destination destination, final String message) {
jmsTemplate.convertAndSend(destination,message);
}@Override
public void sendMessage(String message) {
jmsTemplate.convertAndSend(queue,message);
}@Override
public void pushMessage(String message) {
jmsTemplate.convertAndSend(topic,message);
}
}
- 消费者1
package com.example.demo.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class TestConsumer {@JmsListener(destination="test.queue")
public void receiveQueue(String message){
System.out.println("TestConsumer接收报文:"+message);
}
}
- 消费者2
package com.example.demo.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class CommonConsumer {@JmsListener(destination="common.queue")
public void receiveQueue(String message){
System.out.println("CommonConsumer接收报文:"+message);
}
}
- 订阅者
package com.example.demo.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class TopicSub {@JmsListener(destination="video.topic",containerFactory="jmsListenerContainerTopic")
public void receive1(String message){
System.out.println("receive1接收报文:"+message);
}
@JmsListener(destination="video.topic",containerFactory="jmsListenerContainerTopic")
public void receive2(String message){
System.out.println("receive2接收报文:"+message);
}
@JmsListener(destination="video.topic",containerFactory="jmsListenerContainerTopic")
public void receive3(String message){
System.out.println("receive3接收报文:"+message);
}
}
- controller类
package com.example.demo.controller;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.example.demo.domain.JsonData;
import com.example.demo.service.ProducerService;@RestController
@RequestMapping("/activemq")
public class ActiveMQController {
@Autowired
private ProducerService messageProducerService;/**
* 功能描述: 发送信息到指定队列
* @param message
* @return
*/
@RequestMapping("/test")
public Object test(String message){
Destination destination = new ActiveMQQueue("test.queue");
messageProducerService.sendMessage(destination, message);
return JsonData.buildSuccess();
}
/**
* 功能描述: 发送信息到公用队列
* @param message
* @return
*/
@RequestMapping("/common")
public Object common(String message){
messageProducerService.sendMessage(message);
return JsonData.buildSuccess();
}
/**
* 功能描述: 发布消息
* @param message
* @return
*/
@RequestMapping("/topic")
public Object topic(String message){
messageProducerService.pushMessage(message);
return JsonData.buildSuccess();
}
}