activeMQ之点对点(PTP),监听器模式
来源:互联网 发布:北龙中网域名认证 编辑:程序博客网 时间:2024/05/21 07:15
监听器模式
Consumer
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class Consumer implements MessageListener { private String user = ActiveMQConnection.DEFAULT_USER; //默认 用户 private String password = ActiveMQConnection.DEFAULT_PASSWORD; //默认 密码 private String url = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的是localhost:8080 private String subject = "DEMO.LISTENER"; //消息目的地名称 private Destination destination = null; //在点对点(PTP)消息传递域中,目的地被成为队列(queue) private Topic topic = null;//在发布/订阅(PUB/SUB)消息传递域中,目的地被成为主题(topic)。 private Connection connection = null; //初始化 一个JMS客户端到JMS Provider的连接 private Session session = null; //初始化 一个接受消息的进程 private MessageConsumer consumer = null; //初始化 消息消费者 // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式) session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //PTP消息方式 目的地被成为队列(queue) destination = session.createQueue(subject); consumer = session.createConsumer(destination); } // 消费消息 public void start() throws Exception { initialize(); connection.start(); System.out.println("Consumer:->Begin listening..."); consumer.setMessageListener(this); } // 关闭连接 public void close() throws JMSException { System.out.println("Consumer:->Closing connection"); if (consumer != null) consumer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } // 消息处理函数 public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); System.out.println("Consumer:->Received: " + msg); } else { System.out.println("Consumer:->Received: " + message); } } catch (JMSException e) { e.printStackTrace(); } }}
Producer
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class Producer { private String user = ActiveMQConnection.DEFAULT_USER; //默认 用户 private String password = ActiveMQConnection.DEFAULT_PASSWORD; //默认 密码 private String url = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的是localhost:8080 private String subject = "DEMO.LISTENER"; //消息目的地名称 private Destination destination = null; //在点对点(PTP)消息传递域中,目的地被成为队列(queue) private Connection connection = null; //初始化 一个JMS客户端到JMS Provider的连接 private Session session = null; //初始化 一个发送消息的进程 private MessageProducer producer = null; //初始化 消息生产者 (它是由session 创建的) // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式) session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //PTP消息方式 目的地被成为队列(queue) destination = session.createQueue(subject); producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } // 发送消息 public void produceMessage(String message) throws JMSException, Exception { initialize(); TextMessage msg = session.createTextMessage(message); connection.start(); System.out.println("Producer:->Sending message: " + message); producer.send(msg); System.out.println("Producer:->Message sent complete!"); } // 关闭连接 public void close() throws JMSException { System.out.println("Producer:->Closing connection"); if (producer != null) producer.close(); if (session != null) session.close(); if (connection != null) connection.close(); }}
Test
public class Test { public static void main(String[] args) throws Exception { Consumer consumer = new Consumer(); Producer producer = new Producer(); //通过监听Listener 开始监听 consumer.start(); // 延时500毫秒之后发送消息 Thread.sleep(500); producer.produceMessage("Hello, world!"); producer.close(); consumer.close(); }}
0 0
- activeMQ之点对点(PTP),监听器模式
- ActiveMQ点对点模式(PTP)
- activeMQ之点对点(PTP),主动接受消息
- 消息中间件之ActiveMQ PTP和PUB/SUB模式实例(三)
- Spring整合activeMq(一):点对点模式
- Activemq点对点模式
- activemq点对点模式
- ActiveMQ 点对点模式
- spring整合activemq消息队列之点对点模式
- activeMQ消息中间件之点对点与发布订阅模式实现
- ActiveMQ 点对点(p2p)
- JMS之activeMQ--点对点通信
- ActiveMQ两种模式PTP和PUB/SUB
- ActiveMQ demos-PTP domain
- ActiveMq-PTP入门例子
- ActiveMq点对点模式发送、接收消息
- ActiveMQ实战之 Queue点对点消息
- ActiveMQ实战之 Queue点对点消息
- HTTP 状态消息
- 阿里云OTS(开放结构化数据服务)可视化管理工具的设计和功能介绍
- 数组指针和指针数组的区别
- php下memcache的应用(一)——什么是memcache
- HTTP 方法:GET 对比 POST
- activeMQ之点对点(PTP),监听器模式
- C++学习 STL之五:set/multiset用法详解
- HDU 1711 Number Sequence (数字KMP)
- Android自定义View,你必须知道的几点
- spoj375Query on a tree
- Linux GCC常用命令
- C++学习 STL之六:map/multimap用法详解
- C++ 重载赋值操作符
- the info.plist in the package must contain the CFBundleShortVersinString key