1.1-ActiveMQ队列(Queue)模式下消息生产与消费

ActiveMQ队列模式下消息生产与消费

使用Maven工具构建项目需要引入对应依赖:

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

消息生产者

package service.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

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

public class QueueProducer {

    /**
     * ActiveMq服务器地址
     * 支持的协议参见官网  http://activemq.apache.org/configuring-transports
     * TCP协议使用文档 http://activemq.apache.org/tcp-transport-reference
     */
    public static final String DEFAULT_BROKER_URL = "tcp://192.168.136.135:61616";

    public static final String USER_NAME = "admin";

    public static final String PWD = "admin";

    public static final String QUEUE_NAME = "test.queue";

    public void work() {
        try {
            // 1. Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USER_NAME, PWD, DEFAULT_BROKER_URL);

            // 2. Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            // 3. Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // 4.Create the destination (Topic or Queue)
            Destination destination = session.createQueue(QUEUE_NAME);

            // 5. Create a MessageProducer from the Session to the Topic or Queue  创建消息生产者
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            for (int i = 1; i <= 100; i++) {
                // 6. Create a messages  创建消息
                String text = "第" + i + "条消息" + "来自" + Thread.currentThread().getName() + " " + System.currentTimeMillis();
                TextMessage message = session.createTextMessage(text);

                // Tell the producer to send the message   生产者发送消息
                System.out.println("Sent message: " + text);
                producer.send(message);
            }
            // Clean up
            session.close();
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new QueueProducer().work();
    }

}

消息消费者

package service.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

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

public class QueueConsumer implements Runnable {

    /**
     * ActiveMq服务器地址
     * 支持的协议参见官网  http://activemq.apache.org/configuring-transports
     * TCP协议使用文档 http://activemq.apache.org/tcp-transport-reference
     */
    public static final String DEFAULT_BROKER_URL = "tcp://192.168.136.135:61616";

    public static final String USER_NAME = "admin";

    public static final String PWD = "admin";

    public static final String QUEUE_NAME = "test.queue";

    public static void main(String[] args) {
    }

    public void run() {
        try {
            // 1. Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USER_NAME, PWD, DEFAULT_BROKER_URL);

            // 2. Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            connection.setExceptionListener(new ExceptionListener() {
                public void onException(JMSException e) {
                    System.out.println("JMS Exception occured.  Shutting down client.");
                }
            });

            // 3. Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // 4.Create the destination (Topic or Queue)
            Destination destination = session.createQueue(QUEUE_NAME);

            // 5. Create a MessageConsumer from the Session to the Topic or Queue
            MessageConsumer consumer = session.createConsumer(destination);

            // 6. Wait for a message  等待接收消息(在接收到下一条消息之前或者在指定时间内)该方法会阻塞
            Message message = consumer.receive(1000);
            if (message != null) {
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    String text = textMessage.getText();
                    System.out.println(Thread.currentThread().getName() + "Received: " + text);
                } else {
                    System.out.println("Received: " + message);
                }
            } else {
                System.out.println(Thread.currentThread().getName() + "在指定时间内没有接收到消息");
            }
            consumer.close();
            session.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

消费者除了使用receive接收消息外,还可以使用消息监听方式:

// 6. Wait for a message
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    String text = null;
                    try {
                        text = textMessage.getText();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Received: " + text);
                } else {
                    System.out.println("Received: " + message);
                }
            }
        });
        // 阻塞线程,使消息消费完后才关闭连接
        System.in.read();

测试:

package service.queue;

public class Test {
    public static void main(String[] args) {
        new QueueProducer().work();
        for (int i = 0; i < 100; i++) {
            Thread consumerThread = new Thread(new QueueConsumer());
            consumerThread.setName("第" + i + "个消费者");
            consumerThread.start();
        }
    }
}

