activemq之topic主题模式(代码片段)

xyhero xyhero     2022-12-19     510

关键词:

开发环境
我们使用的是ActiveMQ 5.11.1 Release的Windows版,官网最新版是ActiveMQ 5.12.0 Release,大家可以自行下载,下载地址。
需要注意的是,开发时候,要将apache-activemq-5.11.1-bin.zip解压缩后里面的activemq-all-5.11.1.jar包加入到classpath下面,这个包包含了所有jms接口api的实现。

搭建开发环境
建立项目,我们只需要建立一个java项目就可以了,导入jar包,项目截图:

技术分享图片

1、编写生产者

package com.activemq.producer.topic;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer 
    
    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址
    private static final int SENDNUM=10; // 发送的消息数量

    public static void main(String[] args) 
        ActiveMQConnectionFactory connectionFactory; // 连接工厂
        Connection connection = null; // 连接
        Session session; // 会话 接受或者发送消息的线程
        Destination destination; // 消息的目的地
        MessageProducer messageProducer; // 消息生产者
        
        // 实例化连接工厂
        connectionFactory=new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
        
        try 
            connection=connectionFactory.createConnection(); // 通过连接工厂获取连接
            connection.start(); // 启动连接
            session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 创建Session
            destination=session.createTopic("FirstTopic1"); // 创建消息队列
            messageProducer=session.createProducer(destination); // 创建消息生产者
            sendMessage(session, messageProducer); // 发送消息
            session.commit();
         catch (Exception e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
         finally
            if(connection!=null)
                try 
                    connection.close();
                 catch (JMSException e) 
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                
            
        

    
    
    /**
     * 发送消息
     * @param session
     * @param messageProducer
     * @throws Exception
     */
    public static void sendMessage(Session session,MessageProducer messageProducer)throws Exception
        for(int i=0;i<JMSProducer.SENDNUM;i++)
            TextMessage message=session.createTextMessage("ActiveMQ 发送的消息"+i);
            System.out.println("发送消息:"+"ActiveMQ 发送的消息"+i);
            messageProducer.send(message);
        
    

2、编写监听器
为了模拟两个客户端接收消息,我们写两个监听器

 

public class Listener implements MessageListener 

    @Override
    public void onMessage(Message message) 
        // TODO Auto-generated method stub
        try 
            System.out.println("订阅者一收到的消息:"+((TextMessage)message).getText());
         catch (JMSException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

public class Listener2 implements MessageListener 

    @Override
    public void onMessage(Message message) 
        // TODO Auto-generated method stub
        try 
            System.out.println("订阅者二收到的消息:"+((TextMessage)message).getText());
         catch (JMSException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

3、编写消费者
connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
用户名和密码这两个参数可以不写

 技术分享图片

package com.activemq.consumer.topic;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.activemq.listener.Listener;

public class JMSConsumer 
    
    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址

    public static void main(String[] args) 
        //创建消息工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(JMSConsumer.BROKEURL);
        Connection connection = null;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;
        try 
            //创建连接
            connection= factory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            // 创建连接的消息队列
            destination=session.createTopic("FirstTopic1");
            // 创建消息消费者
            messageConsumer=session.createConsumer(destination);
            // 注册消息监听
            messageConsumer.setMessageListener(new Listener());
         catch (JMSException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

package com.activemq.consumer.topic;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.activemq.listener.Listener2;

public class JMSConsumer2 
    
    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址

    public static void main(String[] args) 
        //创建消息工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(JMSConsumer2.BROKEURL);
        Connection connection = null;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;
        try 
            //创建连接
            connection= factory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            // 创建连接的消息队列
            destination=session.createTopic("FirstTopic1");
            // 创建消息消费者
            messageConsumer=session.createConsumer(destination);
            // 注册消息监听
            messageConsumer.setMessageListener(new Listener2());
         catch (JMSException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

4、启动消费者和生产者
先启动消费者,再启动生产者,顺序不可颠倒。

 技术分享图片

 








activemq-初识mq--主题topic(代码片段)

1.导入依赖<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.15.9</version></dependency>2.编写生产者代码编写生产者步骤获取连接工 查看详情

activemq-初识mq--主题topic(代码片段)

1.导入依赖<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.15.9</version></dependency>2.编写生产者代码编写生产者步骤获取连接工 查看详情

activemq之队列和主题发布订阅实例(代码片段)

...据流 增加maven依赖<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.10.0</version></dependen 查看详情

springboot集成rabbitmq之主题模式(topics)(代码片段)

springBoot集成rabbitmq之主题模式(topics)springBoot整合rabbitmq的例子:https://blog.csdn.net/weixin_45730866/article/details/128971917,建议先看。consumer服务配置类importorg.springframework.amqp.core.Binding;importorg.springframework.amqp.core.BindingBuilder;importorg.sprin... 查看详情

springboot整合activemq之topic,不懂也得懂了吧

前言?今天和大家分享springboot整合activeMq之topic(主题)--发布/订阅模式,类似微信公众号,我们关注公共就可以收到消息,topic需要消费者先订阅才能收到消息,如果没有消费者订阅,生产者产生的消息就是废消息(发布/订阅模式,... 查看详情

activemq-初识mq--主题topic(代码片段)

1.导入依赖<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.15.9</version></dependency>2.编写生产者代码编写生产者步骤获取连接工厂获取连接connection获取session创建目的地q... 查看详情

rabbitmq之topic交换器模式下开发(代码片段)

Topic交换器,即主题模式,进行规则匹配。一、Provider配置文件1spring.application.name=provider2spring.rabbitmq.host=192.168.50.303spring.rabbitmq.port=56724spring.rabbitmq.username=rabbit5spring.rabbitmq.password=rabbit6#设置交换 查看详情

activemq入门系列三:发布/订阅模式(代码片段)

在上一篇《ActiveMQ入门系列二:入门代码实例(点对点模式)》中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub&Sub),详细介绍了点对点模式并用代码实例进行说明,今天就介绍下发布/订阅模式。一、... 查看详情

springboot整合rabbitmq(topic主题模式)(代码片段)

在direct模式基础上改,但是此次使用注解方式消费者修改对应的consumer,用注解方式定义交换机和队列的关系@Service@RabbitListener(bindings=@QueueBinding(value=@Queue(value="duanxin.topic.queue",durable="true",autoDelete="false"),exchange=@Exchange(v 查看详情

activemq--模式(队列模式/主题模式)(代码片段)

...列模式/主题模式pom.xml<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.15.9</version></dependency> 队列模式,其实就是分食模式。  比如生产方发了10条消息到activeMQ... 查看详情

rabbitmq:topics主题/通配符模式(代码片段)

✨RabbitMQ:Topics主题/通配符模式1.基本介绍2.生产者3.消费者4.测试📃个人主页:不断前进的皮卡丘🌞博客描述:梦想也许遥不可及,但重要的是追梦的过程,用博客记录自己的成长,记录自己一步一步向上攀... 查看详情

activemq的两种消息模式,主题队列

1、开发的模式流程如下:2、队列模式Queue如果生产者产生了100条消息,那么两个消费同时在的话,会分工合作来接收这100条消息。就是每个消费者接收到50条来处理。3、主题模式topic如果生产者产生了100条消息,消费者在还没有... 查看详情

springboot2.5.6集成rabbitmq,实现topic主题模式(代码片段)

1.application.ymlserver:port:8184spring:application:name:rabbitmq-demorabbitmq:host:127.0.0.1#ip地址port:5672username:admin#连接账号password:123456#连接密码template:retry:enabled:true#开启失败重试initial-interval:100 查看详情

activemq使用(代码片段)

 一、Windows安装ActiveMQ1.下载解压2.启动服务二、Linux安装ActiveMQ1.下载解压2.启动访问三、队列模式1.创建maven项目2.生产者3.消费者四、主题模式1.修改队列名字2.修改主题五、Spring集成ActiveMQ1.pom.xml2.消息接口3.... 查看详情

rabbitmq之主题模糊匹配(代码片段)

topic类型的交换器允许在RabbitMQ中使用模糊匹配来绑定自己感兴趣的信息通过匹配交换器,我们可以配置更灵活的消息系统 匹配交换器的匹配符*(星号)表示一个单词#(井号)表示零个或者多个单词 这次的例子中,我们... 查看详情

activemq概述和安装(代码片段)

概述面向消息的中间件(MOM),是指利用高效可靠的消息传递机制与平台无关的数据交流,并基于数据通信来进行分布式系统的集成大致过程是这样的:发送者把消息发送给消息服务器,消息服务器将消息存放在若干队列/主题topi... 查看详情

03activemq的topic模式

参考技术A上一篇文章我们已经看过activemq的点对点模式,本文将阐述使用java完成发布订阅值topic模式的应用,所谓topic模式,其实就是广播。https://www.jianshu.com/p/042073b7710b在pom.xml中加入以下依赖:启动两次,查看管理界面。依次... 查看详情

rabbitmq:订阅者模式之主体模式(topic)(代码片段)

主体模式和路由模式很像路由模式是精确匹配主体模式是模糊匹配  依然先通过管理后台添加一个交换机.生产者publicclassProducerprivateconststringExchangeName="test_exchange_topic";publicstaticvoidSend()//获取一个连接IConnectionconnection=Connect... 查看详情