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. UVaLive 6855 Banks (水题,暴力)

    题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...

  2. 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包,你需要设置一下eclipse:项目 —> 属性 -> Deployment Assembly ...

  3. sqlserver表分区与调优与行列转换

    转自: http://www.cnblogs.com/knowledgesea/p/3696912.html http://www.open-open.com/lib/view/open1418462 ...

  4. CKEditor与CKFinder整合并实现文件上传功能

    事先说明:此整合的是java版本的, 用到的有:jsp + ckeditor + ckfinder (没有servlet 及其它框架技术) 一.需要的资源: 用到的网站,文件自己下载: a) cked ...

  5. 简谈java的split

    最近都在处理视频音频,今天在合成音频视频时为了给合成的新文件换个新名字,我打算获取了之前的视频名称,用split来分割出不带后缀的名字,再自己加上后缀. 众所周知split可以分割由某种字符分段的St ...

  6. CSS里的单位

    CSS中预设了16种颜色以及16种颜色的衍生色,这16种颜色是CSS规范推荐的.并且一些主流的浏览器都可以识别.例如以下表所看到的: 十六进制颜色是最经常使用的定义方式.它的基本格式为#RRGGBB, ...

  7. MKMapView的内存释放问题

    MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...

  8. MHA手动在线切换主 原创3(主不参与复制)

    monitor 执行:slave2连接到slave1,server1 不做(主/从复制角色,停在那里) [root@monitor app1]# masterha_master_switch --co ...

  9. 【Unity3D插件】NGUI屏幕自适应(转)

    屏幕自适应 NGUI可以比较方便的实现屏幕自适应,但是它的官方教程里面针对这个问题没有详细的教程,所以可能在实现的时候会走比较多的弯路.以下是我在开发过程中找到的一个比较方便的实现方法. 主要组件 1 ...

  10. 网络IPC:套接字

    网络进程间通信(network IPC):不同计算机(通过网络相连)上运行的进程相互通信的机制. 套接字网络IPC接口:进程能够使用该接口和其他进程通信.通过该接口,其他进程运行位置是透明的,它们可以 ...