消息中间件 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规范的消息中间件服务器客户端:发送或接收消息的 ...
随机推荐
- intel python加速效果初探
python3安装intel的加速库: conda config --add channels intel conda create --name intelpy intelpython3_full ...
- spring boot 学习(十)SpringBoot配置发送Email
SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...
- 火狐浏览器访问网站出现 HTTP Error 400. The request is badly formed.错误,怎么解决
今天在访问某个网站时,出现一个“HTTP Error 400. The request is badly formed.”错误, 那么应该如何解决呢? 1.问题描述: 用火狐浏览网站出现“”HTTP ...
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
- MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(六)
[周年庆]MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] MyEclipse支持Java EE技术(如JAX-WS和EJB 3.0),它们以功能包 ...
- 粗略学习《Agile Guide》后的总结
碍于个人能力极度欠佳,所以即使我大致了解了一下何谓“Agile Guide”(敏捷开发),也不很能理解其中的软件工程思想,只能大概谈一下我的理解. 我所理解的“敏捷开发”,应该是一种特殊的.相较于传统 ...
- Redis (error) NOAUTH Authentication required.解决方法
当设置redis密码后,打开客户端,需要使用密码验证 auth 123456 就是设置的密码
- 转:HTML5中的element.dataset
使用HTML5中的 element.dataset 操作自定义 data-* 数据: 不久之前我向大家展示了非常有用的classList API,它是一种HTML5里提供的原生的对页面元素的CSS类进 ...
- Java第九次作业--输入输出流和文件操作
Deadline: 2017-5-25 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握使用File类访问文件 掌握IO操作的基本原理 掌握字节流和字符流读写文件的操作 二.作业 ...
- Oracle text组件安装
1.目标:在数据库中,安装Oracle Text组件: 970473.1 MOS文档ID 2.组件相关视图:查询验证 #查询DB中的组件: #视图:USER_REGISTRY (注册) COM ...