Activemq实例
来源:互联网 发布:东华软件金融部 编辑:程序博客网 时间:2024/06/07 20:32
介绍activemq点对点消息实例。
生产者代码:
package com.xxx.activemq.send;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import org.apache.activemq.ActiveMQConnectionFactory;/** * Hello world! * */public class Producer { private static String userName = "admin"; private static String password = "admin"; private static String brokerUrl = "tcp://localhost:61616"; public static void main( String[] args ) { ConnectionFactory factory; Connection connection = null; Session session; Destination dest; MessageProducer producer; factory = new ActiveMQConnectionFactory(userName, password, brokerUrl); try { connection = factory.createConnection(); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); dest = session.createQueue("brokerQueue"); producer = session.createProducer(dest); for(int i=0;i<5;i++){ producer.send(session.createTextMessage("i say ->" + i )); } producer.close(); session.close(); } catch (JMSException e) { e.printStackTrace(); }finally{ if(connection!=null){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } }}
代码很直观,先由activemq实现的ConnectionFactory直接构建一个连接工厂,这里需要传入连接broker的用户名,密码,url,可以直接使用系统默认的值。
接着用工厂创建连接。然后利用连接创建会话,会话创建消息目的(Queue),生产者。利用生产发送会话创建的消息。
运行生产者代码,然后查看队列,产生了五个消息,等待消费者来消费:
消费者代码:
package com.xxx.activemq.receive;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Consumer { private static String userName = ActiveMQConnection.DEFAULT_USER;//"admin"; private static String password = ActiveMQConnection.DEFAULT_PASSWORD;//"admin"; private static String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;//"tcp://localhost:61616"; public static void main(String[] args) { ConnectionFactory factory; Connection connection = null; Session session; Destination dest; MessageConsumer consumer; factory = new ActiveMQConnectionFactory(userName, password, brokerUrl); try { connection = factory.createConnection(); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); dest = session.createQueue("brokerQueue"); consumer = session.createConsumer(dest); while(true){ TextMessage message = (TextMessage)consumer.receive(100); if(message != null){ System.out.println("received: "+message.getText()); }else { break; } } session.close(); } catch (JMSException e) { e.printStackTrace(); }finally{ if(connection!=null){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } }}
消费者代码和生产者基本类似,到了消息接收这里稍微区别一下,这里用的是通过循环去broker上读取,直到读取完成,将收到的五个消息都打印出来。Eclipse控制台打印的消息如下:
消费了生产者产生的消息之后,再次查看队列,发现五个消息已经被消费了(dequeue)。
阅读全文
0 0
- ActiveMQ 实例
- ActiveMQ实例
- activeMq 实例
- ActiveMQ实例
- ActiveMQ实例
- Activemq实例
- ActiveMQ实例
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ入门实例
- ActiveMQ简述及实例
- ActiveMQ简述及实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ开发实例
- 变分自编码器VAE
- Python中的range和xrange区别
- Hibernate第一天
- 解决android.os.NetworkOnMainThreadException
- linux常用命令(logout,alias,clear,history,wc)
- Activemq实例
- 运行Tomcat时报java.lang.UnsupportedClassVersionError: oracle/demo/oow/bd/ui/IsUserLogued
- Android控制之ToggleButton和Switch与RatingBar
- Swift
- EularProject 74:Digit factorial chains
- python 记录,防止遗忘
- iframe有哪些缺点?应该怎么合理使用?
- Mac电脑上配置Java开发环境
- log4js 日志模块