ActiveMQ在Win7的搭建与使用

Win7

1.下载ActiveMQ

去官方网站下载:官网为http://activemq.apache.org/

2.运行ActiveMQ

解压缩apache-activemq-5.9.0-bin.zip,然后双击apache-activemq-5.9.0\bin\activemq.bat运行ActiveMQ程序。

启动ActiveMQ以后,登录:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。


登录activemq:http://localhost:8161/admin/


创建简单的生产者----消费者模式例子:

maven依赖:

<dependencies>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-client</artifactId>
        <version>5.11.1</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>

消息生产者:

package com.controller.demo;

/**
 * Created by qixuan.chen on 2017/7/21.
 */

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

import javax.jms.*;

/**
 * 消息生成者
 */
public class Producer {

    private static final Logger LOG = Logger.getLogger(Producer.class);

    public static void main(String[] args) {
        // 获取连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://192.168.1.200:61616");

        /* 获取连接 */
        Connection connection = null;
        try {
            connection = factory.createConnection();
            connection.start();
        } catch (JMSException e) {
            LOG.error("获取连接出现异常", e);
        }

        /* 创建会话 */
        Session session = null;
        try {
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            LOG.error("创建会话出现异常", e);
        }

        /* 创建消息生产者 */
        Destination destination = null;
        try {
            destination = session.createQueue("TestQueue");
        } catch (JMSException e) {
            LOG.error("创建队列出现异常", e);
        }

        /* 创建队列 */
        MessageProducer producer = null;
        try {
            producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        } catch (JMSException e) {
            LOG.error("创建消息生产者出现异常", e);
        }

        /* 发送消息 */
        ObjectMessage message = null;
        try {
            message = session.createObjectMessage("hello world...");
            producer.send(message);
        } catch (JMSException e) {
            LOG.error("发送消息出现异常", e);
        }

        try {
            session.commit();
        } catch (JMSException e) {
            LOG.error("提交会话出现异常", e);
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error("关闭连接出现异常", e);
            }
        }

        LOG.info("sent...");
    }

}

消费者:

package com.controller.demo;

/**
 * Created by qixuan.chen on 2017/7/21.
 */
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

public class Consumer {

    private static final Logger LOG = Logger.getLogger(Consumer.class);

    // 是否继续响应,可按需由其他逻辑修改值,true:继续响应,false-停止响应
    public static volatile boolean handleFlag = true;

    public static void main(String[] args) {
        // 获取连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://192.168.1.200:61616");

        /* 获取连接 */
        Connection connection = null;
        try {
            connection = factory.createConnection();
            connection.start();
        } catch (JMSException e) {
            LOG.error("获取连接出现异常", e);
        }

        /* 创建会话 */
        Session session = null;
        try {
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            LOG.error("创建会话出现异常", e);
        }

        /* 创建消息生产者 */
        Destination destination = null;
        try {
            destination = session.createQueue("TestQueue");
        } catch (JMSException e) {
            LOG.error("创建队列出现异常", e);
        }

        /* 创建消费者 */
        MessageConsumer consumer = null;
        try {
            consumer = session.createConsumer(destination);
        } catch (JMSException e) {
            LOG.error("创建消费者出现异常", e);
        }

        /* 获取消息对象 */
        ObjectMessage objectMessage = null;
        while(handleFlag) {
            try {
                objectMessage = (ObjectMessage)consumer.receive();
                handleMessage(objectMessage);
            } catch (JMSException e) {
                LOG.error("接收消息出现异常", e);
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error("关闭连接出现异常", e);
            }
        }

    }

    /**
     * 处理消息对应的业务
     * @param objectMessage 消息对象
     */
    public static void handleMessage(final ObjectMessage objectMessage) {
        if (objectMessage == null) {
            return;
        }

        /* 处理业务 */
        Object object = null;
        try {
            object = objectMessage.getObject();
        } catch (JMSException e) {
            LOG.error("获取消息内容出现异常", e);
        }
        handleMessage(object);
    }

    /**
     * 处理消息对应的业务
     */
    public static void handleMessage(Object object) {
        if (object == null) {
            return;
        }

        String messageString = (String)object;
        LOG.info("Receive : " + messageString); // 这里仅作打印业务而已
    }

}

看到控制台打印出:Receive : hello world...,可知接收到消息了。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值