ActiveMQ学习总结——(二)Queue队列模式示例

来源:互联网 发布:手机淘宝没有卖家中心 编辑:程序博客网 时间:2024/06/06 23:16

1创建一个生产者,用于生产消息

package com.jms.queue;import javax.jms.Connection;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 org.apache.activemq.ActiveMQConnectionFactory;/** * 消息生产者 * @author root * */public class AppProducer {    private static final String url="tcp://127.0.0.1:61616";//服务地址,端口默认61616    private static final String queueName="queue-test";//要创建的消息名称    public static void main(String[] args) throws JMSException {        //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(queueName);        //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("发送消息:"+i);        }        connection.close();    }}

执行后,

发送消息:0发送消息:1发送消息:2发送消息:3发送消息:4发送消息:5。。。 。。。省略发送消息:98发送消息:99

查看activemq服务上可以看到生产的消息。

编写消息的消费者(接收者)

package com.jms.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;/** * 接受/消费者 * @author root * */public class AppConsumer {    private static final String url="tcp://127.0.0.1:61616";//端口默认    private static final String queueName="queue-test";//要消费的消息名称    public static void main(String[] args) throws JMSException {        //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(queueName);        //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("接收消息:"+textMessage.getText());                } catch (JMSException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        });    }}

执行消费者

接收消息:哈哈哈哈哈:0接收消息:哈哈哈哈哈:1接收消息:哈哈哈哈哈:2。。。。。。省略接收消息:哈哈哈哈哈:98接收消息:哈哈哈哈哈:99

可以看到,生产者的所有消息都被这个消费者消费了。此时再启动一个消费者。这时候有两个消费者处于等待接受消息。一旦出现消息,那么两个消费者轮流消费。
这是再启动一次生产者,生产100个消息。
这时候一个消费偶数个

接收消息:哈哈哈哈哈:0接收消息:哈哈哈哈哈:2接收消息:哈哈哈哈哈:4接收消息:哈哈哈哈哈:6接收消息:哈哈哈哈哈:8接收消息:哈哈哈哈哈:10接收消息:哈哈哈哈哈:12接收消息:哈哈哈哈哈:14接收消息:哈哈哈哈哈:16接收消息:哈哈哈哈哈:18接收消息:哈哈哈哈哈:20接收消息:哈哈哈哈哈:22接收消息:哈哈哈哈哈:24接收消息:哈哈哈哈哈:26接收消息:哈哈哈哈哈:28接收消息:哈哈哈哈哈:30接收消息:哈哈哈哈哈:32接收消息:哈哈哈哈哈:34接收消息:哈哈哈哈哈:36接收消息:哈哈哈哈哈:38接收消息:哈哈哈哈哈:40接收消息:哈哈哈哈哈:42接收消息:哈哈哈哈哈:44接收消息:哈哈哈哈哈:46接收消息:哈哈哈哈哈:48接收消息:哈哈哈哈哈:50接收消息:哈哈哈哈哈:52接收消息:哈哈哈哈哈:54接收消息:哈哈哈哈哈:56接收消息:哈哈哈哈哈:58接收消息:哈哈哈哈哈:60接收消息:哈哈哈哈哈:62接收消息:哈哈哈哈哈:64接收消息:哈哈哈哈哈:66接收消息:哈哈哈哈哈:68接收消息:哈哈哈哈哈:70接收消息:哈哈哈哈哈:72接收消息:哈哈哈哈哈:74接收消息:哈哈哈哈哈:76接收消息:哈哈哈哈哈:78接收消息:哈哈哈哈哈:80接收消息:哈哈哈哈哈:82接收消息:哈哈哈哈哈:84接收消息:哈哈哈哈哈:86接收消息:哈哈哈哈哈:88接收消息:哈哈哈哈哈:90接收消息:哈哈哈哈哈:92接收消息:哈哈哈哈哈:94接收消息:哈哈哈哈哈:96接收消息:哈哈哈哈哈:98

另一个接受的消息情况

接收消息:哈哈哈哈哈:1接收消息:哈哈哈哈哈:3接收消息:哈哈哈哈哈:5接收消息:哈哈哈哈哈:7接收消息:哈哈哈哈哈:9接收消息:哈哈哈哈哈:11接收消息:哈哈哈哈哈:13接收消息:哈哈哈哈哈:15接收消息:哈哈哈哈哈:17接收消息:哈哈哈哈哈:19接收消息:哈哈哈哈哈:21接收消息:哈哈哈哈哈:23接收消息:哈哈哈哈哈:25接收消息:哈哈哈哈哈:27接收消息:哈哈哈哈哈:29接收消息:哈哈哈哈哈:31接收消息:哈哈哈哈哈:33接收消息:哈哈哈哈哈:35接收消息:哈哈哈哈哈:37接收消息:哈哈哈哈哈:39接收消息:哈哈哈哈哈:41接收消息:哈哈哈哈哈:43接收消息:哈哈哈哈哈:45接收消息:哈哈哈哈哈:47接收消息:哈哈哈哈哈:49接收消息:哈哈哈哈哈:51接收消息:哈哈哈哈哈:53接收消息:哈哈哈哈哈:55接收消息:哈哈哈哈哈:57接收消息:哈哈哈哈哈:59接收消息:哈哈哈哈哈:61接收消息:哈哈哈哈哈:63接收消息:哈哈哈哈哈:65接收消息:哈哈哈哈哈:67接收消息:哈哈哈哈哈:69接收消息:哈哈哈哈哈:71接收消息:哈哈哈哈哈:73接收消息:哈哈哈哈哈:75接收消息:哈哈哈哈哈:77接收消息:哈哈哈哈哈:79接收消息:哈哈哈哈哈:81接收消息:哈哈哈哈哈:83接收消息:哈哈哈哈哈:85接收消息:哈哈哈哈哈:87接收消息:哈哈哈哈哈:89接收消息:哈哈哈哈哈:91接收消息:哈哈哈哈哈:93接收消息:哈哈哈哈哈:95接收消息:哈哈哈哈哈:97接收消息:哈哈哈哈哈:99

此时想到,如果再来一个接受者会是什么情况,那么再启动一个接受者, 此时有三个同时等待者消息进行接受。在服务器上可以看到。

Name    Remote Address      Active      Slow  ID:localhost.localdomain-51411-1513823735949-0:1    tcp://127.0.0.1:43751   true    falseID:localhost.localdomain-55828-1513824112837-0:1    tcp://127.0.0.1:43753   true    falseID:localhost.localdomain-36330-1513823606217-0:1    tcp://127.0.0.1:43748   true    false

再次启动生产者生产100条消息后
分别消费情况如下
第一个:

接收消息:哈哈哈哈哈:0接收消息:哈哈哈哈哈:3接收消息:哈哈哈哈哈:6接收消息:哈哈哈哈哈:9接收消息:哈哈哈哈哈:12接收消息:哈哈哈哈哈:15接收消息:哈哈哈哈哈:18接收消息:哈哈哈哈哈:21接收消息:哈哈哈哈哈:24接收消息:哈哈哈哈哈:27接收消息:哈哈哈哈哈:30接收消息:哈哈哈哈哈:33接收消息:哈哈哈哈哈:36接收消息:哈哈哈哈哈:39接收消息:哈哈哈哈哈:42接收消息:哈哈哈哈哈:45接收消息:哈哈哈哈哈:48接收消息:哈哈哈哈哈:51接收消息:哈哈哈哈哈:54接收消息:哈哈哈哈哈:57接收消息:哈哈哈哈哈:60接收消息:哈哈哈哈哈:63接收消息:哈哈哈哈哈:66接收消息:哈哈哈哈哈:69接收消息:哈哈哈哈哈:72接收消息:哈哈哈哈哈:75接收消息:哈哈哈哈哈:78接收消息:哈哈哈哈哈:81接收消息:哈哈哈哈哈:84接收消息:哈哈哈哈哈:87接收消息:哈哈哈哈哈:90接收消息:哈哈哈哈哈:93接收消息:哈哈哈哈哈:96接收消息:哈哈哈哈哈:99

第二个:

接收消息:哈哈哈哈哈:1接收消息:哈哈哈哈哈:4接收消息:哈哈哈哈哈:7接收消息:哈哈哈哈哈:10接收消息:哈哈哈哈哈:13接收消息:哈哈哈哈哈:16接收消息:哈哈哈哈哈:19接收消息:哈哈哈哈哈:22接收消息:哈哈哈哈哈:25接收消息:哈哈哈哈哈:28接收消息:哈哈哈哈哈:31接收消息:哈哈哈哈哈:34接收消息:哈哈哈哈哈:37接收消息:哈哈哈哈哈:40接收消息:哈哈哈哈哈:43接收消息:哈哈哈哈哈:46接收消息:哈哈哈哈哈:49接收消息:哈哈哈哈哈:52接收消息:哈哈哈哈哈:55接收消息:哈哈哈哈哈:58接收消息:哈哈哈哈哈:61接收消息:哈哈哈哈哈:64接收消息:哈哈哈哈哈:67接收消息:哈哈哈哈哈:70接收消息:哈哈哈哈哈:73接收消息:哈哈哈哈哈:76接收消息:哈哈哈哈哈:79接收消息:哈哈哈哈哈:82接收消息:哈哈哈哈哈:85接收消息:哈哈哈哈哈:88接收消息:哈哈哈哈哈:91接收消息:哈哈哈哈哈:94接收消息:哈哈哈哈哈:97

第三个:

接收消息:哈哈哈哈哈:2接收消息:哈哈哈哈哈:5接收消息:哈哈哈哈哈:8接收消息:哈哈哈哈哈:11接收消息:哈哈哈哈哈:14接收消息:哈哈哈哈哈:17接收消息:哈哈哈哈哈:20接收消息:哈哈哈哈哈:23接收消息:哈哈哈哈哈:26接收消息:哈哈哈哈哈:29接收消息:哈哈哈哈哈:32接收消息:哈哈哈哈哈:35接收消息:哈哈哈哈哈:38接收消息:哈哈哈哈哈:41接收消息:哈哈哈哈哈:44接收消息:哈哈哈哈哈:47接收消息:哈哈哈哈哈:50接收消息:哈哈哈哈哈:53接收消息:哈哈哈哈哈:56接收消息:哈哈哈哈哈:59接收消息:哈哈哈哈哈:62接收消息:哈哈哈哈哈:65接收消息:哈哈哈哈哈:68接收消息:哈哈哈哈哈:71接收消息:哈哈哈哈哈:74接收消息:哈哈哈哈哈:77接收消息:哈哈哈哈哈:80接收消息:哈哈哈哈哈:83接收消息:哈哈哈哈哈:86接收消息:哈哈哈哈哈:89接收消息:哈哈哈哈哈:92接收消息:哈哈哈哈哈:95接收消息:哈哈哈哈哈:98

注意:这里的一二三并不代表执行顺序。

可以看出队列模式的消费是消费者依次轮流进行消费。

原创粉丝点击