首页  >  文章  >  Java  >  Java ActiveMQ的代码实例分享

Java ActiveMQ的代码实例分享

黄舟
黄舟原创
2017-09-15 09:59:541628浏览

下面小编就为大家带来一篇基于Java ActiveMQ的实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

所需引入Jar包:

jms-1.1.jar

activemq-all-5.15.0.jar

生产者


package com.mousewheel.demo;

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.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

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

  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.31.33:61616");
  Connection connection = null;
  try {
   connection = connectionFactory.createConnection();
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Destination destination = session.createQueue("test-queue");
   MessageConsumer consumer = session.createConsumer(destination);
   consumer.setMessageListener(new MessageListener() {
    @Override
    public void onMessage(Message message) {
     try {
      MqBean bean = (MqBean) ((ObjectMessage) message).getObject();
      System.out.println(bean);
      if (null != message) {
       System.out.println("收到消息" + bean.getName());
      }
     } catch (Exception e) {
      // TODO: handle exception
     }

    }
   });
   System.out.println("Begin listen");
  } catch (Exception e) {
   // TODO: handle exception
  }

 }
}

消费者


package com.mousewheel.demo;

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.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

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

  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.31.33:61616");
  Connection connection = null;
  try {
   connection = connectionFactory.createConnection();
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Destination destination = session.createQueue("test-queue");
   MessageConsumer consumer = session.createConsumer(destination);
   consumer.setMessageListener(new MessageListener() {
    @Override
    public void onMessage(Message message) {
     try {
      MqBean bean = (MqBean) ((ObjectMessage) message).getObject();
      System.out.println(bean);
      if (null != message) {
       System.out.println("收到消息" + bean.getName());
      }
     } catch (Exception e) {
      // TODO: handle exception
     }

    }
   });
   System.out.println("Begin listen");
  } catch (Exception e) {
   // TODO: handle exception
  }

 }
}

以上是Java ActiveMQ的代码实例分享的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn