ActiveMQ初体验


本文完整工程github地址: https://github.com/LovelyBear2018/ActiveMQDemo/

一、下载安装ActiveMQ

ActiveMQ官网下载地址:http://activemq.apache.org/download-archives.html
ActiveMQ 提供了Windows 和Linux、Unix 等几个版本,这里选择了Linux 版本下进行开发。
在这里插入图片描述

下载完后,解压安装包,目录为:
在这里插入图片描述
各个目录的作用分别为:

bin:脚本文件
conf:基本配置文件
data:日志文件
docs:说明文档
examples:简单的实例
lib:activemq所需jar包
webapps:项目的目录

启动ActiveMQ

进入到ActiveMQ 根目录的Bin目录下,输入 ./activemq start 命令启动ActiveMQ服务。若提示创建进程号ID,则表示创建成功。
在这里插入图片描述
ActiveMQ服务启动时,默认启动了一个内置的web服务器,提供一个用于监控ActiveMQ服务的前端界面。访问路径为:
http://127.0.0.1:8161/admin/
打开链接之后输入账号密码:
账号:admin
密码:admin
在这里插入图片描述
在这里插入图片描述

到这里为止,ActiveMQ 服务启动完毕。
ActiveMQ重启命令为 ./activemq restart
ActiveMQ停止命令为 ./activemq stop

三、ActiveMQ Demo工程

在这里插入图片描述

在ActiveMQ中有一个Jar包
在这里插入图片描述
将其导入到项目中。

创建生产者
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.concurrent.atomic.AtomicInteger;	
/**
 * ActiveMQ生产者
 * Created by liuzhixiong on 2018/11/15.
 */
