结构图

第一步:导入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

  

第二步:创建application.yml配置文件

//消费者的yml
server:
port: 8081
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
//在使用发布订阅的时候的开
#jms:
#pub-sub-domain: true //生产者的yml
server:
port: 8080
#activemq配置
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin

  

第三步: 创建生产者,通过JMSTemplate模板发送消息

//队列

package com.wish.producer.producermethod;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component
public class QueueProducer {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage() {
//点对点,创建队列
ActiveMQQueue queue = new ActiveMQQueue("boot_queue");
//发送消息
jmsTemplate.convertAndSend(queue, "生产者产生的消息~");
jmsTemplate.setDeliveryMode(2);
jmsTemplate.setExplicitQosEnabled(true);
jmsTemplate.setDeliveryPersistent(true);
}
} //发布订阅
package com.wish.producer.producermethod; import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource; @Component
public class TopicProducer {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage(){
//发布订阅:创建主题
ActiveMQTopic topic=new ActiveMQTopic("boot_topic");
//springboot默认是queue
jmsTemplate.setPubSubDomain(true);
//发送消息
jmsTemplate.convertAndSend(topic,"生产者产生主题的消息~"); }
}

  

第四步:创建客户端访问的方法

//队列
package com.wish.producer.controller; import com.wish.producer.producermethod.QueueProducer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class QueueController {
@Resource
private QueueProducer queueProducer; @RequestMapping("/sendQueueMessage")
public String sendMessage(){
queueProducer.sendMessage();
return "QueueSuccess";
}
} //发布订阅
package com.wish.producer.controller; import com.wish.producer.producermethod.TopicProducer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource;
@RestController
public class TopicController {
@Resource
private TopicProducer topicProducer; @RequestMapping("/sendTopicMessage")
public String sendMessage(){
topicProducer.sendMessage();
return "TopicSuccess";
}
}

  

第五步:创建消费者

 //队列
@JmsListener(destination = "boot_queue")
public void getMessage(TextMessage message) throws JMSException {
System.out.println("消费者获取到消息:"+message.getText());
} //发布订阅
@Bean
public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
//这里必须设置为true,false则表示是queue类型
factory.setPubSubDomain(true);
return factory;
} //消费者消费 destination队列或者主题的名字
@JmsListener(destination = "boot_topic",containerFactory = "jmsTopicListenerContainerFactory")
public void getMessage(TextMessage message) throws JMSException {
System.out.println("消费者获取到消息:"+message.getText());
}

  

最新文章

  1. Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]
  2. 【JSP】JSP基础学习记录(一)—— 基础介绍以及3个编译指令
  3. CSU 1328: 近似回文词
  4. 1.JS设计模式-this,call&amp;apply
  5. Spring IoC原理详解
  6. 62. Divide Two Integers
  7. 3D全景!这么牛!!
  8. EF Code First教程-02 约定配置
  9. treap 1286郁闷的出纳员.cpp
  10. 同一台电脑启动两个或多个tomcat
  11. Server.MapPath() 解析
  12. Keil C51里关于堆栈指针的处理
  13. Lucene全文搜索 分组,精确查找,模糊查找
  14. Jdk1.6 JUC源码解析(13)-LinkedBlockingQueue
  15. makefile在编译的过程中出现“except class name”
  16. 天猫精灵X1智能音箱使用感想
  17. 【吐槽向】iOS 中的仿射变换
  18. [蓝桥杯]PREV-27.历届试题_蚂蚁感冒
  19. Go Web:数据存储(2)——CSV文件
  20. OSI的七层模型介绍

热门文章

  1. SpringBoot + Mybatis 和ssm 使用数据库的区别
  2. 令牌桶算法实现API限流
  3. C++ 自动类型推断
  4. java核心技术----接口
  5. Java中的代码点与代码单元
  6. 浅谈C语言的数据存储(一)
  7. vue-cli 脚手架
  8. 接口测试:http状态码
  9. numpy 介绍与使用
  10. 面试官:你说你熟悉jvm?那你讲一下并发的可达性分析