队列模式
- 瓜分模式,生产者发送了10条消息到activeMQ服务器,多个消费者瓜分10个消息,一个消费只能被一个消费者得到
TestConsumer.java

1 package cn.how2j.queue; 2 3 import javax.jms.Connection; 4 import javax.jms.ConnectionFactory; 5 import javax.jms.Destination; 6 import javax.jms.JMSException; 7 import javax.jms.Message; 8 import javax.jms.MessageConsumer; 9 import javax.jms.MessageListener; 10 import javax.jms.Session; 11 import javax.jms.TextMessage; 12 13 import org.apache.activemq.ActiveMQConnectionFactory; 14 15 import cn.how2j.util.ActiveMQUtil; 16 import cn.hutool.core.util.RandomUtil; 17 /** 18 * 订阅者 19 * @author root 20 * 21 */ 22 public class TestConsumer { 23 //服务地址,端口默认61616 24 private static final String url="tcp://127.0.0.1:61616"; 25 //这次消费的消息名称 26 private static final String topicName="queue_style"; 27 28 //消费者有可能是多个,为了区分不同的消费者,为其创建随机名称 29 private static final String consumerName="consumer-" + RandomUtil.randomString(5); 30 public static void main(String[] args) throws JMSException { 31 //0. 先判断端口是否启动了 Active MQ 服务器 32 ActiveMQUtil.checkServer(); 33 System.out.printf("%s 消费者启动了。 %n", consumerName); 34 35 //1.创建ConnectiongFactory,绑定地址 36 ConnectionFactory factory=new ActiveMQConnectionFactory(url); 37 //2.创建Connection 38 Connection connection= factory.createConnection(); 39 //3.启动连接 40 connection.start(); 41 //4.创建会话 42 Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 43 //5.创建一个目标 (主题类型) 44 Destination destination=session.createQueue(topicName); 45 //6.创建一个消费者 46 MessageConsumer consumer=session.createConsumer(destination); 47 //7.创建一个监听器 48 consumer.setMessageListener(new MessageListener() { 49 50 public void onMessage(Message arg0) { 51 // TODO Auto-generated method stub 52 TextMessage textMessage=(TextMessage)arg0; 53 try { 54 System.out.println(consumerName +" 接收消息:"+textMessage.getText()); 55 } catch (JMSException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 60 } 61 }); 62 63 //8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态 64 //connection.close(); 65 } 66 }
TestProducer.java

1 package cn.how2j.queue; 2 3 import javax.jms.Connection; 4 import javax.jms.ConnectionFactory; 5 import javax.jms.Destination; 6 import javax.jms.JMSException; 7 import javax.jms.MessageProducer; 8 import javax.jms.Session; 9 import javax.jms.TextMessage; 10 11 import org.apache.activemq.ActiveMQConnectionFactory; 12 13 import cn.how2j.util.ActiveMQUtil; 14 15 16 public class TestProducer { 17 18 //服务地址,端口默认61616 19 private static final String url="tcp://127.0.0.1:61616"; 20 //这次发送的消息名称 21 private static final String topicName="queue_style"; 22 public static void main(String[] args) throws JMSException { 23 //0. 先判断端口是否启动了 Active MQ 服务器 24 ActiveMQUtil.checkServer(); 25 //1.创建ConnectiongFactory,绑定地址 26 ConnectionFactory factory=new ActiveMQConnectionFactory(url); 27 //2.创建Connection 28 Connection connection= factory.createConnection(); 29 //3.启动连接 30 connection.start(); 31 //4.创建会话 32 Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 33 //5.创建一个目标 (队列类型) 34 Destination destination=session.createQueue(topicName); 35 //6.创建一个生产者 36 MessageProducer producer=session.createProducer(destination); 37 38 39 for (int i = 0; i < 100; i++) { 40 //7.创建消息 41 TextMessage textMessage=session.createTextMessage("队列消息-"+i); 42 //8.发送消息 43 producer.send(textMessage); 44 System.out.println("发送:"+textMessage.getText()); 45 } 46 //7. 关闭连接 47 connection.close(); 48 } 49 }
主题模式
- 订阅模式,生产者发送一条消息,所有消费者都可得到
- 消费者要先启动,才能收到消息(订阅后才可收到消息)
- 代码和队列模式类似,只是把createQueue改为了createTopic