mq的消息调用
2016-06-07 15:16
204 查看
本文来自: http://blog.csdn.net/jason5186/article/details/9196041
本文中用到的代码和java类可以到我的网盘下载:http://pan.baidu.com/s/1gfwLjqz
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:
jms在创建Session时可以有两个参数,第一个参数是是否使用事务,第二个参数是消费者向发送者确认消息已经接收的方式:
[java] view
plain copy
print?
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
//
jms在创建Session时可以有两个参数,第一个参数是是否使用事务,第二个参数是消费者向发送者确认消息已经接收的方式:
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:
[java] view
plain copy
print?
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();
监听起来后,不能关闭session和connection ,否则会监听不到消息,一般项目中,如果不想和主项目分开,都是和主项目一起放的,这样,只要在web。xml配置一个监听
然后调用此接收的方法,监听会一直开着,等着消息过来,不需要外面再套一个while循环。
session.close();
connection.close();
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]
.......
这个文章也不错:
http://www.cnblogs.com/zhuhongbao/archive/2013/06/09/3128272.html
此篇也是不错的文章
http://blog.csdn.net/keda8997110/article/details/13997859
本文中用到的代码和java类可以到我的网盘下载:http://pan.baidu.com/s/1gfwLjqz
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:
jms在创建Session时可以有两个参数,第一个参数是是否使用事务,第二个参数是消费者向发送者确认消息已经接收的方式:
[java] view
plain copy
print?
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
//
jms在创建Session时可以有两个参数,第一个参数是是否使用事务,第二个参数是消费者向发送者确认消息已经接收的方式:
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:
[java] view
plain copy
print?
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();
监听起来后,不能关闭session和connection ,否则会监听不到消息,一般项目中,如果不想和主项目分开,都是和主项目一起放的,这样,只要在web。xml配置一个监听
然后调用此接收的方法,监听会一直开着,等着消息过来,不需要外面再套一个while循环。
session.close();
connection.close();
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]
.......
这个文章也不错:
http://www.cnblogs.com/zhuhongbao/archive/2013/06/09/3128272.html
此篇也是不错的文章
http://blog.csdn.net/keda8997110/article/details/13997859
相关文章推荐
- tjut 5297
- JAVA进行密码加盐
- Mirantis对OpenStack的性能测试:高并发创建75000台虚拟机
- 时间选择器
- 史上最详细的CocoaPods安装教程
- Popstar小游戏1(Popstar的POP精灵的创建)
- 监听器获取Spring配置文件的方法
- 关于虚拟机出错的解决办法——The VMware Authorization Service is not running.
- Design Support Library---->NavigationView
- 理解Spark RDD中的aggregate函数
- Mysql常用命令
- Java学习笔记之字符串常用方法
- javascript 中的 parameter vs arguments
- centos下hadoop2.6.0集群搭建详细过程
- Java线程之线程池
- 分治算法---poj1723【士兵排队】Soldier
- 将保护清理不良资产的银行家:Arun Jaitley在评审会议
- 对onreadystatechange属性的理解
- 数据结构之堆和栈
- OpenStack Grizzly Architecture