ActiveMQ的基本使用
优点:消息中间件主要解决了各个模块异步调动问题,解除了模块之间的耦合,提高了运行的性能。
一.点对点方式
生产者
1.创建连接工厂
ActiveMQConnectionFactory 基于TCP协议
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.25.135:61616")
2.创建连接
Connection connection=connectionFactory.createConnection();
3.启动连接
connection.start();
4.获取Session,参数1:是否启动事务 参数2:消息的确认方式
Session session=connection.creatSession(false,AUTO_ACKNOWLEDGE);
5.创建队列对象
Queue queue=session.creatQueue("test-queue");
6.创建消息的生产者对象 Destination是Queue的父接口
MessageProduceer producer=session.createProducer(queue);
7.创建消息对象(文本消息)
TextMessage textMessage=session.creatTextMessage("欢迎您");
8.发送消息
producer.send(textMessage);
9.关闭资源
producer.close();
session.close();
connection.close();
消费者
1.创建连接工厂
ActiveMQConnectionFactory 基于TCP协议
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.25.135:61616")
2.创建连接
Connection connection=connectionFactory.createConnection();
3.启动连接
connection.start();
4.获取Session,参数1:是否启动事务 参数2:消息的确认方式
Session session=connection.creatSession(false,AUTO_ACKNOWLEDGE);
5.创建队列对象
Queue queue=session.creatQueue("test-queue");
6.创建消费者对象
MessageConsumer consumer=session.createConsumer(queue);
7.设置监听
consumer.setMessageListener(new MessageLister(){
public void onMessage(Message message){
TextMessage textMessage=(TextMessage)message;
try{
System.out.println("提取的消息:"+textMessage.getText());
}catch(JMSException e){
e.printStackTrace();
}
}
});
8.等待键盘输入
System.in.read();
9.关闭资源
consumer.close();
session.close();
connection.close();
二.订阅者模式
生产者
1.创建连接工厂
ActiveMQConnectionFactory 基于TCP协议
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.25.135:61616")
2.创建连接
Connection connection=connectionFactory.createConnection();
3.启动连接
connection.start();
4.获取Session,参数1:是否启动事务 参数2:消息的确认方式
Session session=connection.creatSession(false,AUTO_ACKNOWLEDGE);
5.创建主题对象
Topic topic=session.creatTopic("test-topic");
6.创建消息的生产者对象 Destination是Queue的父接口
MessageProduceer producer=session.createProducer(topic);
7.创建消息对象(文本消息)
TextMessage textMessage=session.creatTextMessage("欢迎您");
8.发送消息
producer.send(textMessage);
9.关闭资源
producer.close();
session.close();
connection.close();
消费者
1.创建连接工厂
ActiveMQConnectionFactory 基于TCP协议
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.25.135:61616")
2.创建连接
Connection connection=connectionFactory.createConnection();
3.启动连接
connection.start();
4.获取Session,参数1:是否启动事务 参数2:消息的确认方式
Session session=connection.creatSession(false,AUTO_ACKNOWLEDGE);
5.创建主题对象
Topic topic=session.creatTopic("test-topic");
6.创建消费者对象
MessageConsumer consumer=session.createConsumer(topic);
7.设置监听
consumer.setMessageListener(new MessageLister(){
public void onMessage(Message message){
TextMessage textMessage=(TextMessage)message;
try{
System.out.println("提取的消息:"+textMessage.getText());
}catch(JMSException e){
e.printStackTrace();
}
}
});
8.等待键盘输入
System.in.read();
9.关闭资源
consumer.close();
session.close();
connection.close();
ActiveMQ的基本使用的更多相关文章
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- (jms)ActiveMQ 安装配置.
前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...
- node(ActiveMq)
简单尝试了node下的ActiveMQ 1.下载apache-activemq-5.9.0,执行bat文件: 2.登录http://localhost:8161/admin可查看其管理后台: 3.安装 ...
- ActiveMQ的集群方案对比及部署
转载:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- JMS学习之路(一):整合activeMQ到SpringMVC
JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到实际的业务需求中的话我们可以 ...
- ActiveMQ消息队列的使用及应用
这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录: 一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- Spring下ActiveMQ实战
MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...
- ActiveMQ(li)
一.ActiveMQ 首先,ActiveMQ不是一个框架,它不是struct,webx,netty这种框架,它更像是tomcat服务器,因为你使用它之前必须启动它,activeMQ和JMS的关系有点类 ...
随机推荐
- Symfony2在Nginx下的配置方法图文教程
来源: https://www.xp.cn/b.php/79706.html Symfony2在Nginx下的配置方法图文教程 本文详细讲述了Symfony2在Nginx下的配置方法.分享给大家供大家 ...
- Adobe Acrobat PDF Reader DC软件下载
安装包下载 https://get.adobe.com/en/reader/enterprise/ ftp下载,按日期排序 ftp://ftp.adobe.com/pub/adobe/reader/w ...
- ansible笔记第四章(jinj2的使用与role的使用)
一.jinj2概述 1.jinja2模板与Ansible有什么关系 Ansible通常会使用Jlinja2模板来修改被管理主机的配置文件.例如给10台远程主机都装上httpd服务,但是要求每个服务器的 ...
- vscode 右键运行php文件到浏览器
1.安装PHP Server插件 2.在需要打开的文件中右键选择PHP Server:Server project 3.浏览器页面显示
- gson TypeAdapter 和FieldNamingStrategy,SerializedName实现属性名称的设置别名
gson TypeAdapter 和FieldNamingStrategy,SerializedName实现属性名称的设置别名 package com.example.core.mydemo.Type ...
- java post格式发送application/x-www-form-urlencoded
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.*; ...
- Jmeter - Config Tips
Guideline: Jmeter.properties file should avoid to be modified , modified user.properties instead > ...
- 解题报告:Codeforces 768B Code For 1
Codeforces 768B Code For 1 题义 有一个序列,刚开始,只有1个数\(n\),接着按照以下规则变化找到序列中任意一个\(>1\)的数\(p\),将他变为 \(\lfloo ...
- C与C++字符串比较
#include<iostream> #include<string> using namespace std; int main() { char a[] = "a ...
- .NetCore【工作应用】Unity
Unity Unity是一个IoC容器,用来实现依赖注入(Dependency Injection,DI),减少耦合 Unity安装 install-package Unity 使用 IUnityCo ...