activeMQ之点对点(PTP),监听器模式

监听器模式

Consumer
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


public class Consumer implements MessageListener {

  private String user = ActiveMQConnection.DEFAULT_USER;     //默认 用户
  private String password = ActiveMQConnection.DEFAULT_PASSWORD;    //默认 密码

  private String url = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的是localhost:8080
  private String subject = "DEMO.LISTENER"; //消息目的地名称

  private Destination destination = null;  //在点对点(PTP)消息传递域中,目的地被成为队列(queue)


  private Topic topic = null;//在发布/订阅(PUB/SUB)消息传递域中,目的地被成为主题(topic)。


  private Connection connection = null;  //初始化 一个JMS客户端到JMS Provider的连接


  private Session session = null;     //初始化  一个接受消息的进程


  private MessageConsumer consumer = null;   //初始化 消息消费者


  // 初始化

  private void initialize() throws JMSException, Exception {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
      user, password, url);
    connection = connectionFactory.createConnection();
    //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式)
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //PTP消息方式   目的地被成为队列(queue)
    destination = session.createQueue(subject);
    consumer = session.createConsumer(destination);
  }


  // 消费消息
  public void start() throws Exception {
    initialize();
    connection.start();
    System.out.println("Consumer:->Begin listening...");
    consumer.setMessageListener(this);
  }


  // 关闭连接

  public void close() throws JMSException {
    System.out.println("Consumer:->Closing connection");
    if (consumer != null)
      consumer.close();
    if (session != null)
      session.close();
    if (connection != null)
      connection.close();
  }


  // 消息处理函数
  public void onMessage(Message message) {
    try {
      if (message instanceof TextMessage) {
        TextMessage txtMsg = (TextMessage) message;
        String msg = txtMsg.getText();
        System.out.println("Consumer:->Received: " + msg);
      } else {
        System.out.println("Consumer:->Received: " + message);
      }
    } catch (JMSException e) {
      e.printStackTrace();
    }
  }

}

Producer
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


public class Producer {

    private String user = ActiveMQConnection.DEFAULT_USER; //默认 用户
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;  //默认 密码
    private String url = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的是localhost:8080
    private String subject = "DEMO.LISTENER"; //消息目的地名称
    private Destination destination = null;  //在点对点(PTP)消息传递域中,目的地被成为队列(queue)
    private Connection connection = null;  //初始化 一个JMS客户端到JMS Provider的连接
    private Session session = null;  //初始化  一个发送消息的进程
    private MessageProducer producer = null;    //初始化 消息生产者 (它是由session 创建的)

    // 初始化
    private void initialize() throws JMSException, Exception {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                user, password, url);
        connection = connectionFactory.createConnection();
        //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式)
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //PTP消息方式   目的地被成为队列(queue)
        destination = session.createQueue(subject);
        producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    }



    // 发送消息
    public void produceMessage(String message) throws JMSException, Exception {
        initialize();
        TextMessage msg = session.createTextMessage(message);
        connection.start();
        System.out.println("Producer:->Sending message: " + message);
        producer.send(msg);
        System.out.println("Producer:->Message sent complete!");
    }



    // 关闭连接

    public void close() throws JMSException {

        System.out.println("Producer:->Closing connection");

        if (producer != null)

            producer.close();

        if (session != null)

            session.close();

        if (connection != null)

            connection.close();

    }

}


Test
public class Test {

    public static void main(String[] args) throws  Exception {
        Consumer consumer = new Consumer();
        Producer producer = new Producer();
        //通过监听Listener 开始监听
        consumer.start();
        // 延时500毫秒之后发送消息
        Thread.sleep(500);
        producer.produceMessage("Hello, world!");
        producer.close();
        consumer.close();
    }

}














你可能感兴趣的:(activeMQ之点对点(PTP),监听器模式)