activeMq的点对点发送
来源:互联网 发布:淘宝网棉衣女装中长款 编辑:程序博客网 时间:2024/05/29 15:23
1. 建立连接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory("system","manager","tcp://localhost:61616");参数1:用户名,默认:ActiveMQConnection.DEFAULT_USER=null;可以根据mq的配置文件自己设置参数2:密码,默认:ActiveMQConnection.DEFAULT_PASSWORD=null;可以根据mq的配置文件自己设置参数3:url,默认:ActiveMQConnection.DEFAULT_BROKER_URL=failover://tcp://localhost:61616;url有几种:1. TCP:
MQ默认使用的协议,开放性、高效性、可靠性是其主要特点,这个协议可满足一般应用的大部分需求,因此在项目中还是比较常用的。<transportConnector name="nio" uri="nio:localhost:61618" />2. NIO:
网络消息的传播效率非常的高。因此,当系统存在高用户量、高并发或网络堵塞时,可考虑用此协议。3. SSL:
安全套接层协议在MQ目录/conf/activemq.xml文件中修改以下配置Xml代码:<sslContext> <sslContext keyStore="file:${activemq.base}/conf/broker.ks" keyStorePassword="password"/> </sslContext> <transportConnectors> <transportConnector name="ssl" uri="ssl://localhost:61617"/> </transportConnectors>2. 建立连接
Connection conn = factory.createConnection();3. 获取session
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);参数1:是否自动提交参数2:Session.AUTO_ACKNOWLEDGE, Session.CLIENT_ACKNOWLEDGE, Session.DUPS_OK_ACKNOWLEDGE4. 建立Destination
Destination destination = session.createQueue("queue");5. 建立MessageProducer
MessageProducer producer = session.createProducer(destination);6. 产生消息并发送:
ObjectMessage om = session.createObjectMessage(d);producer.send(om);7. 建立消费者
MessageConsumer consumer = session2.createConsumer(destination2);8. 注册监听器
consumer.setMessageListener(new MyListener());
发送者完整代码:
<pre name="code" class="java">public class MqSender {private static String JMS_USER_NAME = ActiveMQConnection.DEFAULT_USER;private static String JMS_PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;private static String JMS_URL = ActiveMQConnection.DEFAULT_BROKER_URL;public void send(){Connection conn = null;try{ConnectionFactory factory = new ActiveMQConnectionFactory("system","manager","tcp://localhost:61616");conn = factory.createConnection();conn.start();Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createQueue("queue");MessageProducer producer = session.createProducer(destination);for(int i=0; i<10; i++){Data d = new Data("data"+i);ObjectMessage om = session.createObjectMessage(d);producer.send(om);System.out.println("producer向"+producer.getDestination()+"发送了消息:"+d);Thread.sleep(3000);session.commit();}}catch(Exception e){}finally{if(conn != null){try {conn.close();} catch (JMSException e1) {e1.printStackTrace();}}}}public static void main(String[] args) {MqSender sender = new MqSender();try {sender.send();} catch (Exception e) {e.printStackTrace();}}}
消费者完整代码:
<pre name="code" class="java">public class Receiver {private static String JMS_USER_NAME = ActiveMQConnection.DEFAULT_USER;private static String JMS_PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;private static String JMS_URL = ActiveMQConnection.DEFAULT_BROKER_URL;public void receive(){Connection conn = null;try{ConnectionFactory factory = new ActiveMQConnectionFactory(JMS_USER_NAME,JMS_PASSWORD,JMS_URL);conn = factory.createConnection();conn.start();Session session2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);Destination destination2 = session2.createQueue("queue");MessageConsumer consumer = session2.createConsumer(destination2);consumer.setMessageListener(new MyListener1());}catch(Exception e){}finally{if(conn != null){try {conn.close();} catch (JMSException e1) {e1.printStackTrace();}}}}public static void main(String[] args) {Receiver re = new Receiver();try {re.receive();} catch (Exception e) {e.printStackTrace();}}}class MyListener1 implements MessageListener{@Overridepublic void onMessage(Message msg) {try {Data d = (Data) ((ObjectMessage)msg).getObject();System.out.println("rec1:"+d);Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}}}
0 0
- activeMq的点对点发送
- 【ActiveMQ】ActiveMQ在Windows的安装,以及点对点的消息发送案例
- ActiveMq点对点模式发送、接收消息
- ActiveMQ发送消息,接受消息。点对点连接
- activeMQ之点对点(p2p)发送与接收消息
- ActiveMQ 点对点(p2p)
- ActiveMQ 点对点消息实现
- ActiveMQ入门:点对点模型
- Activemq点对点模式
- activemq点对点模式
- ActiveMQ点对点消息实现
- ActiveMQ 点对点模式
- ActiveMQ消息队列的使用--点对点的消息模式
- [ActiveMQ实战]基于JMS的点对点消息传播
- 基于spring整合activeMQ以及点对点队列的封装
- ActiveMQ 实现点对点的消息模型小Demo
- ActiveMQ实战(二)--ActiveMQ的通信方式之P2P点对点通信(point-to-point)
- JMS之activeMQ--点对点通信
- PAT乙级 1036 跟奥巴马一起编程
- js高程 (DOM)
- Kinect视觉SLAM技术介绍
- linux 进程间通信简介(IPC)
- kafka windows环境的搭建之路
- activeMq的点对点发送
- HDU 1202 The calculation of GPA
- mysql获取按日期排序获取最新的记录
- Oracle 11g安装出现ORA-12154:could not resolve the connect identifier specified
- 爬虫测试
- 百练 4124 海贼王之伟大航路
- 2016 Multi-University Training Contest 1 C Game(hdu 5725)
- hdoj-1370-Biorhythms
- jQuery jqXHR对象的属性和方法