public class Producer {
    //ActiveMq 的默认用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    //ActiveMq 的默认登录密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    //ActiveMQ 的链接地址
    private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
    //链接工厂
    ActiveMQConnectionFactory connectionFactory;	
    //链接对象
    Connection connection;
    //事务管理
    Session session;
    AtomicInteger count = new AtomicInteger(0);
    ThreadLocal<MessageProducer> producers = new ThreadLocal<MessageProducer>();
    public void init() {
        try {
            //创建一个链接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
            //从工厂中创建一个链接
            connection = connectionFactory.createConnection();
            //开启链接
            connection.start();
            //创建一个事务(这里通过参数可以设置事务的级别)
            session = connection.createSession(true, Session.SESSION_TRANSACTED);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    public void sendMessage(String disname) {
        try {
            //创建一个消息队列
            Queue queue = session.createQueue(disname);
            //消息生产者
            MessageProducer messageProducer;
            if (producers.get() != null) {
                messageProducer = producers.get();
            } else {
                messageProducer = session.createProducer(queue);
                producers.set(messageProducer);
            }
            while (true) {
                Thread.sleep(1000);
                int num = count.getAndIncrement();
                //创建一条消息
                TextMessage msg = session.createTextMessage(Thread.currentThread().getName() +
                        "productor:我是生产者,我现在正在生产东西!,count:" + num);
                System.out.println(Thread.currentThread().getName() +
                        "productor:我是生产者,我现在正在生产东西!,count:" + num);
                //发送消息
                messageProducer.send(msg);
                //提交事务
                session.commit();
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
创建消费者
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.concurrent.atomic.AtomicInteger;	
/**
 * ActiveMQ消费者
 * Created by liuzhixiong on 2018/11/15.
 */
public class Consumer {
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
    ConnectionFactory connectionFactory;
    Connection connection;
    Session session;
    ThreadLocal<MessageConsumer> consumers = new ThreadLocal<MessageConsumer>();
    AtomicInteger count = new AtomicInteger();
    public void init() {
        try {
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
	public void getMessage(String disname) {
        try {
            Queue queue = session.createQueue(disname);
            MessageConsumer consumer = null;

            if (consumers.get() != null) {
                consumer = consumers.get();
            } else {
                consumer = session.createConsumer(queue);
                consumers.set(consumer);
            }
            while (true) {
                Thread.sleep(1000);
                TextMessage msg = (TextMessage) consumer.receive();
                if (msg != null) {
                    msg.acknowledge();
                    System.out.println(Thread.currentThread().getName() + ": Consumer:我是消费者,我正在消费Msg" + msg.getText() + "--->" + count.getAndIncrement());
                } else {
                    break;
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

运行ActiveMQDemo项目

生产者发消息
public class ProducerTest {
    public static void main(String[] args){
        Producer producter = new Producer();
        producter.init();
        ProducerTest testProducer = new ProducerTest();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(testProducer.new ProductorMq(producter)).start();
        new Thread(testProducer.new ProductorMq(producter)).start();
        new Thread(testProducer.new ProductorMq(producter)).start();
        new Thread(testProducer.new ProductorMq(producter)).start();
        new Thread(testProducer.new ProductorMq(producter)).start();
    }
    private class ProductorMq implements Runnable{
        Producer producter;
        public ProductorMq(Producer producter){
            this.producter = producter;
        }
        @Override
        public void run() {
            while(true){
                try {
                    producter.sendMessage("TestQueue");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
运行结果
Thread-2productor:我是生产者,我现在正在生产东西!,count:0
Thread-4productor:我是生产者,我现在正在生产东西!,count:2
Thread-5productor:我是生产者,我现在正在生产东西!,count:1
Thread-3productor:我是生产者,我现在正在生产东西!,count:3
Thread-6productor:我是生产者,我现在正在生产东西!,count:4
Thread-6productor:我是生产者,我现在正在生产东西!,count:5
Thread-2productor:我是生产者,我现在正在生产东西!,count:6
Thread-4productor:我是生产者,我现在正在生产东西!,count:7
消费者收消息
public class ConsumerTest {
    public static void main(String[] args){
        Consumer consumer = new Consumer();
        consumer.init();
        ConsumerTest testConsumer = new ConsumerTest();
        new Thread(testConsumer.new ConsumerMq(consumer)).start();
        new Thread(testConsumer.new ConsumerMq(consumer)).start();
        new Thread(testConsumer.new ConsumerMq(consumer)).start();
        new Thread(testConsumer.new ConsumerMq(consumer)).start();
    }
    private class ConsumerMq implements Runnable{
        Consumer comsumer;
        public ConsumerMq(Consumer comsumer){
            this.comsumer = comsumer;
        }
        @Override
        public void run() {
            while(true){
                try {
                    comsumer.getMessage("TestQueue");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
运行结果
Thread-5: Consumer:我是消费者,我正在消费MsgThread-5productor:我是生产者,我现在正在生产东西!,count:0--->1
Thread-3: Consumer:我是消费者,我正在消费MsgThread-2productor:我是生产者,我现在正在生产东西!,count:3--->2
Thread-2: Consumer:我是消费者,我正在消费MsgThread-4productor:我是生产者,我现在正在生产东西!,count:2--->0
Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是生产者,我现在正在生产东西!,count:1--->3
Thread-5: Consumer:我是消费者,我正在消费MsgThread-6productor:我是生产者,我现在正在生产东西!,count:4--->4
Thread-3: Consumer:我是消费者,我正在消费MsgThread-6productor:我是生产者,我现在正在生产东西!,count:5--->5
Thread-2: Consumer:我是消费者,我正在消费MsgThread-4productor:我是生产者,我现在正在生产东西!,count:7--->6
Thread-4: Consumer:我是消费者,我正在消费MsgThread-5productor:我是生产者,我现在正在生产东西!,count:8--->7

查看运行结果,http://127.0.0.1:8161/admin/ 里面的Queues 中查看生产与消费情况。
在这里插入图片描述

ActiveMQ使用场景

1、多个项目之间集成
(1) 跨平台
(2) 多语言
(3) 多项目
2、降低系统间模块的耦合度,解耦
(1) 软件扩展性
3、系统前后端隔离
(1) 前后端隔离,屏蔽高安全区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值