ActiveMQ应用笔记二:Producer与Consumer模板代码

接上一篇文章。

ActiveMQ成功部署运行后,就可以开发producer和consumer了。
在eclipse中新建Java项目,将ActiveMQ安装目录下lib中的jar包都引入项目,然后就可以编写producer和consumer。
注意:如果是测试代码,为了真实性,最好把producer和consumer分开写,而不是写在一个类中。

1.Producer:
package com.liyang.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactor y;

public class Sender_lion {
      public static void main(String[] args) {
            ConnectionFactory connectionFactory;
            Connection connection = null;
            Session session;
            Destination destination;
            MessageProducer producer;
        //参数为用户名,密码,MQ的url
        connectionFactory = new ActiveMQConnectionFactory(
                "smechina",
                "fulong",
                "tcp://localhost:61616");
            try {
                  connection = connectionFactory.createConnection();
                  connection.start();
                  session = connection.createSession(Boolean.TRUE,
                              Session.AUTO_ACKNOWLEDGE);
                   //*******注意:此处需修改为topic才能支持1对多发信息
           //destination = session.createQueue("FirstQueue");
           destination = session.createTopic("FirstTopic");
                   // 得到消息生成者【发送者】
           producer = session.createProducer(destination);
           // ********设置是否持久化
           //producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
           producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                  //uandp是需要发送的消息
                  String uandp = "liyang:fulong";
                  sendMessage(session, producer,uandp);
                  session.commit();
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  try {
                        if (null != connection)
                              connection.close();
                  } catch (Throwable ignore) {
                  }
            }
      }

      public static void sendMessage(Session session, MessageProducer producer,String passport)
                  throws Exception {
                  // 发送10条消息
                  for (int i = 0; i < 9; i++) {
                   TextMessage message = session.createTextMessage("账号"+i+":" + passport);
                        //System.out.println("账号是:" + passport);
                  //可设置属性过滤
                  //message.setStringProperty("receiver", "A");
                         producer.send(message);
}
      }
}

1.Consumer:
package com.liyang.activemq;

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 javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactor y;

public class Receiver_lion{
      public static void main(String[] args) {
            ConnectionFactory connectionFactory;
            Connection connection = null;
            Session session;
            Destination destination;
            MessageConsumer consumer;
        //参数为用户名,密码,MQ的url
        connectionFactory = new ActiveMQConnectionFactory(
        "smeguangdong",
                "fulong",
                "tcp://localhost:61616");
            try {
                  connection = connectionFactory.createConnection();
            //---持久化订阅才使用下语句!!!!参数是clientid,设置该参数后MQ会记住该ID---
            connection.setClientID("guangdong"); 
                  connection.start();
                  session = connection.createSession(Boolean.FALSE,
                              Session.AUTO_ACKNOWLEDGE);
                   //*******注意:此处需修改为topic才能支持1对多发信息
            //destination = session.createQueue("FirstQueue");
            destination = session.createTopic("FirstTopic");
            //创建普通消费者【接收者】,使用属性过滤
            //consumer = session.createConsumer(destination,"receiver = 'A'");
            //--------持久化订阅!!!!------第二个参数是client名
            consumer = session.createDurableSubscriber((Topic)destination,"guangdong");
                  
                   MessageListener ml = new MessageListener(){ 
            @Override 
            //设置监听器
            public void onMessage(Message m) { 
             TextMessage textMsg = (TextMessage) m; 
             try{
              System.out.println("收到消息:" + textMsg.getText()); 
             } catch (JMSException e) { 
              e.printStackTrace(); 
             } 
            } 
            };
            consumer.setMessageListener(ml);
                  while (true) {
}
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  try {
                        if (null != connection)
                              connection.close();
                  } catch (Throwable ignore) {
                  }
            }
      }
}

 

你可能感兴趣的:(activemq)