Home > Java > javaTutorial > body text

Java ActiveMQ code example sharing

黄舟
Release: 2017-09-15 09:59:54
Original
1626 people have browsed it

The editor below will bring you an example explanation based on Java ActiveMQ. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

Required Jar package:

jms-1.1.jar

activemq-all-5.15.0 .jar

##producer


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
  }

 }
}
Copy after login
Copy after login

consumer


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
  }

 }
}
Copy after login
Copy after login

The above is the detailed content of Java ActiveMQ code example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template