"C:\Program Files\Java\jdk1.8.0_121\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=2239:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\workspace\idea-test\activemq\activemq-demo\target\classes;D:\m2\repository\org\apache\activemq\activemq-all\5.9.0\activemq-all-5.9.0.jar" service.queue.Test
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Sent message: 第1条消息来自main 1614445679348
Sent message: 第2条消息来自main 1614445679354
Sent message: 第3条消息来自main 1614445679354
Sent message: 第4条消息来自main 1614445679355
Sent message: 第5条消息来自main 1614445679355
Sent message: 第6条消息来自main 1614445679355
Sent message: 第7条消息来自main 1614445679355
Sent message: 第8条消息来自main 1614445679355
Sent message: 第9条消息来自main 1614445679355
Sent message: 第10条消息来自main 1614445679356
Sent message: 第11条消息来自main 1614445679356
Sent message: 第12条消息来自main 1614445679356
Sent message: 第13条消息来自main 1614445679356
Sent message: 第14条消息来自main 1614445679356
Sent message: 第15条消息来自main 1614445679356
Sent message: 第16条消息来自main 1614445679356
Sent message: 第17条消息来自main 1614445679356
Sent message: 第18条消息来自main 1614445679357
Sent message: 第19条消息来自main 1614445679357
Sent message: 第20条消息来自main 1614445679357
Sent message: 第21条消息来自main 1614445679357
Sent message: 第22条消息来自main 1614445679357
Sent message: 第23条消息来自main 1614445679357
Sent message: 第24条消息来自main 1614445679357
Sent message: 第25条消息来自main 1614445679357
Sent message: 第26条消息来自main 1614445679357
Sent message: 第27条消息来自main 1614445679358
Sent message: 第28条消息来自main 1614445679358
Sent message: 第29条消息来自main 1614445679358
Sent message: 第30条消息来自main 1614445679358
Sent message: 第31条消息来自main 1614445679358
Sent message: 第32条消息来自main 1614445679358
Sent message: 第33条消息来自main 1614445679358
Sent message: 第34条消息来自main 1614445679359
Sent message: 第35条消息来自main 1614445679359
Sent message: 第36条消息来自main 1614445679359
Sent message: 第37条消息来自main 1614445679359
Sent message: 第38条消息来自main 1614445679359
Sent message: 第39条消息来自main 1614445679359
Sent message: 第40条消息来自main 1614445679359
Sent message: 第41条消息来自main 1614445679359
Sent message: 第42条消息来自main 1614445679361
Sent message: 第43条消息来自main 1614445679361
Sent message: 第44条消息来自main 1614445679361
Sent message: 第45条消息来自main 1614445679362
Sent message: 第46条消息来自main 1614445679362
Sent message: 第47条消息来自main 1614445679362
Sent message: 第48条消息来自main 1614445679362
Sent message: 第49条消息来自main 1614445679362
Sent message: 第50条消息来自main 1614445679362
Sent message: 第51条消息来自main 1614445679362
Sent message: 第52条消息来自main 1614445679362
Sent message: 第53条消息来自main 1614445679363
Sent message: 第54条消息来自main 1614445679363
Sent message: 第55条消息来自main 1614445679363
Sent message: 第56条消息来自main 1614445679363
Sent message: 第57条消息来自main 1614445679363
Sent message: 第58条消息来自main 1614445679363
Sent message: 第59条消息来自main 1614445679363
Sent message: 第60条消息来自main 1614445679364
Sent message: 第61条消息来自main 1614445679364
Sent message: 第62条消息来自main 1614445679364
Sent message: 第63条消息来自main 1614445679364
Sent message: 第64条消息来自main 1614445679364
Sent message: 第65条消息来自main 1614445679365
Sent message: 第66条消息来自main 1614445679365
Sent message: 第67条消息来自main 1614445679365
Sent message: 第68条消息来自main 1614445679365
Sent message: 第69条消息来自main 1614445679365
Sent message: 第70条消息来自main 1614445679365
Sent message: 第71条消息来自main 1614445679365
Sent message: 第72条消息来自main 1614445679365
Sent message: 第73条消息来自main 1614445679365
Sent message: 第74条消息来自main 1614445679365
Sent message: 第75条消息来自main 1614445679366
Sent message: 第76条消息来自main 1614445679366
Sent message: 第77条消息来自main 1614445679366
Sent message: 第78条消息来自main 1614445679366
Sent message: 第79条消息来自main 1614445679366
Sent message: 第80条消息来自main 1614445679366
Sent message: 第81条消息来自main 1614445679366
Sent message: 第82条消息来自main 1614445679366
Sent message: 第83条消息来自main 1614445679366
Sent message: 第84条消息来自main 1614445679366
Sent message: 第85条消息来自main 1614445679366
Sent message: 第86条消息来自main 1614445679367
Sent message: 第87条消息来自main 1614445679367
Sent message: 第88条消息来自main 1614445679367
Sent message: 第89条消息来自main 1614445679367
Sent message: 第90条消息来自main 1614445679367
Sent message: 第91条消息来自main 1614445679367
Sent message: 第92条消息来自main 1614445679367
Sent message: 第93条消息来自main 1614445679367
Sent message: 第94条消息来自main 1614445679368
Sent message: 第95条消息来自main 1614445679368
Sent message: 第96条消息来自main 1614445679368
Sent message: 第97条消息来自main 1614445679368
Sent message: 第98条消息来自main 1614445679368
Sent message: 第99条消息来自main 1614445679368
Sent message: 第100条消息来自main 1614445679368
第80个消费者Received: 第14条消息来自main 1614445679356
第57个消费者Received: 第10条消息来自main 1614445679356
第45个消费者Received: 第5条消息来自main 1614445679355
第62个消费者Received: 第16条消息来自main 1614445679356
第58个消费者Received: 第12条消息来自main 1614445679356
第61个消费者Received: 第11条消息来自main 1614445679356
第49个消费者Received: 第4条消息来自main 1614445679355
第59个消费者Received: 第3条消息来自main 1614445679354
第83个消费者Received: 第19条消息来自main 1614445679357
第73个消费者Received: 第17条消息来自main 1614445679356
第14个消费者Received: 第1条消息来自main 1614445679348
第70个消费者Received: 第13条消息来自main 1614445679356
第50个消费者Received: 第6条消息来自main 1614445679355
第46个消费者Received: 第2条消息来自main 1614445679354
第54个消费者Received: 第9条消息来自main 1614445679355
第74个消费者Received: 第18条消息来自main 1614445679357
第53个消费者Received: 第7条消息来自main 1614445679355
第68个消费者Received: 第8条消息来自main 1614445679355
第69个消费者Received: 第15条消息来自main 1614445679356
第56个消费者Received: 第23条消息来自main 1614445679357
第13个消费者Received: 第25条消息来自main 1614445679357
第60个消费者Received: 第22条消息来自main 1614445679357
第75个消费者Received: 第21条消息来自main 1614445679357
第79个消费者Received: 第20条消息来自main 1614445679357
第76个消费者Received: 第24条消息来自main 1614445679357
第84个消费者Received: 第26条消息来自main 1614445679357
第55个消费者Received: 第27条消息来自main 1614445679358
第48个消费者Received: 第28条消息来自main 1614445679358
第52个消费者Received: 第29条消息来自main 1614445679358
第42个消费者Received: 第31条消息来自main 1614445679358
第38个消费者Received: 第32条消息来自main 1614445679358
第51个消费者Received: 第30条消息来自main 1614445679358
第44个消费者Received: 第33条消息来自main 1614445679358
第34个消费者Received: 第34条消息来自main 1614445679359
第37个消费者Received: 第35条消息来自main 1614445679359
第77个消费者Received: 第44条消息来自main 1614445679361
第82个消费者Received: 第41条消息来自main 1614445679359
第81个消费者Received: 第43条消息来自main 1614445679361
第85个消费者Received: 第45条消息来自main 1614445679362
第88个消费者Received: 第40条消息来自main 1614445679359
第86个消费者Received: 第46条消息来自main 1614445679362
第92个消费者Received: 第36条消息来自main 1614445679359
第95个消费者Received: 第39条消息来自main 1614445679359
第87个消费者Received: 第38条消息来自main 1614445679359
第96个消费者Received: 第37条消息来自main 1614445679359
第90个消费者Received: 第47条消息来自main 1614445679362
第78个消费者Received: 第42条消息来自main 1614445679361
第40个消费者Received: 第52条消息来自main 1614445679362
第94个消费者Received: 第50条消息来自main 1614445679362
第33个消费者Received: 第51条消息来自main 1614445679362
第98个消费者Received: 第48条消息来自main 1614445679362
第3个消费者Received: 第53条消息来自main 1614445679363
第89个消费者Received: 第49条消息来自main 1614445679362
第21个消费者Received: 第89条消息来自main 1614445679367
第17个消费者Received: 第85条消息来自main 1614445679366
第22个消费者Received: 第88条消息来自main 1614445679367
第18个消费者Received: 第81条消息来自main 1614445679366
第10个消费者Received: 第92条消息来自main 1614445679367
第2个消费者Received: 第74条消息来自main 1614445679365
第29个消费者Received: 第72条消息来自main 1614445679365
第9个消费者Received: 第69条消息来自main 1614445679365
第5个消费者Received: 第65条消息来自main 1614445679365
第93个消费者Received: 第67条消息来自main 1614445679365
第66个消费者Received: 第61条消息来自main 1614445679364
第65个消费者Received: 第57条消息来自main 1614445679363
第1个消费者Received: 第66条消息来自main 1614445679365
第97个消费者Received: 第68条消息来自main 1614445679365
第30个消费者Received: 第60条消息来自main 1614445679364
第41个消费者Received: 第58条消息来自main 1614445679363
第26个消费者Received: 第96条消息来自main 1614445679368
第6个消费者Received: 第94条消息来自main 1614445679368
第25个消费者Received: 第98条消息来自main 1614445679368
第47个消费者Received: 第87条消息来自main 1614445679367
第19个消费者Received: 第80条消息来自main 1614445679366
第23个消费者Received: 第90条消息来自main 1614445679367
第24个消费者Received: 第91条消息来自main 1614445679367
第31个消费者Received: 第84条消息来自main 1614445679366
第7个消费者Received: 第83条消息来自main 1614445679366
第32个消费者Received: 第79条消息来自main 1614445679366
第27个消费者Received: 第78条消息来自main 1614445679366
第11个消费者Received: 第82条消息来自main 1614445679366
第15个消费者Received: 第76条消息来自main 1614445679366
第16个消费者Received: 第75条消息来自main 1614445679366
第12个消费者Received: 第77条消息来自main 1614445679366
第8个消费者Received: 第73条消息来自main 1614445679365
第67个消费者Received: 第71条消息来自main 1614445679365
第64个消费者Received: 第63条消息来自main 1614445679364
第91个消费者Received: 第59条消息来自main 1614445679363
第35个消费者Received: 第62条消息来自main 1614445679364
第20个消费者Received: 第86条消息来自main 1614445679367
第36个消费者Received: 第97条消息来自main 1614445679368
第0个消费者Received: 第54条消息来自main 1614445679363
第99个消费者Received: 第56条消息来自main 1614445679363
第4个消费者Received: 第55条消息来自main 1614445679363
第39个消费者Received: 第64条消息来自main 1614445679364
第72个消费者Received: 第70条消息来自main 1614445679365
第71个消费者Received: 第99条消息来自main 1614445679368
第28个消费者Received: 第95条消息来自main 1614445679368
第43个消费者Received: 第93条消息来自main 1614445679367
第63个消费者在指定时间内没有接收到消息

Process finished with exit code 0


由此可见,在队列模式下,多个消费者会分摊消费队列中的多条消息

官网示例:
http://activemq.apache.org/version-5-hello-world

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值