天天看点

消息中间件学习笔记--ActiveMQ(二、队列模式)

队列模式

ActiveMQ 有两种模式,分别是队列模式和主题模式。

队列模式,其实就是分食模式。 比如生产方发了 10条消息到 activeMQ 服务器, 而此时有多个 消费方,那么这些消费方就会瓜分这些10条消息,一条消息只会被一个消费方得到。

主题模式,就是订阅模式。 比如生产方发了10条消息,而此时有多个消费方,那么多个消费方都能得到这 10条消息,就如同订阅公众号那样。

创建项目

创建一个maven项目ActiiveMQDemo。

pom.xml

添加activemq和hutool的依赖。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>edu.hpu</groupId>
  <artifactId>ActiveMQDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ActiveMQDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
   <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.15.9</version>
    </dependency> 
      <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>4.3.1</version>
    </dependency>

  </dependencies>
</project>      

工具类

ActiveMQUtil类,写一个checkServer方法,用于判断ActiveMQ服务是否启动。

package edu.hpu.util;

import javax.swing.JOptionPane;

import cn.hutool.core.util.NetUtil;

public class ActiveMQUtil {
   public static void checkServer() {
       if(NetUtil.isUsableLocalPort(8161)) {
           JOptionPane.showMessageDialog(null, "ActiveMQ 服务器未启动 ");
           System.exit(1);
       }
   }
}
      

生产者类

TestProductor:

package edu.hpu.queue;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Connection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.transport.stomp.Stomp.Headers.Connect;

import edu.hpu.util.ActiveMQUtil;

public class TestProducter {
    //服务地址,默认端口号61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次发送的消息名称
    private static final String topicName="queue_style";
    public static void main(String[] args) throws Exception {
        //0、先判断是启动了ActiveMQ服务器
        ActiveMQUtil.checkServer();
        //1、创建ConnectionFactory,绑定地址
        ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url);
        //2、创建Connection
        Connection connection=connectionFactory.createConnection();
        //3、启动连接
        connection.start();
        //4、创建会话
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、创建一个目标(队列类型)
        Destination destination=session.createQueue(topicName);
        //6、创建一个生产者
        MessageProducer producer=session.createProducer(destination);
        
        for (int i = 0; i <100; i++) {
            //7、创建消息
            TextMessage textMessage=session.createTextMessage("队列消息--"+i);
            //8、发送消息
            producer.send(textMessage);
            System.out.println("生产者发送消息--"+textMessage.getText());
        }
        //9、关闭连接
        connection.close();
    }
}      

消费者类

TestConsumer:

package edu.hpu.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import cn.hutool.core.util.RandomUtil;
import edu.hpu.util.ActiveMQUtil;

/*
 * @订阅者
 */
public class TestConsumer {
    //服务地址,端口默认61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次消费的消息名称
    private static final String topicName="queue_style";
   //消费者可能会有多个,为了区分不同的消费者,为其创建随机名称
    private static final String consumerName="consumer-" + RandomUtil.randomString(5);
    
    public static void main(String[] args) throws JMSException {
        //0、先判断端口是否启动了ActiveMQ服务
        ActiveMQUtil.checkServer();
        System.out.printf("%s 消费者启动了。 %n", consumerName);
        //1.创建ConnectiongFactory,绑定地址
        ConnectionFactory factory=new ActiveMQConnectionFactory(url);
        //2.创建Connection
        Connection connection= factory.createConnection();
        //3.启动连接
        connection.start();
        //4.创建会话
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建一个目标 (队列类型)
        Destination destination=session.createQueue(topicName);
        //6.创建一个消费者
        MessageConsumer consumer=session.createConsumer(destination);
        //7、创建一个监听器
        consumer.setMessageListener(new MessageListener() {
            
            public void onMessage(Message arg0) {
                // TODO Auto-generated method stub
                TextMessage textMessage=(TextMessage)arg0;
                try {
                    System.out.println(consumerName +" 接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}      

启动

启动消费者类两次,产生两个消费者,启动生产者类,产生一个生产者,这个生产者会生产100条消息,而这两个消费者会分食这100条消息。

消息中间件学习笔记--ActiveMQ(二、队列模式)
消息中间件学习笔记--ActiveMQ(二、队列模式)

访问地址:

http://127.0.0.1:8161/admin/queues.jsp

就可以看到 刚才的消息处理情况。

queue_style 是在代码中定义的消息名称。

number Of Consumers 表示有2个消费者。

Messages Enqueued:表示收到了 100 个消息。

Messages Dequeued:表示消费了 100 个消息。

消息中间件学习笔记--ActiveMQ(二、队列模式)

参考:

【1】、

http://how2j.cn/k/message/message-activemq-queue/2026.html#nowhere