Maven使用ActiveMQ

1. ActiveMQ JMS入门案例

环境配置

  • 官网地址: https://activemq.apache.org/
  • 下载成功后解压,进入bin目录执行 ./active start即可
  • 访问web页面, 服务器地址:8161 用户名密码均为 admin

1.1 环境准备

  • pom文件引入坐标依赖
		<dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.14.0</version>
        </dependency>

1.2 JMS-点对点模式发送消息

在这里插入图片描述

  • 后台代码
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;

import javax.jms.*;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 13:46
 */
public class MQProvider {

    /*
    * 模式: 点对点模式
    * 需求: 发送消息
    * */

    @Test
    public void sendMessage() throws Exception{
        // 创建连接工厂  对象,指定连接地址 ,协议,ip,通信端口
        String brokerUrl = "tcp://192.168.25.120:61616";
        ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
        // 从工厂中获取连接对象
        Connection connection = cf.createConnection();
        // 开启连接
        connection.start();
        // 从连接中获取session session存储回话数据
        // 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
        // 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 设置消息发送的目标地 在acticeMQ消息服务器开辟空间并起名字 ningning
        Queue ningning = session.createQueue("ningning");

        // 指定消息发送者 并告知空间名称
        MessageProducer producer = session.createProducer(ningning);
        // 创建消息对象封装消息
        TextMessage message = new ActiveMQTextMessage();
        message.setText("宁宁小可爱");

        // 发送消息
        producer.send(message);
        // 关闭资源
        producer.close();
        session.close();
        connection.close();

    }
}

  • 可以看到消息发送成功

在这里插入图片描述

1.3 JMS-点对点模式接收消息

点对点模式接收消息

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;

import javax.jms.*;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 14:06
 */
public class ActiveMqConsumer {
    /*
    * 模式: 点对点
    * 需求: 接收消息
    * */

    @Test
    public void recvMessage() throws Exception{
        // 创建连接工厂  对象,指定连接地址 ,协议,ip,通信端口
        String brokerUrl = "tcp://192.168.25.120:61616";
        ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
        // 从工厂中获取连接对象
        Connection connection = cf.createConnection();
        // 开启连接
        connection.start();
        // 从连接中获取session session存储回话数据
        // 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
        // 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 指定消息空间
        Queue ningning = session.createQueue("ningning");

        // 指定消息消费者 并告知空间名称
        MessageConsumer consumer = session.createConsumer(ningning);

        // 接收消息
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                // 获取消息
                if (message instanceof TextMessage){
                    TextMessage msg = (TextMessage) message;
                    try {
                        String text = msg.getText();
                        System.out.println("接收消息: "+text);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        // 关闭资源
        consumer.close();
        session.close();
        connection.close();

    }
}

结果展示

在这里插入图片描述

1.4 JMS-发布订阅模式-发送消息

在这里插入图片描述

  • 发布订阅模式发送消息
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;

import javax.jms.*;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 13:46
 */
public class TopicMQProvider {

    /*
    * 模式: 发布订阅模式
    * 需求: 发送消息
    * */

    @Test
    public void sendMessage() throws Exception{
        // 创建连接工厂  对象,指定连接地址 ,协议,ip,通信端口
        String brokerUrl = "tcp://192.168.25.120:61616";
        ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
        // 从工厂中获取连接对象
        Connection connection = cf.createConnection();
        // 开启连接
        connection.start();
        // 从连接中获取session session存储回话数据
        // 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
        // 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 设置消息发送的目标地 在acticeMQ消息服务器开辟空间并起名字 ningning
        Topic ningning = session.createTopic("ningning");

        // 指定消息发送者 并告知空间名称
        MessageProducer producer = session.createProducer(ningning);
        // 创建消息对象封装消息
        TextMessage message = new ActiveMQTextMessage();
        message.setText("宁宁小可爱,星河滚烫,你是人间理想");

        // 发送消息
        producer.send(message);
        // 关闭资源
        producer.close();
        session.close();
        connection.close();

    }
}

  • 结果展示

在这里插入图片描述

1.5 JMS-发布订阅模式-接收消息

  • 后台代码
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import javax.jms.*;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 14:06
 */
public class TopicActiveMqConsumer {
    /*
    * 模式: 发布订阅
    * 需求: 接收消息
    * */

    @Test
    public void recvMessage() throws Exception{
        // 创建连接工厂  对象,指定连接地址 ,协议,ip,通信端口
        String brokerUrl = "tcp://192.168.25.120:61616";
        ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
        // 从工厂中获取连接对象
        Connection connection = cf.createConnection();
        // 开启连接
        connection.start();
        // 从连接中获取session session存储回话数据
        // 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
        // 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 指定消息空间 Topic
        Topic ningning = session.createTopic("ningning");

        // 指定消息消费者 并告知空间名称
        MessageConsumer consumer = session.createConsumer(ningning);

        // 接收消息
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                // 获取消息
                if (message instanceof TextMessage){
                    TextMessage msg = (TextMessage) message;
                    try {
                        String text = msg.getText();
                        System.out.println("接收消息: "+text);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        // 阻塞
        System.in.read();
        // 关闭资源
        consumer.close();
        session.close();
        connection.close();

    }
}

  • 结果展示

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叫我三胖哥哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值