消息中间件 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规范的消息中间件服务器客户端:发送或接收消息的 ...
随机推荐
- PHP:第一章——PHP中的goto语句和
goto语句: <?php header("Content-Type:text/html;charset=utf-8"); /*goto语句:可以代替break*/ //例1 ...
- 浅谈Obejct.assign
Object.assign属于浅拷贝 Object.assign只能拷贝:可被枚举的属性,自有属性,string或者Symbol类型是可以被直接分配的 var ab={ name:"没有被覆 ...
- 重置delphi Printer对象
http://www.efg2.com/Lab/Library/UseNet/1999/0714b.txt From: "Nick Ryan" <nick@avatardes ...
- pywin32 的安装
这个东西不是在包管理器安装一下就可以的. https://github.com/mhammond/pywin32/releases 请到git下载exe安装文件.
- Python 编程核心知识体系-模块|面向对象编程(三)
模块 面向对象编程
- SQL 递归找查所有子节点及所有父节
在SQL的树型结构中,很多时候,知道某一节点的值,需要查找该节点的所有子节点(包括多级)的功能,这时就需要用到如下的用户自定义函数. 表结构如下: ID int Dep_Type int Dep_Co ...
- cocos2dx 不同平台上加载文件
原文转自:http://blog.sina.com.cn/s/blog_62b2318d0101eozt.html cocos2dx在不同平台上读取资源文件时的处理方式是不同的. 在ios下,程序调用 ...
- (转)spring hibernate properties详解
转载地址:http://blog.sina.com.cn/s/blog_692d0a650100xyqx.html Hibernate配置属性 hibernate.dialect:一个Hibernat ...
- This App does not have access to your photos or videos in iOS 9
出现这个总是由于info.plist文件内的CFBundleDisplayName没有值或者为空.把名称填进去就可以用了.
- HDU 4585
http://acm.hdu.edu.cn/showproblem.php?pid=4585 从原来的人中找出战斗数值最接近的,输出他们两人的序号 要在logn的复杂度完成查找,我用的是set,当然用 ...