ActiveMQ应用笔记二:Producer与Consumer模板代码
来源:互联网 发布:阿里云推荐码2017 编辑:程序博客网 时间:2024/04/28 14:10
接上一篇文章。
public static void main(String[] args) { ConnectionFactory connectionFactory; Connection connection = null; Session session; Destination destination; MessageProducer producer; //参数为用户名,密码,MQ的url connectionFactory = new ActiveMQConnectionFactory( "smechina", "fulong", "tcp://localhost:61616"); try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //*******注意:此处需修改为topic才能支持1对多发信息 //destination = session.createQueue("FirstQueue"); destination = session.createTopic("FirstTopic"); // 得到消息生成者【发送者】 producer = session.createProducer(destination); // ********设置是否持久化 //producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); producer.setDeliveryMode(DeliveryMode.PERSISTENT); //uandp是需要发送的消息 String uandp = "liyang:fulong"; sendMessage(session, producer,uandp); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }
public static void sendMessage(Session session, MessageProducer producer,String passport) throws Exception { // 发送10条消息 for (int i = 0; i < 9; i++) { TextMessage message = session.createTextMessage("账号"+i+":" + passport); //System.out.println("账号是:" + passport); //可设置属性过滤 //message.setStringProperty("receiver", "A"); producer.send(message); }
public static void main(String[] args) { ConnectionFactory connectionFactory; Connection connection = null; Session session; Destination destination; MessageConsumer consumer; //参数为用户名,密码,MQ的url connectionFactory = new ActiveMQConnectionFactory( "smeguangdong", "fulong", "tcp://localhost:61616"); try { connection = connectionFactory.createConnection(); //---持久化订阅才使用下语句!!!!参数是clientid,设置该参数后MQ会记住该ID--- connection.setClientID("guangdong"); connection.start(); session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); //*******注意:此处需修改为topic才能支持1对多发信息 //destination = session.createQueue("FirstQueue"); destination = session.createTopic("FirstTopic"); //创建普通消费者【接收者】,使用属性过滤 //consumer = session.createConsumer(destination,"receiver = 'A'"); //--------持久化订阅!!!!------第二个参数是client名 consumer = session.createDurableSubscriber((Topic)destination,"guangdong"); MessageListener ml = new MessageListener(){ @Override //设置监听器 public void onMessage(Message m) { TextMessage textMsg = (TextMessage) m; try{ System.out.println("收到消息:" + textMsg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }; consumer.setMessageListener(ml); while (true) { } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }
ActiveMQ成功部署运行后,就可以开发producer和consumer了。
在eclipse中新建Java项目,将ActiveMQ安装目录下lib中的jar包都引入项目,然后就可以编写producer和consumer。
注意:如果是测试代码,为了真实性,最好把producer和consumer分开写,而不是写在一个类中。
1.Producer:
package com.liyang.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender_lion {
}
}
1.Consumer:
package com.liyang.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver_lion{
}
}
- ActiveMQ应用笔记二:Producer与Consumer模板代码
- producer consumer java代码
- ActiveMQ消息中间件Producer和Consumer
- ActiveMQ消息中间件Producer和Consumer
- Producer-Consumer模型:二、如何实现
- producer consumer
- producer & consumer
- Kafka 学习笔记(4) - Consumer和Producer
- kafka0.9 producer与consumer参数
- 生产者-消费者(producer-consumer)问题(二)
- java多线程设计模式之Producer-Consumer模式(二)
- Java菜鸟入门(20) Producer Consumer经典代码
- kafka学习笔记 --- Scala实现Kafka producer 和 consumer
- 线程问题:生产者(Producer)与消费者(Consumer)
- java基础之线程同步实例之Producer与Consumer
- 用python,100行完成producer与consumer模型
- The Producer-Consumer Problem
- compile producer-consumer
- Try ... Catch ... Finally camel
- android升级ADT22没有R文件
- ActiveMQ应用笔记一:基本概念&安装
- android AsyncTask介绍
- 无法登陆、无法下载
- ActiveMQ应用笔记二:Producer与Consumer模板代码
- poj 3468 A Simple Problem with Integers 解题报告 线段树 数状数组两种实现
- ActiveMQ应用笔记三:安全性
- 表单里加了个一个下拉列表
- PHP 获取参数的方法
- DataGridView使用技巧—获取任意行或列的单元格文本
- 经验分享--免安装Oracle客户端使用PL/SQL
- 编译器相关是一个问题!
- 让360俯首称臣----详细教程含源码