JMS - ActiveMQ的简单使用
首先需要下载ActiveMQ,下面的链接给我们列出了所有版本:
http://activemq.apache.org/download-archives.html
每个版本为不同的OS提供了链接:
公司电脑是windows的,用目录下的activemq.bat启动:
端口号默认是61616,可以在conf/activemq.xml中看到:
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
相关的Maven dependency:
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
使用javax.jms.Session跟JMS Provider通信:
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); Connection connection = connectionFactory.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
然后一些目的地、发送者、发送内容什么的都是由session来弄的:
Destination destination = session.createQueue("this is sparta!!"); MessageProducer producer = session.createProducer(destination); TextMessage message0 = session.createTextMessage("这是斯巴达!!!");
TextMessage message1 = session.createTextMessage("这也是斯巴达!!!");
TextMessage message2 = session.createTextMessage("这些都是斯巴达!!!"); producer.send(message0);
producer.send(message1);
producer.send(message2); session.commit();
有了producer,相应地也有consumer,接收消息方法如下:
MessageConsumer consumer = session.createConsumer(destination);
System.out.println(((TextMessage) consumer.receive(10000)).getText());
结果还是consumer去一个个receive了,就像是接收人亲自去确认那样。
或许我们可以让Listener代劳:
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
System.out.println("listener catched:::"+((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
当这个consumer设置了Listener的时候就不能再以receive()的方式接收了,
不然会出现javax.jms.IllegalStateException:Cannot synchronously receive a message when a MessageListener is set
如果想使用publish/subscribe,直接将createQueue改为createTopic即可,但需要理解Topic是无状态的。
完整code如下,发送者:
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("this is sparta!!"); MessageProducer producer = session.createProducer(destination);
TextMessage message0 = session.createTextMessage("这是斯巴达!!!");
TextMessage message1 = session.createTextMessage("这也是斯巴达!!!");
TextMessage message2 = session.createTextMessage("这些都是斯巴达!!!");
producer.send(message0);
producer.send(message1);
producer.send(message2); session.commit();
}
接收者:
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");;
Connection connection = connectionFactory.createConnection();
connection.start(); Session session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("this is sparta!!");
MessageConsumer consumer = session.createConsumer(destination);
System.out.println(((TextMessage) consumer.receive(10000)).getText());
System.out.println(((TextMessage) consumer.receive(10000)).getText());
System.out.println(((TextMessage) consumer.receive(10000)).getText());
}
JMS - ActiveMQ的简单使用的更多相关文章
- Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)
首先声明:以下内容均是在网上找别人的博客综合学习而成的,可能会发现某些代码与其他博主的相同,由于参考的文章比较多,这里对你们表示感谢,就不一一列举,如果有侵权的地方,请通知我,我可以把该文章删除. 1 ...
- JMS - ActiveMQ集成Spring
下面是ActiveMQ官网提供的文档.http://activemq.apache.org/spring-support.html 下面是我添加的一些dependency: <!-- jms a ...
- SpringBoot使用JMS(activeMQ)的两种方式 队列消息、订阅/发布
刚好最近同事问我activemq的问题刚接触所以分不清,前段时间刚好项目中有用到,所以稍微整理了一下,仅用于使用 1.下载ActiveMQ 地址:http://activemq.apache.org/ ...
- 遇见JMS[1] —— activeMQ的简单使用
1.JMS Java Message Service,提供API,供两个应用程序或者分布式应用之间异步通信,以传送消息. 2.相关概念 提供者:实现JMS规范的消息中间件服务器客户端:发送或接收消息的 ...
- 简单记录下springboot+jms+activemq
1. 安装ActiveMQ 到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下后运行 2. pom.xml引入 springboot配置文件中填写相关配置 3.创建生产者 ...
- JMS ActiveMQ研究文档
1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...
- jms - activeMQ入门案例
activeMQ入门案例 叨叨一波,很久没写博客了,最近比较慢,时间抽不出来,这个借口说的很尴尬...我知道作为一名合格的码农就必须养成每天一博客的习惯.希望友友们别像我这样懒,闲话不多时进入今天的主 ...
- JMS Activemq实战例子demo
上一篇已经讲了JMS的基本概念,这一篇来上手练一练,如果对JMS基本概念还不熟悉,欢迎参靠JMS基本概. 这篇文章所使用的代码已经不是我刚入手时的代码,已经经过我重构过的代码,便于理解,并且加了很多中 ...
- (jms)ActiveMQ 安装配置.
前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...
随机推荐
- day12学python 多进程+进程池
多进程+进程池 多进程(不同进程不可直接访问数据) 引入(多进程套线程) 多进程 需导入multiprocessing模块 模板示例1 import threading,time,multiproce ...
- [Android] Android MVP 架构下 最简单的 代码实现
Android MVP 架构下 最简单的 代码实现 首先看图: 上图是MVP,下图是MVC MVP和MVC的区别,在于以前的View层不仅要和model层交互,还要和controller层交互.而 ...
- PHP中implode()和explode()
1, implode()函数返回由数组元素组合成的字符串,函数语法:string implode(separator,array),separator参数可选,规定数组元素之间放置的内容,默认是空字符 ...
- Ubuntu更新提示哈希和不匹配“Hash Sum mismatch”
Ubuntu更新提示哈希和不匹配"Hash Sum mismatch" 今天在常规更新软件的时候,我的ubuntu报了一个错误. 应该是ubuntu程序更新转交给另外一个更新造成 ...
- Tomcat入门级小白教程
Tomcat 类似与一个apache的扩展型,属于apache软件基金会的核心项目,属于开源的轻量级Web应用服务器,是开发和调试JSP程序的首选,主要针对Jave语言开发的网页代码进行解析,Tomc ...
- leetcode-819-Most Common Word(词频统计)
题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- Hibernate 自动创建表bug问题解决
我在hibernate.cfg.xml配置文件中添加了自动创建表的的属性:(这样当数据库中没有此表是,hibernate就会自动帮我们创建一张表) <property name="hb ...
- 百度地图 Infowidow 内容(content 下标签) 点击事件
需要监听 infowindow 的打开事件 ,查看InfoWindow API 实现 html 点击效果 代码 var infoWindow = that.createDangerInfoWindo ...
- 01. css sprite是什么,有什么优缺点?
1.css sprite是什么,有什么优缺点? 通常被意译为“CSS图像拼合”或“CSS贴图定位” 1)CSS Sprites的优点 利用CSS Sprites能很好地减少网页的http请求,从而大大 ...
- 【医学影像】《Identifying Medical Diagnoses and Treatable Diseases by Image-Based Deep Learning》论文笔记
这篇论文的作者是张康教授为首的团队,联合国内外众多医院及科研机构,合力完成,最后发表在cell上,实至名归. 从方法的角度上来说,与上一篇博客中的论文很相似,采用的都是InceptionV3模型,同时 ...