ActiveMQ(5.10.0) - hello world
Sending a JMS message
public class MyMessageProducer {
...
// 创建连接工厂实例
ConnectionFactory connFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection conn = null;
try {
// 取得连接对象实例
conn = connFactory.createConnection();
// 启动连接
conn.start();
// 创建会话对象实例
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("hello_queue");
// 创建消息生产者
MessageProducer msgProducer = session.createProducer(destination);
// 创建消息对象实例
Message textMsg = session.createTextMessage("This is a test message.");
// 发送消息
msgProducer.send(textMsg);
// 提交会话
session.commit();
} catch (JMSException e) {
e.printStackTrace();
} finally {
// 关闭连接
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
...
}
Receiving a JMS message synchronously
public class MySynMessageConsumer {
...
// 创建连接工厂实例
ConnectionFactory connFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection conn = null;
try {
// 取得连接对象实例
conn = connFactory.createConnection();
// 启动连接,当调用此方法后才能接收到消息
conn.start();
// 创建会话对象实例
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("hello_queue");
// 创建消息消费者
MessageConsumer msgConsumer = session.createConsumer(destination);
// 接收消息
TextMessage textMsg = (TextMessage) msgConsumer.receive(10 * 1000);
if (textMsg != null) {
System.out.println(textMsg.getText());
}
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
...
}
Receiving a JMS message asynchronously
public class MyAsynMessageConsumer {
...
// 创建连接工厂实例
ConnectionFactory connFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection conn = null;
try {
// 取得连接对象实例
conn = connFactory.createConnection();
// 启动连接,当调用此方法后才能接收到消息
conn.start();
// 创建会话对象实例
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("hello_queue");
// 创建消息消费者
MessageConsumer msgConsumer = session.createConsumer(destination);
// 注册消息监听器
msgConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message msg) {
TextMessage textMsg = (TextMessage) msg;
try {
System.out.println(textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
Thread.sleep(60 * 1000);
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 关闭连接
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
...
}
Others
- 对于 Pub/Sub 模型,使用 session.createTopic 方法创建 Destination。
- MessageConsumer 同步消费消息时,receive() 方法会阻塞线程直到接收到下一条消息;receive(long timeout) 方法在指定的时间内阻塞线程直到接收到下一条消息,如果超时,则返回 null 值;receiveNoWait() 方法立刻接收下一条消息,如果消息源中没有消息,则返回 null 值。
ActiveMQ(5.10.0) - hello world的更多相关文章
- ActiveMQ 5.10.0 安装与配置
先在官网下载activeMQ,我这里是5.10.0. 然后在解压在一个文件夹下即可. 我这里是:D:\apache-activemq-5.10.0-bin 然后进入bin目录:D:\apache-ac ...
- ActiveMQ(5.10.0) - Configuring the JAAS Authentication Plug-in
JAAS provides pluggable authentication, which means ActiveMQ will use the same authentication API re ...
- ActiveMQ(5.10.0) - Spring Support
Maven Dependency: <dependencies> <dependency> <groupId>org.apache.activemq</gro ...
- ActiveMQ(5.10.0) - 删除闲置的队列或主题
方法一 通过 ActiveMQ Web 控制台删除. 方法二 通过 Java 代码删除. ActiveMQConnection.destroyDestination(ActiveMQDestinati ...
- ActiveMQ(5.10.0) - Connection Configuration URI
An Apache ActiveMQ connection can be configured by explicitly setting properties on the ActiveMQConn ...
- ActiveMQ(5.10.0) - Building a custom security plug-in
If none of any built-in security mechanisms works for you, you can always build your own. Though the ...
- ActiveMQ(5.10.0) - 使用 JDBC 持久化消息
1. 编辑 ACTIVEMQ_HOME/conf/activemq.xml. <beans> <broker brokerName="localhost" per ...
- ActiveMQ(5.10.0) - Configuring the Simple Authentication Plug-in
The easiest way to secure the broker is through the use of authentication credentials placed directl ...
- ActiveMQ(5.10.0) - Destination-level authorization
To build upon authentication, consider a use case requiring more fine-grained control over clients t ...
随机推荐
- [置顶] Jquery中DOM操作(详细)
Jquery中的DOM操作 为了能全面的讲解DOM操作,首先需要构建一个网页. HTML代码: <%@ page language="java" import="j ...
- Tiny6410声卡驱动——录音与回放
在Linux下,音频设备程序的实现与文件系统的操作密切相关.Linux将各种设备以文件的形式给出统一的接口,这样的设计使得对设备的编程与对文件的操作基本相同,对Linux内核的系统调用也基本一致,从而 ...
- Android布局优化之include、merge、ViewStub的使用
本文针对include.merge.ViewStub三个标签如何在布局复用.有效减少布局层级以及如何可以按需加载三个方面进行介绍的. 复用布局可以帮助我们创建一些可以重复使用的复杂布局.这种方式也意味 ...
- The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- 关于DateTime.Now.Ticks
DataTime.Now.Ticks 的值表示自 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔的间隔数,可用于较精确的计时. 1秒=1000豪秒 1毫秒=10 ...
- ssh 设置免password登录
如果: 实现->操作机A机 要以用户"user1"身份.免password登录B机和C机? B机和C机 建立用户 user1 而且能够ssh 登录. A机设置: 1.安装ss ...
- TC SRM 663 div2 A ChessFloor 暴力
ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...
- HDU 1077Catching Fish(简单计算几何)
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Error creating bean with name 'menuController': Injection of autowired dependency……
出现了一大串错误 Error creating bean with name 'userController': Injection of autowired dependencies failed. ...
- MYSQL 5.7 新增150多个新功能
http://www.thecompletelistoffeatures.com/ There are over 150 new features in MySQL 5.7. The MySQL ma ...