如何下载安装MQ我就不说了,百度一大把,老规矩先上一下项目目录结构:
先看一下配置文件,主要是中间件的配置:
ps:1.主要注意的是activeMQ默认提供ptp模式,若要使用topic模式需要假如最后一个配置为true
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=true
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true
看一下pom用到哪些包吧,其实除了spring boot基本的两个包以外在引入两个activemq的依赖就行了:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
然后就是像spring容器声明配置类:
ps:
1.其实学习一段时间spring boot有点小心得了,当我们使用外部插件时需要自己声明并且写配置类,加@Bean注解来为我们需要提供服务的类进行注入,以往传统的springMVC就是在配置文件配置bean,指定class,properties之类的属性等等
2.ptp和topic两种消息模式我也不赘述了,很好理解,可以理解为qq的私聊和群聊的关系
@Configuration
@EnableJms
public class MsgListeners {
/**
* 点对点
* @return
*/
@Bean
public Queue queue(){
return new ActiveMQQueue("zh-queue");
}
/**
* 发布/订阅
* @return
*/
@Bean
public Topic topic(){
return new ActiveMQTopic("zh-topic");
}
}
接下来是controller层接受请求
@RestController
@RequestMapping("/activeMq")
public class ActiveMqController {
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Autowired
private ProductService productService;
@GetMapping("/queue/{msg}")
public void sendQueue(@PathVariable("msg") String msg){
productService.sendMessage(this.queue,msg);
}
@GetMapping("/topic/{msg}")
public void sendTopic(@PathVariable("msg")String msg){
productService.sendMessage(this.topic,msg);
}
}
然后是生产者service接口和其实现类:
ps:1.第一个方法实现了接口,把接收到的消息和消息模式放到了队列里或者主题里就是queue或topic里,然后只要写消费者加上@JmsListener监听队列消息就可以自动获取
2.这里的第二个方法加了监听注解就可以收到消费者反馈的信息,前提消费者要加@SendTo注解,具体看下面消费者类
public interface ProductService {
void sendMessage(Destination destination,String message);
}
@Service
public class ProductServiceImpl implements ProductService{
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Override
public void sendMessage(Destination destination, String message) {
jmsMessagingTemplate.convertAndSend(destination,message);
}
@JmsListener(destination = "return-queue")
public void Message(String message){
System.out.println("Product收到:"+message);
}
}
消费者类,2个
@Service
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "zh-topic")
public void receiveQueue(String text) {
System.out.println("Consumer收到:"+text);
}
}
@Service
public class Consumer2 {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "zh-topic")
@SendTo("return-queue")
public String receiveQueue(String text) {
System.out.println("Consumer2收到:"+text);
return "Consumer2收到!";
}
}
然后使用浏览器或者postman测试一下就好啦,至于如何同时支持ptp模式和topic我还没有办法呢,不知道你有没有呢?
对了,MQ还支持对象,Map,流,字节等传输我还没试过,以后在做记录吧