ActiveMQ-JMS(二):发送消息

示例功能介绍

本示例实现一个java application,运行该应用,每隔1秒发送一条消息到消息队列hello-world-queue。消息队列“hello-world-queue”是通过ActiveMQ控制台创建的。通过控制台创建消息队列的方法,参见ActiveMQ学习笔记(一):安装里的第三步“验证”。

创建工程

用eclipse创建一个maven工程,工程类型使用maven-archetype-quickstart

导入jar包

通过maven导入javax.jms包

<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0.1</version>
</dependency>

通过maven导入ActiveMQ的client包

<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-client -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-client</artifactId>
    <version>5.14.0</version>
</dependency>

实现方法

要向消息队列发送消息,需要先建立与ActiveMQ的连接,然后创建一个生产者(MessageProducer)对象,再然后创建目的队列和消息对象,最后发送消息。

建立与ActiveMQ的连接

启动ActiveMQ后,默认监听tcp的61616端口,应用通过该端口与ActiveMQ建立连接。代码如下,创建一个ActiveMQ的连接工厂对象。如果使用其他的MQ,就要建立对应MQ提供的连接工厂。
一些文章讲连接工厂通过JNDI活动,其实这只是一种方法,并不是必须的。初学者可以采用本文中的方式,直接new一个连接工处对象。在正式的工程里,也可以采用配置文件来配置连接参数。

ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");

创建生产者

通过连接工厂创建连接,通过连接创建会话,通过会话创建生产者。创建生产者时可以指定目的地址,页可以不指定。这里没有指定,而是在发送消息的时候才通过参数传入

        connection = factory.createConnection();
        try {
            connection.start();
        } catch (JMSException jmse) {
            connection.close();
            throw jmse;
        }
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(null);

创建消息队列

这里创建消息队列对象,是创建一个消息队列的引用,不会真正在ActiveMQ里创建一个消息队列,这一点可以通过控制台查看队列列表验证。因为队列对象只是一个引用,因此可以多次创建ID相同的队列引用,这些引用最终指向同一个队列。
创建这个引用的作用,是指定消息队里的唯一ID。发送方用这个引用对象发送消息,接收方用这个引用对象接收消息。

Destination destination = session.createQueue("hello-world-queue");

发送消息

创建消息对象message,然后指定destination和message发送消息。支持如下类型的消息对象:BytesMessage, MapMessage, ObjectMessage, StreamMessage, TextMessage

        TextMessage message = session.createTextMessage();
        message.setText(letter);
        System.out.println("Sending: " + message.getText() + " on destination: " + destination);
        producer.send(destination, message);

完整代码

package practise.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class App {
    /**
     * ActiveMQ相关的成员变量
     */
    protected static String brokerURL = "tcp://localhost:61616";
    protected static ConnectionFactory factory;
    protected Connection connection;
    protected Session session;
    protected MessageProducer producer;

    public App() throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        try {
            connection.start();
        } catch (JMSException jmse) {
            connection.close();
            throw jmse;
        }
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(null);
    }

    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
    }

    public static void main(String[] args) throws JMSException {
        System.out.println("Hello World!");
        App publisher = new App();
        int total = 0;
        while (total < 10) {
            publisher.sendMessage(String.format("hello-%d", total));
            System.out.println("Published '" + total + "' messages");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException x) {}
            total++;
        }
        publisher.close();
    }

    protected void sendMessage(String letter) throws JMSException {
        Destination destination = session.createQueue("hello-world-queue");
        TextMessage message = session.createTextMessage();
        message.setText(letter);
        System.out.println("Sending: " + message.getText() + " on destination: " + destination);
        producer.send(destination, message);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值