/**
* 基于tcp协议接收JMS消息
* @param args 参数
*/
public static void main(String[] args)
{
ConnectionFactory connFactory;
Connection conn = null;
Session session;
Destination destination;
MessageConsumer consumer;
try {
connFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
conn = connFactory.createConnection("user1", "password1");
conn.start();
// 第一个参数为 false, 表示不使用事务
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(JMSInfo.QUEUE_NAME);
// destination = session.createTopic(JMSInfo.TOPIC_NAME);
consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener()
{
@Override
public void onMessage(Message msg)
{
try {
if (msg instanceof TextMessage) {
TextMessage textMsg = (TextMessage)msg;
System.out.println("received text: " + textMsg.getText());
} else if (msg instanceof MapMessage){
ActiveMQMapMessage map = (ActiveMQMapMessage)msg;
System.out.println("received map: ");
printMapMessage(map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
// if (null != conn) {
// try {conn.close(); } catch (Exception e) {}
// }
}
}
/**
* 输出 map 消息
* @param map 参数
*/
@SuppressWarnings("unchecked")
public static void printMapMessage(ActiveMQMapMessage map) throws Exception
{
String key = null;
String value = null;
// print headers
System.out.println("JMS Header:");
Enumeration<String> headerKeys = map.getPropertyNames();
while (headerKeys.hasMoreElements()) {
key = headerKeys.nextElement();
value = map.getStringProperty(key);
System.out.println(key + " : " + value);
}
// print body
System.out.println("JMS Body:");
Enumeration<String> bodyKeys = map.getMapNames();
while (bodyKeys.hasMoreElements()) {
key = bodyKeys.nextElement();
value = map.getString(key);
System.out.println(key + " : " + value);
}
}
分享到:
相关推荐
activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载
activemq-pool-5.8.0-sources.jar
apache-activemq-5.16.0.zip
赠送jar包:activemq-core-5.7.0.jar; 赠送原API文档:activemq-core-5.7.0-javadoc.jar; 赠送源代码:activemq-core-5.7.0-sources.jar; 包含翻译后的API文档:activemq-core-5.7.0-javadoc-API文档-中文...
赠送jar包:activemq-protobuf-1.1.jar; 赠送原API文档:activemq-protobuf-1.1-javadoc.jar; 赠送源代码:activemq-protobuf-1.1-sources.jar; 包含翻译后的API文档:activemq-protobuf-1.1-javadoc-API文档-...
apache-activemq-5.9.0-bin.zip
apache-activemq-5.8.0-bin.zip
本代码关于activemq-cpp的核心代码参考的chenxun2009的博客园,其他部分包括:从配置文件中读取消息通道,过滤条件等信息。
apache-activemq-5.15.15二进制包,安装包,apache-activemq-5.15.15-bin.tar.gz
activemq-cpp-library-3.9.5编译的C++库文件,支持vs2015,vs2017,包括了依赖的库文件和头文件,可以在项目中直接使用。
解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。 包含了apache-activemq-5.5.1-bin.zip以及ActiveMQ一个helloworld的demo启动ActiveMQ以后,登陆:...
activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的。其实activemq-web-console完全可以和activemq-broker分开来部署。 activemq-web-console包含3个apps, 1.一...
activemq-all-5.2.0.JAR包,欢迎下载。编写java中间件的时候会用到。这是activemq实现的jms中间件。希望能帮助到你。
activemq-all.5.14jar包,亲测可用!!
tar -zxvf apache-activemq-5.15.12-bin.tar.gz 2.进入bin目录 cd /apache-activemq-5.15.12/bin 3.运行,没有配置环境变量只能在bin目录下使用命令 ./activemq 4.配置环境变量,配置完环境变量之后...
activemq-protobuf-1.1.jar;activemq-protobuf-1.1.jar
apache-activemq-5.15.9-bin.zip apache-activemq-5.15.9-bin.zip apache-activemq-5.15.9-bin.zip
activemq-cpp-library-3.9.5-src.zip
apache-activemq-5.15.3-bin apache-activemq-5.15.3-bin
MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka...3、支持多种传输协议:TCP,SSL,NIO,UDP等 4、支持AJAX 消息形式: 1、点对点(queue) 2、一对多(topic)