消息中间件 ActiveMQ的简单使用
一、AactiveMQ的下载和安装
1. 下载ActiveMQ
地址:http://activemq.apache.org/activemq-5152-release.html
我这里下载的是window的版本
2. 下载后,解压
里面有win32位和win64两种文件夹,找到你电脑上对应的win版本,我这里用的win64
右击activemq.bat,并且以管理员身份运行
启动成功后,会打印http的地址
打开这个网址http://127.0.0.1:8186
二、代码的使用
1. 创建工程
创建一个Maven工程,
2. 创建生产者
public class AppProducer
{
private static final String url = "tcp://192.168.2.121:61616"; private static final String queueName="queue-test"; public static void main(String[] args){
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); try {
//2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动连接
connection.start(); //4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createQueue(queueName); //6. 创建一个目标
MessageProducer producer = session.createProducer(destination); for(int i=0; i<100; i++){
//7. 创建消息
TextMessage textMessage = session.createTextMessage("test" + i);
//8. 发布消息
producer.send(textMessage);
System.out.println("发送消息" + textMessage.getText());
}
//9.关闭连接
connection.close(); } catch (JMSException e) {
e.printStackTrace();
} }
3. 创建消费者
public class AppConsumer {
private static final String url = "tcp://192.168.2.121:61616"; private static final String queueName="queue-test"; public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); //2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动连接
connection.start(); //4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createQueue(queueName); //6. 创建一个消费者
MessageConsumer consumer = session.createConsumer(destination); //7. 创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message; try {
System.out.println("接收消息" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}); }
}
三、主题模式下的消息
1. 消费者
public class AppConsumer {
private static final String url = "tcp://192.168.2.121:61616"; private static final String topicName="topic-test"; public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); //2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动连接
connection.start(); //4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createTopic(topicName); //6. 创建一个消费者
MessageConsumer consumer = session.createConsumer(destination); //7. 创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
try {
System.out.println("接收消息" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}); }
}
2. 创建生产者
public class AppProducer
{
private static final String url = "tcp://192.168.2.121:61616"; private static final String topicName="topic-test"; public static void main(String[] args){
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); try {
//2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动连接
connection.start(); //4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createTopic(topicName); //6. 创建一个目标
MessageProducer producer = session.createProducer(destination); for(int i=0; i<100; i++){
//7. 创建消息
TextMessage textMessage = session.createTextMessage("test" + i);
//8. 发布消息
producer.send(textMessage);
System.out.println("发送消息" + textMessage.getText());
}
//9.关闭连接
connection.close(); } catch (JMSException e) {
e.printStackTrace();
} }
}
消息中间件 ActiveMQ的简单使用的更多相关文章
- 消息中间件ActiveMQ使用详解
消息中间件ActiveMQ使用详解 一.消息中间件的介绍 介绍 消息队列 是指利用 高效可靠 的 消息传递机制 进行与平台无关的 数据交流,并基于 数据通信 来进行分布式系统的集成. 特点(作用) ...
- 消息中间件系列之ActiveMQ的简单安装
本次测试使用一台ip为192.168.2.12的虚拟机 一.解压压缩包 tar -zxvf apache-activemq-5.14.4-bin.tar.gz 二.启动activemq 进入到bin目 ...
- ElasticSearch(九):springboot项目集成消息中间件activeMQ
目的:为了将elasticsearch做成单独的服务,那么我们必须解耦,也就是业务逻辑和搜索模块是没有关系的,并且是异步的.那么项目之间通信,使用的选择有限,消息中间件是一个不错的选择. 消息中间件常 ...
- 消息中间件-activemq安全机制
activemq作为消息中间件这样一个独立的个体存在,连通用户和服务器.如果没有一套完备的安全机制去设置用户权限设置消息分发机制可想后果是非常严重.ActiveMQ如果不加入安全机制的话,任何人只要知 ...
- 消息中间件-activemq入门(二)
上一节我们了解了JMS规范并且知道了JMS规范的良好实现者-activemq.今天我们就去了解一下activemq的使用.另外我们应该抱着目的去学习,别忘了我们为什么要使用消息中间件:解耦系统之间的联 ...
- 关于消息中间件ActiveMQ的企业级应用
几个月前看到项目中配置了activeMq,于是想通透的掌握activeMq,便去网上学习搜寻资料,找到这一篇博客挺不错的,解释的比较清楚,包括原理使用和配置,特此把它分享给大家. 众所周知,消息中间件 ...
- ActiveMQ的简单例子应用
ActiveMQ是一种消息中间件,它实现了JMS规范,提供点对点和订阅-发布两种模式.下面介绍下ActiveMQ的使用: 一.环境的搭建 首先我们需要下载ActiveMQ的安装包,下载地址http:/ ...
- 消息中间件ActiveMQ及Spring整合JMS的介绍
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- 遇见JMS[1] —— activeMQ的简单使用
1.JMS Java Message Service,提供API,供两个应用程序或者分布式应用之间异步通信,以传送消息. 2.相关概念 提供者:实现JMS规范的消息中间件服务器客户端:发送或接收消息的 ...
随机推荐
- Linux文件与目录管理(一)
一.Linux文件与目录管理 1.Linux的目录结构是树状结构,最顶级的目录是根目录/(用"/"表示) 2.Linux目录结构图: /bin:bin是Binary的缩写,这个目录 ...
- IOS-整体框架类图
Cocoa框架是iOS应用程序的基础,了解Cocoa框架,对开发iOS应用有很大的帮助. 1.Cocoa是什么? Cocoa是OS X和 iOS操作系统的程序的运行环境. 是什么因素使一个程序成为Co ...
- DB2默认的事务及并发锁机制
今天有点时间,试验了一下DB2的并发锁机制,结果,和MSSQL的差不多:1.DB2的缺省行为,事务以可执行的SQL开始,以COMMIT或ROLLBACK结束:2.DB2缺省是否提交,以工具的不同而不同 ...
- 获取和设置消息队列的属性msgctl,删除消息队列
消息队列的属性保存在系统维护的数据结构msqid_ds中,用户可以通过函数msgctl获取或设置消息队列的属性. int msgctl(int msqid, int cmd, struct msqid ...
- 微信小程序中的bindTap事件(微信小程序开发QQ群:604788754)
bindTap对应的绑定事件, 第一个:wx.navigateTo wx.navigateTo({ url:"../content/content" }) 第二个:wx.redir ...
- node.js express 4.x 安装指南 (找了很久呀,痛苦之路)
问题出现这,版本的问题,还有express4.0以上的版本启动node app.js指令的变更,蛋疼.参考这个
- 北邮新生排位赛2解题报告d-e
<> 427. 学姐逗学弟 时间限制 3000 ms 内存限制 131072 KB 题目描述 学弟们来了之后,学姐每天都非常高兴的和学弟一起玩耍.这一天,学姐想出了这样一个游戏,她画了一棵 ...
- 【令人振奋】【转】微软潘正磊谈DevOps、Visual Studio 2013新功能、.NET未来
日前,微软开发平台事业部全球资深副总裁潘正磊(Julia Liuson)从美国总部回到北京参加TechEd2013,在大会现场,潘正磊接受了CSDN的访谈,对于微软研发团队如何运用DevOps模式.对 ...
- 20165202 Mypwd
实现mypwd(选做,加分) 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 提交过程博客的链接 实现过程 使用m ...
- android源码追踪学习 RecipientsEditor
RecipientsEditor 新建短信时输入收接者的editor, public class RecipientsEditor extends MultiAutoCompleteTextView ...