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 ...
随机推荐
- LA 3401
彩色立方体. [分析] 弄清楚24种状态,可以由标准姿态旋转而来. 计算24种状态: #include <iostream> #include <string> ,,,,, } ...
- 爬虫开发14.scrapy框架之分布式操作
分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux: redis-server redis.conf windows: redis-server.exe redis-wi ...
- xshell连接centos虚拟机的几点注意
我家用电脑使用联通的宽带,使用virtualbox装了centos6,连接方式使用NAT网络,还有一个是网络地址转换(NAT),不清楚区别是什么,使用xshell连接 当使用cd /etc/sysco ...
- Docker部署常见问题
一.删除容器和镜像 在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器(哪怕是已经停止的容器),否则无法删除该镜像. 停止容器 # docker stop $(docker ps ...
- java学习笔记_多态
多态:父类的变量可以引用子类的对象 引用: Student s1 = stu; s1.setScore(200); 1.一个对象可以有多个引用,可以有多个变量操作同一个对象 2.当一个对象没有任何 ...
- 【转】MVC,MVP 和 MVVM 的图示
复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身很容易理解,但是要讲清楚,它与衍生的 MVP ...
- js数组的常用操作
数组合并 var arr=[1,"abc","张三","122"]; var b=["今天天气不错","适合学 ...
- 手动博客重定向 https://www.cnblogs.com/kelthuzadx/
https://www.cnblogs.com/kelthuzadx/ 博客狂魔又㕛叒换地址了
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
从 NSDictionary 取值的时候有两个方法,objectForKey: 和 valueForKey:,这两个方法具体有什么不同呢? 先从 NSDictionary 文档中来看这两个方法的定义: ...
- 评估指标:ROC,AUC,Precision、Recall、F1-score
一.ROC,AUC ROC(Receiver Operating Characteristic)曲线和AUC常被用来评价一个二值分类器(binary classifier)的优劣 . ROC曲线一般的 ...