ActiveMQ(二):ActiveMQ的基本使用

一、创建Maven项目并引入依赖

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.16.2</version>
</dependency>

在这里插入图片描述

二、生产者程序

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author IT00ZYQ
 * @date 2021/6/12 22:34
 **/
public class Producer {
    public static void main(String[] args) throws JMSException, InterruptedException {
        // 1、创建连接工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616"
        );

        // 2、创建连接
        Connection connection = factory.createConnection();

        // 3. 创建Session(事务,确认)
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 4. 创建用于发送消息的队列
        Queue queue = session.createQueue("test-queue");

        // 5. 创建生产者
        MessageProducer producer = session.createProducer(queue);
        
        // 6. 创建消息并发送
        int i = 1;
        while (true) {
            System.out.println("发送消息:" + i);
            TextMessage message = session.createTextMessage("text-message -> " + i);
            producer.send(message);
            // 每3秒生产一个消息
            Thread.sleep(3000);
            i++;
        }

        // 6. 关闭连接
//        connection.close();
    }
}

在这里插入图片描述

三、消费者程序

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author IT00ZYQ
 * @date 2021/6/12 22:50
 **/
public class Consumer {
    public static void main(String[] args) throws JMSException, InterruptedException {
        // 1、创建连接工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616"
        );

        // 2、创建连接并开启
        Connection connection = factory.createConnection();
        connection.start();

        // 3. 创建Session(事务,确认)
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 4. 指定获取消息的队列
        Destination queue = session.createQueue("test-queue");

        // 5. 创建消费者
        MessageConsumer consumer = session.createConsumer(queue);

        // 6. 消费消息并输出
        while (true) {
            TextMessage message = (TextMessage)consumer.receive();
            System.out.println("消费消息:" + message.getText());
            // 每2秒消费一次
            Thread.sleep(2000);
        }

        // 7. 关闭连接
//        connection.close();
    }
}

在这里插入图片描述

四、ActiveMQ运行情况

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

it00zyq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值