服务器之家

服务器之家 > 正文

基于Java ActiveMQ的实例讲解

时间:2021-01-04 15:43     来源/作者:鼠标滚轮

所需引入Jar包:

jms-1.1.jar

activemq-all-5.15.0.jar

生产者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
  }
 
 }
}

消费者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/mousewheel/archive/2017/09/14/7523555.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部