ActiveMq实例
1.发布端
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class Publisher
{
private static Logger logger = LoggerFactory.getLogger(Publisher.class); public static void main(String[] args)
{
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
MessageProducer producer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try
{
connection = connectionFactory.createConnection();
connection.start(); session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE); //注意这里与ptp例子的区别,使用null作为destination
producer = session.createProducer(null); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); sendMessage(session, producer); session.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (null != connection)
connection.close();
}
catch (Throwable ignore)
{
}
} } private static void sendMessage(Session session, MessageProducer producer) throws JMSException
{
Destination destination = session.createTopic("pub/sub");
Message message = session.createMapMessage();
message.setStringProperty("company", "alibaba");
message.setStringProperty("department", "b2b");
logger.info("destination is {};message is ready to send", destination); producer.send(destination, message);
}
}
2.接收端
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Subscribe
{
public static void main(String[] args)
{
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try
{
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE); destination = session.createTopic("pub/sub");
consumer = session.createConsumer(destination); //设置指定的监听器
consumer.setMessageListener(new MyListener()); Thread.sleep(100*1000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (null != connection)
connection.close();
}
catch (Throwable ignore)
{
}
} }
}
3.监听器
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class MyListener implements MessageListener
{
private static Logger logger = LoggerFactory.getLogger(MyListener.class); public void onMessage(Message message)
{
MapMessage mapMessage = (MapMessage) message;
try
{
String company = mapMessage.getStringProperty("company");
String department = mapMessage.getStringProperty("department"); logger.info("company is {} ; department is {}", company,department);
} catch (JMSException e)
{
e.printStackTrace();
}
}
}
4.测试
先启动接收端,再启动发布端,结果为:
[main] INFO com.mycompany.app.pub.sub.Publisher - destination is topic://pub/sub;message is ready to send
[ActiveMQ Session Task-1] INFO com.mycompany.app.pub.sub.MyListener - company is alibaba ; department is b2b
ActiveMq实例的更多相关文章
- ActiveMQ实例1--简单的发送和接收消息
一.环境准备 1,官网http://activemq.apache.org/下载最新版本的ActiveMQ,并解压 2,打开对应的目录,在Mac环境下,一般可以运行命令: cd /Users/***/ ...
- JMS学习二(简单的ActiveMQ实例)
下载安装ActiveMQ服务,下载地址当然可以去官网下载 http://activemq.apache.org/download-archives.html ActiveMQ安装很简单,下载解压后到b ...
- C#使用ActiveMQ实例
1. ActiveMQ消息总线简介 消息队列(Message Queue,简称MQ),从字面意思上看,本质是个队列,FIFO先入先出,只不过队列中存放的内容是message而已.主要用作不同进程.应用 ...
- ActiveMQ实例2--Spring JMS发送消息
参考文章:http://my.oschina.net/xiaoxishan/blog/381209#OSC_h3_7 一,步骤参照参考文献 二.新建的项目 三.补充 web.xml <?xml ...
- ActiveMQ挂了,重启一直无法将所有实例启起来的问题
背景 2017年3月29日 下午2-3点时分,工单模块无法访问.跟踪日志发现,ActiveMQ连接不上导致整个工单模块瘫痪: 首先判断可能是系统需要然后尝试重启工单模块,重新启动工单模块,结果:重启 ...
- ActiveMQ的集群方案对比及部署
转载:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- ActiveMQ笔记(4):搭建Broker集群(cluster)
上一篇介绍了基于Networks of Borkers的2节点HA方案,这一篇继续来折腾Networks of Brokers,当应用规模日渐增长时,2节点的broker可能仍然抗不住访问压力,这时候 ...
- ActiveMQ集群应用
ActiveMQ集群 ActiveMQ具有强大和灵活的集群功能,但在使用的过程中会发现很多的缺点,ActiveMQ的集群方式主要由两种:Master-Slave和Broker Cluster. 1.M ...
- ActiveMQ: 搭建Broker集群(cluster)
上一篇介绍了基于Networks of Borkers的2节点HA方案,这一篇继续来折腾Networks of Brokers,当应用规模日渐增长时,2节点的broker可能仍然抗不住访问压力,这时候 ...
随机推荐
- 第十六节: EF的CodeFirst模式通过Fluent API修改默认协定
一. 简介 1. 优先级:Fluent API > data annotations > default conventions. 2. 所有的Fluent API配置都要在 OnMode ...
- Quartz C#使用
参考:https://www.cnblogs.com/lazyInsects/p/8075487.htmlQuartz是一款比较好用的定时任务执行工具类,虽然我们平时也可以自己写代码实现定时执行,但是 ...
- Vorticity directions 1: self-improving property of the vorticity
在 [Li, Siran. "On Vortex Alignment and Boundedness of $ L^ q $ Norm of Vorticity." arXiv p ...
- updateXML 注入 python 脚本
用SLQMAP来跑updateXML注入发现拦截关键字,然后内联注入能绕,最后修改halfversionedmorekeywords.py脚本,结果SQLMAP还是跑不出来.>_< hal ...
- requests.session
# -*- coding: utf-8 -*- """requests.session~~~~~~~~~~~~~~~~ This module provides a Se ...
- jQuery UI弹出新窗体
借助jqueryUI 的Dialog 在隐藏的div中嵌入Iframe 改变iframe的路径 如果项目经常用到弹出新窗体,则利用模板,把此代码和html 放入父页面中,实现父级调用, <in ...
- axios发送post请求,如何提交表单数据?
axios发送post请求,提交表单数据的方式 默认情况下,axios将JavaScript对象序列化为JSON.要以application / x-www-form-urlencoded格式发送数据 ...
- WebService - [Debug] javax.xml.ws.WebServiceException: Undefined port type
背景: 使用JDK来开发java web service (Create a SOAP-based RPC style web service endpoint by using JAX-WS). 具 ...
- C++设计模式——迭代器模式
前言 最近非常感伤,总是怀念大学的日子,做梦的时候也常常梦到.梦到大学在电脑前傻傻的敲着键盘,写着代码,对付着数据结构与算法的作业:建立一个链表,遍历链表,打印链表.现在把那个时候声明的链表的头文件拿 ...
- Lnmp下pureftpd新建FTP账户权限不足解决方法
解决办法: 登录服务器.执行以下命令 chattr -i /home/wwwroot/default/.user.ini chown www:www -R /home/wwwroot/你的lnmp安 ...