Apache ActiveMQ DEMO的搭建流程

此处描述一下从配置mq软件到搭建项目的整个流程。


1.首先需要安装ActiveMQ,到Apache官网下载相关的安装包 http://activemq.apache.org/download-archives.html

此处我下载的版本是 ActiveMQ 5.9.1 Release ,将其解压到linux的centos下的/usr/local下的目录,进入/usr/local/apache-activemq-5.9.1/bin,运行./activemq start就启动完毕了,通常情况下肯定是正常的。


2.新建一个maven项目,添加项目依赖:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.1</version>
        </dependency>
        <!-- 日志相关依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    <properties>
        <log4j.version>1.2.12</log4j.version>
        <slf4j.version>1.6.6</slf4j.version>
    </properties>
3.建立消息生产者ProducerQueueApp.java

package com.demo.activemq.demo;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;

/**
 * Producer 功能描述:ActiveMQ生产者实例
 *
 * @author RickyLee
 * @date 2016/11/3 13:17
 */
public class ProducerQueueApp {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProducerQueueApp.class);
    private static final String BROKER_URL = "tcp://192.168.0.104:61616";
    private static final String SUBJECT = "test-activemq-queue";

    public static void main(String[] args) throws JMSException {
        //初始化连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
        //获得连接
        Connection conn = connectionFactory.createConnection();
        //启动连接,因为默认是关闭的
        conn.start();

        //创建Session,此方法第一个参数表示会话是否在事务中执行,第二个参数设定会话的应答模式
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //创建队列
        Destination dest = session.createQueue(SUBJECT);
        //createTopic方法用来创建Topic
        //session.createTopic("TOPIC");

        //通过session可以创建消息的生产者
        MessageProducer producer = session.createProducer(dest);
        for (int i = 0; i < 5; i++) {
            //初始化一个mq消息
            TextMessage message = session.createTextMessage("hello active mq 中文" + i);
            //发送消息
            producer.send(message);
            LOGGER.debug("send message {}", i);
        }

        //关闭mq连接
        conn.close();
    }
}
4.创建消费者

package com.sdzn.activemq.demo;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
/**
 * ConsumerApp 功能描述:ActiveMQ消费者实例
 *
 * @author RickyLee 
 * @date 2016/11/3 13:27
 */
public class ConsumerApp implements MessageListener {
    private static final Logger LOGGER = LoggerFactory.getLogger (ConsumerApp.class);
    private static final String BROKER_URL = "tcp://192.168.0.104:61616";
    private static final String SUBJECT = "test-activemq-queue";

    public static void main(String[] args) throws JMSException {
        //初始化ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);

        //创建mq连接
        Connection conn = connectionFactory.createConnection();
        //启动连接
        conn.start();

        //创建会话
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //通过会话创建目标
        Destination dest = session.createQueue(SUBJECT);
        //创建mq消息的消费者
        MessageConsumer consumer = session.createConsumer(dest);

        //初始化MessageListener
        ConsumerApp me = new ConsumerApp();

        //给消费者设定监听对象
        consumer.setMessageListener(me);
    }

    public void onMessage(Message message) {
        TextMessage txtMessage = (TextMessage)message;
        try {
            LOGGER.info ("get message " + txtMessage.getText());
        } catch (JMSException e) {
            LOGGER.error("error {}", e);
        }
    }
}

5.至此,搭建基本完成。通过运行生产者,进入http://192.168.0.104:8161/admin,如果有填用户名密码的地方都填admin,进入以后可以看到发送的消息;

点开test-activemq-queue可以看到

这个时候启动消费者,可以看到控制台打印的日志消息,同时,mq上的消息被消费;



至于消息订阅/发布模式和应答模式,还在研究中,后续再发文。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值