首先需要下载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&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;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的简单使用的更多相关文章

  1. Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)

    首先声明:以下内容均是在网上找别人的博客综合学习而成的,可能会发现某些代码与其他博主的相同,由于参考的文章比较多,这里对你们表示感谢,就不一一列举,如果有侵权的地方,请通知我,我可以把该文章删除. 1 ...

  2. JMS - ActiveMQ集成Spring

    下面是ActiveMQ官网提供的文档.http://activemq.apache.org/spring-support.html 下面是我添加的一些dependency: <!-- jms a ...

  3. SpringBoot使用JMS(activeMQ)的两种方式 队列消息、订阅/发布

    刚好最近同事问我activemq的问题刚接触所以分不清,前段时间刚好项目中有用到,所以稍微整理了一下,仅用于使用 1.下载ActiveMQ 地址:http://activemq.apache.org/ ...

  4. 遇见JMS[1] —— activeMQ的简单使用

    1.JMS Java Message Service,提供API,供两个应用程序或者分布式应用之间异步通信,以传送消息. 2.相关概念 提供者:实现JMS规范的消息中间件服务器客户端:发送或接收消息的 ...

  5. 简单记录下springboot+jms+activemq

    1. 安装ActiveMQ 到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下后运行 2. pom.xml引入  springboot配置文件中填写相关配置 3.创建生产者 ...

  6. JMS ActiveMQ研究文档

    1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...

  7. jms - activeMQ入门案例

    activeMQ入门案例 叨叨一波,很久没写博客了,最近比较慢,时间抽不出来,这个借口说的很尴尬...我知道作为一名合格的码农就必须养成每天一博客的习惯.希望友友们别像我这样懒,闲话不多时进入今天的主 ...

  8. JMS Activemq实战例子demo

    上一篇已经讲了JMS的基本概念,这一篇来上手练一练,如果对JMS基本概念还不熟悉,欢迎参靠JMS基本概. 这篇文章所使用的代码已经不是我刚入手时的代码,已经经过我重构过的代码,便于理解,并且加了很多中 ...

  9. (jms)ActiveMQ 安装配置.

    前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...

随机推荐

  1. 【06】循序渐进学 docker:跨主机通信

    写在前面的话 目前解决容器跨主机通信的方案有很多种,这里给出的只是其中的一种,而且还不是最好的方案,不过归根结底,大同小异.在学习 docker swarm 之前,大家可以先看看这种. 啥是 over ...

  2. python之爬虫(三) Urllib库的基本使用

    官方文档地址:https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的HTTP请求库包括以下模块urllib.r ...

  3. Red Hat安全性指南

    https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/security_guide/sec-usi ...

  4. leecode刷题(13) -- 字符串中的第一个唯一字符

    leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...

  5. [Swift]队列Queue的两种版本:(1)用类包装Queue (2)用泛型包装Queue

    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...

  6. Struts2框架action路径问题心得----》页面url请求怎么找action

    Struts2 页面url请求怎么找action Struts2 页面url请求如何找action 1.我们使用最原始的方法去查找action,不同注解. struts.xml文件先配置 <!- ...

  7. h5 fieldset

    http://www.w3school.com.cn/tiy/t.asp?f=html_fieldset 这个标签可以让border里面写入文字

  8. 「BZOJ3998」[TJOI2015] 弦论(第K小子串)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3998 Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input ...

  9. node爬取页面元素

    /** * Created by on 2018/12/25. */const http = require("https");const fs = require('fs'); ...

  10. 128th LeetCode Weekly Contest Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...