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的更多相关文章

  1. ActiveMQ 5.10.0 安装与配置

    先在官网下载activeMQ,我这里是5.10.0. 然后在解压在一个文件夹下即可. 我这里是:D:\apache-activemq-5.10.0-bin 然后进入bin目录:D:\apache-ac ...

  2. ActiveMQ(5.10.0) - Configuring the JAAS Authentication Plug-in

    JAAS provides pluggable authentication, which means ActiveMQ will use the same authentication API re ...

  3. ActiveMQ(5.10.0) - Spring Support

    Maven Dependency: <dependencies> <dependency> <groupId>org.apache.activemq</gro ...

  4. ActiveMQ(5.10.0) - 删除闲置的队列或主题

    方法一 通过 ActiveMQ Web 控制台删除. 方法二 通过 Java 代码删除. ActiveMQConnection.destroyDestination(ActiveMQDestinati ...

  5. ActiveMQ(5.10.0) - Connection Configuration URI

    An Apache ActiveMQ connection can be configured by explicitly setting properties on the ActiveMQConn ...

  6. 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 ...

  7. ActiveMQ(5.10.0) - 使用 JDBC 持久化消息

    1. 编辑 ACTIVEMQ_HOME/conf/activemq.xml. <beans> <broker brokerName="localhost" per ...

  8. 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 ...

  9. ActiveMQ(5.10.0) - Destination-level authorization

    To build upon authentication, consider a use case requiring more fine-grained control over clients t ...

随机推荐

  1. freemaker自定义分页控件实现

    <link href="${res}/css/pages-jhdb.css" rel="stylesheet" type="text/css&q ...

  2. 【数据库】MySql常用函数梳理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5866388.html MySql常用函数梳理: 1:数学函数 使用格式:一般用于插入.修改语句中,直接 函数( ...

  3. UVA1395

    // UVa1395 Slim Span // Rujia Liu #include<cstdio> #include<cmath> #include<cstring&g ...

  4. PostgreSQL的 initdb 源代码分析之二十四

    继续分析: make_template0(); 展开: 无需再作解释,就是创建template0数据库 /* * copy template1 to template0 */ static void ...

  5. IIS下的身份验证方式管理

    设置.查看身份验证方式 #导航到某站点下: cd IIS:\Sites\DemoSite\DemoApp #启用站点test01下的Windows身份验证 Set-WebConfigurationPr ...

  6. c# 反射学习笔记

    首先了解C#反射的概念,反射是一个运行库类型发现的过程.通过反射可以得到一个给定程序集所包含的所有类型的列表, 这个列表包括给定类型中定义的方法.字段.属性和事件.也可以动态的发现一组给定类支持的借口 ...

  7. delphi TToolBar

    工具栏  的属性 xe4的事件 Customizable OnCustomizeCanDelete OnCustomizeCanInsert OnCustomized OnCustomizeAdded ...

  8. Android学习笔记(20)————利用ListView制作带竖线的多彩表格

    http://blog.csdn.net/conowen/article/details/7421805 /********************************************** ...

  9. c#智能感知(设置)及实现

    1) 使用工具->选项菜单命令,选择文本编辑器->C#设置, 将上面圈出的2个复选框(自动列出成员和参数信息)打勾, 然后 Intellisense就会工作了.(如果没这么多选项,请勾选S ...

  10. AngularJS - contorller app module

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...