ActiveQq的代码实现
]从java代码开始再过渡到springboot
Java代码的实现
1.activemq这个消息中间件有两种形式
1. p2p(生产者,消费者)
特点:

生产者:
package com.lqh; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue; import javax.jms.*; public class p2ppub {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.91.111:61616");
// 创建连接
Connection connection = factory.createConnection();
// 创建会话 第一个参数就是指是否开启session事务如果开启了,需要手动提交session.commit
// Session.AUTO_ACKNOWLEDGE 这个代表自动回执就是接收到消息后向对列应答成功
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建生产者(指明消息队列的名字)
Destination destination = new ActiveMQQueue("152-queue1");
MessageProducer producer = session.createProducer(destination);
// 创建消息 使用会话创建
TextMessage textMessage = session.createTextMessage("hello-152");
// 生产者发布消息
producer.send(textMessage);
// 提交
// session.commit();
// 关闭资源
connection.close();
session.close();
producer.close();
} }
消费者:
package com.lqh; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue; import javax.jms.*; public class p2psub {
// 消费者
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.91.111:61616");
// 创建连接
Connection connection = factory.createConnection();
// 启动连接
connection.start();
// 创建会话 是否开启事务 开启自动回执
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建消费者 指定消费者的队列名字
Destination destination = new ActiveMQQueue("152-queue1");
MessageConsumer consumer = session.createConsumer(destination); // 消费者获取到消息
TextMessage receive = (TextMessage) consumer.receive();
System.out.println(receive); // 事务提交
// session.commit();
// 资源释放
connection.close();
consumer.close();
session.close(); }
}
2. 发布与订阅



<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>

pub 发布
package com.lqh; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue; import javax.jms.*; public class p2ppub {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.91.111:61616");
// 创建连接
Connection connection = factory.createConnection();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建生产者(指明消息队列的名字)
Destination destination = new ActiveMQQueue("152-queue1");
MessageProducer producer = session.createProducer(destination);
// 创建消息 使用会话创建
TextMessage textMessage = session.createTextMessage("hello-152");
// 生产者发布消息
producer.send(textMessage);
// 提交
// session.commit();
// 关闭资源
connection.close();
session.close();
producer.close();
} }
sub 订阅
package com.lqh; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue; import javax.jms.*; public class p2psub {
// 消费者
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.91.111:61616");
// 创建连接
Connection connection = factory.createConnection();
// 启动连接
connection.start();
// 创建会话 是否开启事务 开启自动回执
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建消费者 指定消费者的队列名字
Destination destination = new ActiveMQQueue("152-queue1");
MessageConsumer consumer = session.createConsumer(destination); // 消费者获取到消息
TextMessage receive = (TextMessage) consumer.receive();
System.out.println(receive); // 事务提交
// session.commit();
// 资源释放
connection.close();
consumer.close();
session.close(); }
}
这个订阅与发布是需要先启动订阅者,再启动发布者这样消息才会发布出来然后程序才会截止。也就是pub/sub的特点

