亲宝软件园·资讯

展开

activemq java 实例 基于Java ActiveMQ的实例讲解

鼠标滚轮 人气:1
想了解基于Java ActiveMQ的实例讲解的相关内容吗,鼠标滚轮在本文为您仔细讲解activemq java 实例的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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
  }

 }
}

以上这篇基于Java ActiveMQ的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论