activemq知识总结

queue可以有多个consumer,但一条消息只能被一个consumer收到

topic也可以有多个consumer,但一条消息可以被所有订阅该消息的consumer收到

 

topic的多个consumer之间用clientid区分。

 

activemq客户端只需要导入activemq.jar包即可

发送者示例:

public class ActivemqPublisherTest {

 /**
  * @param args
  * @throws JMSException
  */
 public static void main(String[] args) throws JMSException {
  ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://xxx.xxx.xxx.xxx:61616");

  Connection connection = factory.createConnection();

  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Topic topic = session.createTopic("product_change");

  MessageProducer producer = session.createProducer(topic);

  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

  
  MapMessage mm = session.createMapMessage();
  //mm.setString("channelid", "0008");
  mm.setString("topicid", "294S0008");
  
  producer.send(mm);


  

  session.close();

  connection.stop();

  connection.close();

 }

}

 

接收者示例:

public class ActivemqConsumerTest {

 public static void main(String[] args) throws JMSException {

  ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://123.126.62.145:61616");

  Connection connection = factory.createConnection();

  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Topic topic = session.createTopic("product_change");

  MessageConsumer consumer = session.createConsumer(topic);

  consumer.setMessageListener(new MessageListener() {

   public void onMessage(Message message) {

    //TextMessage tm = (TextMessage) message;
    MapMessage m = (MapMessage) message;

    try {
     System.out.println("Received message: " + message);
     System.out.println("Received message: " + m.getString("topicid"));

    } catch (Exception e) {

     e.printStackTrace();

    }

   }

  });

  //  session.close();
  //
  //  connection.stop();
  //
  //  connection.close();

 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值