栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Maven使用ActiveMQ

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Maven使用ActiveMQ

文章目录
    • 1. ActiveMQ JMS入门案例
      • 1.1 环境准备
      • 1.2 JMS-点对点模式发送消息
      • 1.3 JMS-点对点模式接收消息
      • 1.4 JMS-发布订阅模式-发送消息
      • 1.5 JMS-发布订阅模式-接收消息

1. ActiveMQ JMS入门案例

环境配置

  • 官网地址: https://activemq.apache.org/
  • 下载成功后解压,进入bin目录执行 ./active start即可
  • 访问web页面, 服务器地址:8161 用户名密码均为 admin
1.1 环境准备
  • pom文件引入坐标依赖
		
            org.apache.activemq
            activemq-all
            5.14.0
        
1.2 JMS-点对点模式发送消息

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

import javax.jms.*;


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.*;


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.*;


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.*;


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();

    }
}

  • 结果展示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919866.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号