ActiveMQ的简单使用
2016-07-15 15:35
411 查看
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ
是一个完全支持JMS1.1和J2EE 1.4规范的
JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
应用的协议包括: OpenWire,Stomp REST,WS Notification,XMPP
支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
ActiveMQ支持与Spring集成
支持的消息模式包括点对点、发布者与订阅。
1、 创建简单的消息发送与接受
1.1、 创建一个消息生产者
2. publicstaticvoidmain(String[]
args){
3.
4. ConnectionFactory connectionFactory;
// 连接工厂
5. Connection connection =
null;//
连接
6. Session
session; // 会话接受或者发送消息的线程
7. Destination destination;
// 消息的目的地
8. MessageProducer messageProducer;
// 消息生产者
9.
10. // 实例化连接工厂
11. connectionFactory=newActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD,
JMSProducer.BROKEURL);
12.
13. try{
14. connection=connectionFactory.createConnection();//
通过连接工厂获取连接
15. connection.start();
// 启动连接
16. session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);//
创建Session
17. destination=session.createQueue("MessageQueue1");
// 创建消息队列
18. messageProducer=session.createProducer(destination);
// 创建消息生产者
19. sendMessage(session,messageProducer);//
发送消息
20. session.commit();
21. } catch (Exception
e){
22. // TODO Auto-generated catchblock
23. e.printStackTrace();
24. } finally{
25. if(connection!=null){
26. try {
27. connection.close();
28. } catch (JMSException
e){
29. // TODO Auto-generated catchblock
30. e.printStackTrace();
31. }
32. }
33. }
34. }
35.
36. /**
37. * 发送消息
38. * @param session
39. * @param messageProducer
40. * @throws Exception
41. */
42. publicstaticvoidsendMessage(Session
session,MessageProducermessageProducer)throws Exception{
43. for(int
i=0;i<10;i++){
44. TextMessage message=session.createTextMessage("ActiveMQ
发送的消息"+i);
45. System.out.println("发送第" +
i + "条消息" );
46. messageProducer.send(message);
47. }
48. }
发送成功在可以在控制台查看消息数
2.2、创建一个消费者
public
static void main(String[]
args) {
ConnectionFactoryconnectionFactory;//
连接工厂
Connectionconnection=
null;//
连接
Sessionsession;//
会话接受或者发送消息的线程
Destinationdestination;//
消息的目的地
MessageConsumermessageConsumer;
// 消息的消费者
// 实例化连接工厂
connectionFactory=newActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD,
JMSConsumer2.BROKEURL);
try {
connection=connectionFactory.createConnection();
// 通过连接工厂获取连接
connection.start();
// 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//
创建Session
destination=session.createQueue("MessageQueue1");
// 创建连接的消息队列
messageConsumer=session.createConsumer(destination);
// 创建消息消费者
messageConsumer.setMessageListener(new Listener());
// 注册消息监听
}catch(JMSException
e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
此时的控制台
1、 ActiveMQ消息的发布与订阅。
首先需要创建多个订阅者,而订阅者应该先订阅消息。
public
static void main(String[]
args) {
ConnectionFactoryconnectionFactory;//
连接工厂
Connectionconnection=
null;//
连接
Sessionsession;//
会话接受或者发送消息的线程
Destinationdestination;//
消息的目的地
MessageConsumermessageConsumer;//
消息的消费者
// 实例化连接工厂
connectionFactory=newActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD,
JMSConsumer.BROKEURL);
try {
connection=connectionFactory.createConnection();
// 通过连接工厂获取连接
connection.start();
// 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//
创建Session
destination=session.createTopic("TopicMessge");
messageConsumer=session.createConsumer(destination);
// 创建消息消费者
messageConsumer.setMessageListener(new Listener());
// 注册消息监听
}catch(JMSException
e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这里可以创建多个订阅者,代码可以一样。然后看你的控制台。
接下来开始生产消息,代码和上面的生产差不多。只是主题需要改变下
destination=session.createTopic("TopicMessge");
messageProducer=session.createProducer(destination);
测试订阅着会同步收到消息:
管理台也会变更消息
非常简单!
是一个完全支持JMS1.1和J2EE 1.4规范的
JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
应用的协议包括: OpenWire,Stomp REST,WS Notification,XMPP
支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
ActiveMQ支持与Spring集成
支持的消息模式包括点对点、发布者与订阅。
1、 创建简单的消息发送与接受
1.1、 创建一个消息生产者
2. publicstaticvoidmain(String[]
args){
3.
4. ConnectionFactory connectionFactory;
// 连接工厂
5. Connection connection =
null;//
连接
6. Session
session; // 会话接受或者发送消息的线程
7. Destination destination;
// 消息的目的地
8. MessageProducer messageProducer;
// 消息生产者
9.
10. // 实例化连接工厂
11. connectionFactory=newActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD,
JMSProducer.BROKEURL);
12.
13. try{
14. connection=connectionFactory.createConnection();//
通过连接工厂获取连接
15. connection.start();
// 启动连接
16. session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);//
创建Session
17. destination=session.createQueue("MessageQueue1");
// 创建消息队列
18. messageProducer=session.createProducer(destination);
// 创建消息生产者
19. sendMessage(session,messageProducer);//
发送消息
20. session.commit();
21. } catch (Exception
e){
22. // TODO Auto-generated catchblock
23. e.printStackTrace();
24. } finally{
25. if(connection!=null){
26. try {
27. connection.close();
28. } catch (JMSException
e){
29. // TODO Auto-generated catchblock
30. e.printStackTrace();
31. }
32. }
33. }
34. }
35.
36. /**
37. * 发送消息
38. * @param session
39. * @param messageProducer
40. * @throws Exception
41. */
42. publicstaticvoidsendMessage(Session
session,MessageProducermessageProducer)throws Exception{
43. for(int
i=0;i<10;i++){
44. TextMessage message=session.createTextMessage("ActiveMQ
发送的消息"+i);
45. System.out.println("发送第" +
i + "条消息" );
46. messageProducer.send(message);
47. }
48. }
发送成功在可以在控制台查看消息数
2.2、创建一个消费者
public
static void main(String[]
args) {
ConnectionFactoryconnectionFactory;//
连接工厂
Connectionconnection=
null;//
连接
Sessionsession;//
会话接受或者发送消息的线程
Destinationdestination;//
消息的目的地
MessageConsumermessageConsumer;
// 消息的消费者
// 实例化连接工厂
connectionFactory=newActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD,
JMSConsumer2.BROKEURL);
try {
connection=connectionFactory.createConnection();
// 通过连接工厂获取连接
connection.start();
// 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//
创建Session
destination=session.createQueue("MessageQueue1");
// 创建连接的消息队列
messageConsumer=session.createConsumer(destination);
// 创建消息消费者
messageConsumer.setMessageListener(new Listener());
// 注册消息监听
}catch(JMSException
e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
此时的控制台
1、 ActiveMQ消息的发布与订阅。
首先需要创建多个订阅者,而订阅者应该先订阅消息。
public
static void main(String[]
args) {
ConnectionFactoryconnectionFactory;//
连接工厂
Connectionconnection=
null;//
连接
Sessionsession;//
会话接受或者发送消息的线程
Destinationdestination;//
消息的目的地
MessageConsumermessageConsumer;//
消息的消费者
// 实例化连接工厂
connectionFactory=newActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD,
JMSConsumer.BROKEURL);
try {
connection=connectionFactory.createConnection();
// 通过连接工厂获取连接
connection.start();
// 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//
创建Session
destination=session.createTopic("TopicMessge");
messageConsumer=session.createConsumer(destination);
// 创建消息消费者
messageConsumer.setMessageListener(new Listener());
// 注册消息监听
}catch(JMSException
e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这里可以创建多个订阅者,代码可以一样。然后看你的控制台。
接下来开始生产消息,代码和上面的生产差不多。只是主题需要改变下
destination=session.createTopic("TopicMessge");
messageProducer=session.createProducer(destination);
测试订阅着会同步收到消息:
管理台也会变更消息
非常简单!
相关文章推荐
- 上传文件到服务器
- 【spring】spring reference doc4.3.1 研读 <一>元数据配置以及Environment
- JavaScript单元测试
- jq的异步请求
- RabbitMQ学习(五)之主题topic(java)
- Android 生成带图片的二维码
- java序列化接口Serializable的使用
- Android自定义View之画笔与画布
- 阿里中间件——diamond
- 丑数
- TCP/UDP报文格式
- How to run jetty in IDEA
- 百度地图sdk无法定位错误总结
- 学习记录-Qt的多工程管理
- shell脚本输出颜色字体
- 操作系统--存储器管理
- 统计字符串中有‘0’——‘9’的字符个数
- 为什么time()函数要返回1970年1月1日00点00分00秒开始到现在的秒数
- studio2.0+ndk-r11 中jni编译问题
- iwconfig的用法(转)