Java消息中间件ActiveMQ初体验
来源:互联网 发布:js 同时满足两个条件 编辑:程序博客网 时间:2024/05/20 18:19
首先来一波百科
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位
下载安装
下载地址http://activemq.apache.org/activemq-5150-release.html
看个人选择windows或者linux版本
下载解压运行bn下面的activemq.bat或者选择安装到系统服务中心
这样就成功了默认账号密码为admin
官网例子
生产
import org.apache.activemq.ActiveMQConnectionFactory;public class HelloWorldProducer implements Runnable{ public void run() { // TODO Auto-generated method stub //创建一个ConnectionFactory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); try { // Create a Connection Connection connection = connectionFactory.createConnection(); //打开连接 connection.start(); // Create a Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue("TEST.FOO"); // Create a MessageProducer from the Session to the Topic or Queue MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create a messages String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode(); TextMessage message = session.createTextMessage(text); // Tell the producer to send the message System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName()); producer.send(message); // Clean up session.close(); connection.close(); } catch (JMSException e) { System.out.println("Caught: " + e); e.printStackTrace(); } }}
消费
package com.activemq;import javax.jms.Connection;import javax.jms.Destination;import javax.jms.ExceptionListener;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class HelloWorldConsumer implements Runnable,ExceptionListener{ public void onException(JMSException arg0) { System.out.println("JMS Exception occured. Shutting down client."); } public void run() { try { // Create a ConnectionFactory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // Create a Connection Connection connection = connectionFactory.createConnection(); connection.start(); connection.setExceptionListener(this); // Create a Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue("TEST.FOO"); // Create a MessageConsumer from the Session to the Topic or Queue MessageConsumer consumer = session.createConsumer(destination); // Wait for a message Message message = consumer.receive(1000); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); System.out.println("Received: " + text); } else { System.out.println("Received: " + message); } consumer.close(); session.close(); connection.close(); } catch (Exception e) { System.out.println("Caught: " + e); e.printStackTrace(); } }}
测试
package com.activemq;public class App { public static void main(String[] args) throws Exception { thread(new HelloWorldProducer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); Thread.sleep(1000); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); Thread.sleep(1000); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldProducer(), false); Thread.sleep(1000); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldConsumer(), false); thread(new HelloWorldProducer(), false); } public static void thread(Runnable runnable, boolean daemon) { Thread brokerThread = new Thread(runnable); brokerThread.setDaemon(daemon); brokerThread.start(); } }
结果
阅读全文
1 0
- Java消息中间件ActiveMQ初体验
- Java消息中间件--ActiveMq,RabbitMQ,Kafka
- [ActiveMQ]消息中间件在Java中的使用
- 初见Java消息中间件之ActiveMQ
- Java消息中间件之Spring中的ActiveMQ
- 初见Java消息中间件之ActiveMQ
- java常用的消息中间件activeMQ
- 【Java】消息中间件ActiveMQ试验记录
- activemq消息中间件
- 初识ActiveMQ消息中间件
- 消息队列中间件ActiveMQ
- 消息中间件-ActiveMQ
- 初识ActiveMQ消息中间件
- 消息中间件 ActiveMQ基础
- 中间件 消息队列 ActiveMQ
- 消息中间件入门(ActiveMQ)
- 【Java消息中间件】Java消息中间件( 第4章 使用activemq
- 【Java消息中间件】Java消息中间件( 第4章 使用activemq
- mybatis框架缓存使用
- StringBuilder在高性能场景下的正确用法
- 微信小程序 整理文档(更新中。。。)
- muduo源码学习(11)-日志类封装1
- Java面试个人整理(手打勿喷,易于个人学习)2
- Java消息中间件ActiveMQ初体验
- C++中extern关键字的使用
- 存储专栏:深度解读高端存储的快照技术
- Mybatis的延迟加载
- 阿里云安全科学家吴翰清入选MIT TR35,你知道弹性安全网络技术吗?
- Oracle习题
- 微信分享功能
- JAVA 通过 Socket 实现 TCP 编程
- Oracle:关于安装oracle中踩得坑以及plsql的安装使用中的坑