Apache ActiveMQ消息中间件的基本使用
来源:互联网 发布:jetbrains 软件更新源 编辑:程序博客网 时间:2024/04/29 19:06
Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件;由于ActiveMQ是一个纯Java程式,因此只需要操作系统支援Java虚拟机,ActiveMQ便可执行。
支持Java消息服务 (JMS) 1.1 版本
Spring Framework
集群 (Clustering)
支持的编程语言包括:C、C++、C#、Delphi、Erlang、Adobe Flash、Haskell、Java、JavaScript、Perl、PHP、Pike、Python和Ruby [1]
协议支持包括:OpenWire、REST、STOMP、WS-Notification、XMPP以及AMQP
好,我们先写个demo来试试 ActiveMQ的效果.
首先我们要下载ActiveMQ,下载地址:
http://www.apache.org/dyn/closer.cgi?path=/activemq/apache-activemq/5.8.0/apache-activemq-5.8.0-bin.zip
解压后,在x:/apache-activemq-5.8.0-bin/bin 目录下执行 activemq.bat即可启动 ActiveMQ,
由于ActiveMQ内置了Jetty web服务器,当ActiveMQ启动成功后,可以通过:http://localhost:8161/admin/访问ActiveMQ的控制台,默认的用户名和密码是:admin。
至此ActiveMQ 服务已经启动了,接下来我们先将x:/apache-activemq-5.8.0-bin/activemq-all-5.8.0.jar拷贝到你的classpath目录下,利用Java写个demo来尝试一下这个消息中间件。
JmsSender:
- ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
- Connection connection = connectionFactory.createConnection();
- connection.start();
- Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
- Destination destination = session.createQueue("Test.foo");
- MessageProducer producer = session.createProducer(destination);
- producer.setDeliveryMode(DeliveryMode.PERSISTENT);
- for(int i=0; i<100; i++) {
- int id = i+1;
- ObjectMessage message = session.createObjectMessage();
- message.setObject(new User(id, "张三"+id, "123456"));
- producer.send(message);
- }
- session.commit();
- session.close();
- connection.close();
JmsReceiver:
- ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
- Connection connection = connectionFactory.createConnection();
- connection.start();
- final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
- Destination destination = session.createQueue("Test.foo");
- MessageConsumer consumer = session.createConsumer(destination);
- //listener 方式
- consumer.setMessageListener(new MessageListener() {
- public void onMessage(Message msg) {
- ObjectMessage message = (ObjectMessage) msg;
- //TODO something....
- try {
- User user = (User) message.getObject();
- System.out.println("收到消息:"+user);
- } catch (JMSException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try {
- session.commit();
- } catch (JMSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- TimeUnit.MINUTES.sleep(1);
- session.close();
- connection.close();
运行后,得到如下消息:
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
收到消息:User [id=1, username=张三1, password=123456, now=Fri Jun 28 12:04:32 CST 2013]
收到消息:User [id=2, username=张三2, password=123456, now=Fri Jun 28 12:04:33 CST 2013]
.......
- Apache ActiveMQ消息中间件的基本使用
- Apache ActiveMQ消息中间件的基本使用
- Apache ActiveMQ消息中间件的基本使用
- Apache ActiveMQ消息中间件的基本使用
- JMS Apache ActiveMQ(消息中间件)使用攻略
- 中间件技术ActiveMQ消息中间件的使用
- 消息中间件--ActiveMQ的使用与部署
- [ActiveMQ]消息中间件在Java中的使用
- java常用的消息中间件activeMQ
- activemq消息中间件
- 初识ActiveMQ消息中间件
- 消息队列中间件ActiveMQ
- 消息中间件-ActiveMQ
- 初识ActiveMQ消息中间件
- 消息中间件 ActiveMQ基础
- 中间件 消息队列 ActiveMQ
- 消息中间件入门(ActiveMQ)
- [转]使用 Apache MINA2 实现 Web 系统的消息中间件
- 【转-listview】ListView工作原理完全解析
- 图像去噪 使用dct变换进行去噪
- @html.ActionLink的几种参数格式
- Same Tree
- 对C++中虚函数的应用所带来的隐藏和重写的探讨
- Apache ActiveMQ消息中间件的基本使用
- ZOJ 1649 Rescue BFS (搜索)
- Android的权限机制总结
- temp
- Java基础查漏补缺:(String篇)一个面试题问倒了我,原来String并不简单
- Android 、切换主题的实现
- VC编程实现色彩空间XYZ与LAB相互转换-----改正版
- 卡特尔16中人格分析,测试题,答案,以及解析
- epoll 或者 kqueue 的原理是什么?