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&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());
    }
时间: 2024-08-12 05:02:35

JMS - ActiveMQ的简单使用的相关文章

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

首先声明:以下内容均是在网上找别人的博客综合学习而成的,可能会发现某些代码与其他博主的相同,由于参考的文章比较多,这里对你们表示感谢,就不一一列举,如果有侵权的地方,请通知我,我可以把该文章删除. 1.jms-xml Spring配置文件 [html] view plain copy print? <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springf

JMS - ActiveMQ集成Spring

下面是ActiveMQ官网提供的文档.http://activemq.apache.org/spring-support.html 下面是我添加的一些dependency: <!-- jms activemq --> <dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> <version>2.0</versio

spring boot整合JMS(ActiveMQ实现)

一.安装ActiveMQ 具体的安装步骤,请参考我的另一篇博文: http://blog.csdn.net/liuchuanhong1/article/details/52057711 二.新建spring boot工程,并加入JMS(ActiveMQ)依赖 三.工程结构 pom依赖如下: [html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu

简单记录下springboot+jms+activemq

1. 安装ActiveMQ 到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下后运行 2. pom.xml引入  springboot配置文件中填写相关配置 3.创建生产者 @Component public class JmsProducer { private static final Logger LOG = LoggerFactory.getLogger(JmsProducer.class); @Autowired private JmsMessagingTem

JMS ActiveMQ研究文档

1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务对象完成处理并返回结果后才能继续执行:(2)客户和服务对象的生命周期紧密耦合:客户进程和服务对象进程 都必须正常运行:如果由于服务对象崩溃或者网络故障导致客户的请求不可达,客户会接收到异常:(3)点对点通信:客户的一次调用只发送给某个单独的目标对象. 面向消息的中间件(Message Oriente

JMS Activemq实战例子demo

上一篇已经讲了JMS的基本概念,这一篇来上手练一练,如果对JMS基本概念还不熟悉,欢迎参靠JMS基本概. 这篇文章所使用的代码已经不是我刚入手时的代码,已经经过我重构过的代码,便于理解,并且加了很多中文注释,希望对大家有所帮助. 在基本概念一篇中已经讲到,JMS有两种消息模型,一种是点对点,另一种的发布/订阅模式.本篇文章就基于这两种消息模型来写例子. 点对点模型 先看一下生产者代码: [java] view plain copy package com.darren.activemq.queu

Spring整合ActiveMQ:spring+JMS+ActiveMQ+Tomcat

一.目录结构 相关jar包 二.关键配置activmq.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi=&quo

JMS ActiveMQ案例

创建一个web工程 导入ActiveMQ依赖的jar包  activemq-all-5.9.jar 写一个生产者(send)servlet package com.sun.jms;import java.io.IOException;import java.io.PrintWriter; import javax.jms.DeliveryMode;import javax.jms.Queue;import javax.jms.QueueConnection;import javax.jms.Qu

activeMQ的简单介绍

1.什么叫activeMQ? ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位. 2.特性列表 ⒈ 多种语言和协议编写客户端.语言: Java,C,C++,C#,Ruby,Perl,Python,PHP.应用协议: OpenWire,Stomp REST,WS Notificatio