为了解决这个问题就采用了持久化 订阅者的持久化
package com.lqh; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic; import javax.jms.*; public class DureableSub {
public static void main(String[] args) throws JMSException {
// 创建一个连接工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.91.111:61616");
// 2从工厂获取一个连接
Connection connection = factory.createConnection();
connection.setClientID("clinet111"); //给连接起一个名字随便起 (为一个标识id)
connection.start();
// 3根据连接创建一个会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 4.根据会话去创建一个可持久化订阅
ActiveMQTopic destination=new ActiveMQTopic("151-Topic");
// 可持久化订阅 给当前消费者起一个id 这个id可以随便起
TopicSubscriber consumer = session.createDurableSubscriber((Topic) destination, "anyname");
// 5.接收消息。
TextMessage message = (TextMessage) consumer.receive();
String text = message.getText();
System.out.println(text); connection.close();
session.close();
consumer.close();
}
}
为了使持久化生效需要先启动一次订阅者,和发布者做一个连接告诉activemq我当前是一个可持久化的订阅者 ,以后将这个信息保存起来为持久化之前topic的信息是不保存的,有订阅者将这个信息推出去没有就将这个信息删除了
例如:没有持久化前 只是启动分布者信息发布后没有订阅者,就会将信息删除
持久化后,即使没有订阅者消息发布后会将消息保存起来当消费者一启动就会将信息发布回来。
p2p中消费者需要访问才能消费
解决方法---监听器
使用springboot集成
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
server.port=8989
spring.activemq.broker-url=tcp://192.168.91.111:61616
p2p 代码
package com.example.controller; import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage; @RestController
public class TestSubController {
@Autowired
JmsTemplate jmsTemplate; @RequestMapping("send")
public void sendMsg(){
// 指定消息队列的名字
ActiveMQQueue destination = new ActiveMQQueue("151-Topic");
jmsTemplate.convertAndSend(destination,"hello");
}
// 监听器当151-Topic消息队列发生了改变就会调用当前方法
@JmsListener(destination = "151-Topic")
public void receive(Message message) throws JMSException {
TextMessage textMessage= (TextMessage) message;
System.out.println(textMessage.getText());
}
}
发布和订阅
package com.example.controller; import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage; @RestController
public class TestPubSubController {
@Autowired
private JmsTemplate jmsTemplate; @RequestMapping("pub")
public String pub(String msg){
ActiveMQTopic destination = new ActiveMQTopic("boot-topic");
jmsTemplate.convertAndSend(destination,msg);
return "publish success";
} @RequestMapping("sub")
public String sub(){
ActiveMQTopic destination = new ActiveMQTopic("boot-topic");
Object o = jmsTemplate.receiveAndConvert(destination);
String s = o.toString();
return s;
}
@JmsListener(destination = "boot-topic",containerFactory = "defaultJmsListenerContainerFactory")
public void receive(Message message) throws JMSException {
TextMessage textMessage= (TextMessage) message;
System.out.println(textMessage.getText());
}
}
因为springboot集成了activemq但是并没有实现持久化所以需要自定义持久化
package com.example.conf; import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory; @Configuration
public class KeepSubConf {
@Bean
public DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory(){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory());
factory.setClientId("client01");
factory.setSubscriptionDurable(true);
return factory;
} public ActiveMQConnectionFactory activeMQConnectionFactory(){
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.121.126:61616"); return factory;
} }
///
ActiveQq的代码实现的更多相关文章
- 日期格式代码出现两次的错误 ORA-01810
错误的原因是使用了两次MM . 一.Oracle中使用to_date()时格式化日期需要注意格式码 如:select to_date('2005-01-01 11:11:21','yyyy-MM-dd ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- iOS代码规范(OC和Swift)
下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...
- Jquery的点击事件,三句代码完成全选事件
先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- redux-amrc:用更少的代码发起异步 action
很多人说 Redux 代码多,开发效率低.其实 Redux 是可以灵活使用以及拓展的,经过充分定制的 Redux 其实写不了几行代码.今天先介绍一个很好用的 Redux 拓展-- redux-amrc ...
- 编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议75~78)
建议75:集合中的元素必须做到compareTo和equals同步 实现了Comparable接口的元素就可以排序,compareTo方法是Comparable接口要求必须实现的,它与equals方法 ...
- 使用 .NET WinForm 开发所见即所得的 IDE 开发环境,实现不写代码直接生成应用程序
直接切入正题,这是我09年到11年左右业余时间编写的项目,最初的想法很简单,做一个能拖拖拽拽就直接生成应用程序的工具,不用写代码,把能想到的业务操作全部封装起来,通过配置的方式把这些业务操作组织起来运 ...
- jsp前端实现分页代码
前端需要订一page类包装,其参数为 private Integer pageSize=10; //每页记录条数=10 private Integer totalCount; //总记录条数 priv ...
- 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】
说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...
随机推荐
- Ajax 局部刷新 异步提交
AJAX简介 局部刷新,异步提交. AJAX 不是新的编程语言,而是一种使用现有标准的新方法.它最大的有点就是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容. 浏览器朝后端发送请 ...
- Intellj IDEA 光标显示insert状态解决办法
使用idea过程中,不知道怎么回事,鼠标的光标老是insert状态,体验效果极其差劲,于是去百度,扒拉了好一阵,过滤了垃圾博客,发现了有两种方法可以解决此问题: 第一种方法: 在File------& ...
- 定位API的原理
参考:0Day 安全 所有的win_32程序都会加载ntdll.dll和kerner32.dll这两个最基础的动态链接库.如果想要在win_32平台下定位kernel32.dll中的API地址 1,首 ...
- 分布式链路追踪系统Sleuth和ZipKin
1.微服务下的链路追踪讲解和重要性 简介:讲解什么是分布式链路追踪系统,及使用好处 进行日志埋点,各微服务追踪. 2.SpringCloud的链路追踪组件Sleuth 1.官方文档 http://cl ...
- docker容器 如何精简镜像减小体积
写在前面 我们在上篇<Docker容器 关于镜像构建的安全问题>一起学习了如何构建一个基于安全的镜像,这篇小作文我们会学习镜像构建的另一个关键性问题,为何别人打造的镜像只有10MB而我的有 ...
- shiro登录源码
//1.获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager Factory<org.apache.shiro.mgt.SecurityManage ...
- jdbc操作mysql(三):利用注解封装
案例五:利用注解封装 重复步骤 我们使用jdbc操作mysql时发现,操作不同表中数据,所写的方法基本相同:比如我们根据id向用户表添加数据,根据id删除商品表的数据,或者查询所有数据并用list集合 ...
- MySQL日志系统bin log、redo log和undo log
MySQL日志系统bin log.redo log和undo log 今人不见古时月,今月曾经照古人. 简介:日志是MySQL数据库的重要组成部分,记录着数据库运行期间各种状态信息,主要包括错误日 ...
- 【Spring 5.x】学习笔记汇总
Spring 工厂 工厂设计模式.第一个Spring程序细节分析.整合日志框架 注入详解 - Set注入(JDK内置类型,用户自定义类型).构造注入(重载) 反转控制与依赖注入.Spring工厂创建复 ...
- Spring Cloud Apollo 实践
接上一篇Windows下安装Apollo的常见问题,安装完毕后试着看怎么来使用一下. 首先到管理页面创建一个新的应用: 创建成功后会自动跳转到应用的维护界面,如下图所示: 新增一个配置信息来进行后续的 ...