大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ActiveMQ如何在Java中使用

ActiveMQ如何在Java中使用 ?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了尧都免费建站欢迎大家使用!

所需引入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
  }

 }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


本文标题:ActiveMQ如何在Java中使用
转载来于:http://dzwzjz.com/article/pcohds.html
在线咨询
服务热线
服务热线:028-86922220
TOP