ActiveMQ 队列与Topic

介绍

  1. 使用activeMQ5, 在window下搭建单节点环境
  2. 使用JMS 测试AMQ Queue和Topic

安装

下载地址:activemq

  • 解压
    在这里插入图片描述
  • 启动
    在这里插入图片描述
  • 访问
    http://localhost:8161/index.html
    登陆: admin/admin
    在这里插入图片描述

依赖

	<dependency>
	    <groupId>org.apache.activemq</groupId>
	    <artifactId>activemq-all</artifactId>
	    <version>5.14.3</version>
	</dependency>
	 <dependency>
	    <groupId>org.apache.xbean</groupId>
	    <artifactId>xbean-spring</artifactId>
	    <version>4.17</version>
	</dependency>
	

队列 Queue

生产者将消息发送到队列,消费者消费消息。生产者不用关注消费者状态。消息不会被重复消费

步骤

1.创建工厂类
2.建立连接
3.创建session
4.创建队列
5.发送消息
6.关闭连接

生产者
public class MQProcuder {

    private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";

    public void testProcuder() throws JMSException {
        //创建工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //建立获取连接
        Connection connection = factory.createConnection();
        connection.start();
        //创建session,  是否开启事物,  接收方式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建队列
        Queue queue = session.createQueue("guan-queue");
        //创建生产者
        MessageProducer producer = session.createProducer(queue);

        for (int i = 0; i < 6; i++) {
            TextMessage textMessage = session.createTextMessage("hello active" + i);
            //发送消息
            producer.send(textMessage);
        }

      	//关闭连接
        producer.close();
        session.close();
        connection.close();
    }
   //测试
    public static void main(String[] args) throws JMSException {
        MQProcuder procuder = new MQProcuder();
        procuder.procuder();
    }

}

启动生产者

Queues队列如下
在这里插入图片描述

消费者
public class MQCustomer {

    private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";

    public void testCustomer() throws JMSException {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("guan-queue");

        MessageConsumer consumer = session.createConsumer(queue);

        while (true) {
            TextMessage messge = (TextMessage) consumer.receive();
            if (messge != null) {
                String text = messge.getText();
                System.out.println(text);
            } else {
                break;
            }
        }
   }
    //测试
    public static void main(String[] args) throws JMSException {
        MQCustomer mqCustomer = new MQCustomer();
        mqCustomer.testCustomer();
    }
}

启动消费者

在这里插入图片描述
注册了1 个消费者, 消费了6 条消息

Topic

topic 是发布/订阅

生产者
public class TopicProducer {

    private static final  String ACTIVEMQ_URL = "tcp://localhost:61616";

    public void testTopicProducer() throws JMSException {

        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("guan-queue");
        MessageProducer produce = session.createProducer(topic);

        for (int i = 0; i < 6; i++) {
            TextMessage textMessage = session.createTextMessage("hello active" + i);
            //发送消息
            produce.send(textMessage);
        }

        //关闭资源
        produce.close();
        session.close();
        connection.close();
    }

    public static void main(String[] args) throws JMSException {
        TopicProducer producer = new TopicProducer();
        producer.testTopicProducer();
    }
}

启动生产者

在这里插入图片描述

消费者
public class TopicCustomer {

    private static final String ACTIVEMQ_URL = "tcp://localhost:61616";

    public void testTopicCustomer() throws Exception {

        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("guan-queue");
        MessageConsumer topConsumer = session.createConsumer(topic);

        topConsumer.setMessageListener((message -> {
            if (message != null && message instanceof TextMessage) {
                try {
                    TextMessage textMessage = (TextMessage) message;
                    System.out.println(textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }));

        System.in.read();
        System.out.println("接收完毕====");

        //关闭资源
        topConsumer.close();
        session.close();
        connection.close();
    }

    public static void main(String[] args) throws Exception {
        TopicCustomer topicCustomer = new TopicCustomer();
        topicCustomer.testTopicCustomer();
    }
}

启动消费者

在这里插入图片描述

topic 需启动消费者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>