activemq安装与简单消息发送接收实例
安装环境:
Activemq5.11.1, jdk1.7(activemq5.11.1版本需要jdk升级到1.7),虚拟机: 192.168.147.131
[root@localhost software]# pwd
/export/software
[root@localhost software]# tar -zxvf apache-activemq-5.11.-bin.tar.gz
[root@localhost software]# mv apache-activemq-5.11. /usr/local
配置Nginx代理Activemq后台管理应用默认绑定的8161端口
upstream tomcat_tools.activemq.local {
server 127.0.0.1: weight= max_fails= fail_timeout=300s;
}
server {
listen ;
server_name tools.activemq.local.com;
root /usr/local/apache-activemq-5.11./webapps/;
access_log /usr/local/apache-activemq-5.11./logs/tools.activemq.local.com_access.log main;
error_log /usr/local/apache-activemq-5.11./logs/tools.activemq.local.com_error.log warn;
error_page /40x.html;
location / {
index index.html index.htm;
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_tools.activemq.local;
}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点,
#如果频繁更新,则可以设置得小一点。
expires 30d;
}
}
重启nginx
启动activemq
[root@localhost linux-x86-]# pwd
/usr/local/apache-activemq-5.11./bin/linux-x86-
[root@localhost linux-x86-]# ./activemq start
配置host[192.168.147.131 tools.activemq.local.com]

登录activemq的后台,默认账号 admin/admin
http://tools.activemq.local.com/admin 实例展示MQ消息的发送和接收[消息类型分为queue 和 Topic]
pom引入
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.11.1</version>
</dependency>
Queue类型消息 1、定义消息destination和brokerUrl[61616为activemq用于消息通讯的端口]
public class Constant {
public static final String brokerURL = "tcp://192.168.147.131:61616";
public static final String queueDestination = "testQueue";
}
2、编写消息的发送程序
package com.mq.base.queue;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; /**
* created on 2015/6/4
* @author dennisit@163.com
* @version 1.0
*/
public class MqSender { public static void main(String[] args) throws JMSException {
// 默认的账号和密码为null
String username = ActiveMQConnection.DEFAULT_USER;
String password = ActiveMQConnection.DEFAULT_PASSWORD;
// 初始化连接工厂, DEFAULT_BROKER_URL =failover://tcp://localhost:61616
ConnectionFactory factory = new ActiveMQConnectionFactory(username, password, Constant.brokerURL);
// 创建连接
Connection connection = factory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息主题Queue
Destination destination = session.createQueue(Constant.queueDestination);
// MessageProducer负责发送消息
MessageProducer producer = session.createProducer(destination);
// 消息不持久化
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
ObjectMessage message = session.createObjectMessage("hello world...");
producer.send(message);
// 只有commit之后,消息才会进入队列
session.commit();
System.out.println("send...");
// 测试状态,这里把关闭会话和连接注释掉了。
// session.close();
// connection.close();
}
}
执行消息发送,在管理后台查看

3、编写消息的消费程序
package com.mq.base.queue; import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; /**
* created on 2015/6/4
* @author dennisit@163.com
* @version 1.0
*/
public class MqReceiver { public static void main(String[] args) throws JMSException {
// 默认的账号和密码为null
String username = ActiveMQConnection.DEFAULT_USER;
String password = ActiveMQConnection.DEFAULT_PASSWORD;
// 初始化连接工厂, DEFAULT_BROKER_URL =failover://tcp://localhost:61616
ConnectionFactory factory = new ActiveMQConnectionFactory(username, password, Constant.brokerURL);
// 创建连接
Connection connection = factory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(Constant.queueDestination);
// MessageConsumer负责接受消息
MessageConsumer consumer = session.createConsumer(destination);
ObjectMessage message = (ObjectMessage)consumer.receive();
if (null != message) {
String messageString = (String)message.getObject();
System.out.println("Receive : " + messageString);
}
// 测试状态,这里把关闭会话和连接注释掉了。
// session.close();
// connection.close();
}
}
执行这段代码会输出接收到的消息内容:

管理后台在查看queue中心结果如下:

Topic类型消息
1、定义消息destination和brokerUrl[61616为activemq用于消息通讯的端口]
public class Constant {
public static final String brokerURL = "tcp://192.168.147.131:61616";
public static final String topicDestination = "testTopic";
}
2、编写消息生产者
package com.mq.base.topic; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /**
* created on 2015/6/4
* @author dennisit@163.com
* @version 1.0
*/
public class MqSender { public static void main(String[] args) throws JMSException {
// 默认的账号和密码为null
String username = ActiveMQConnection.DEFAULT_USER;
String password = ActiveMQConnection.DEFAULT_PASSWORD;
// 初始化连接工厂, DEFAULT_BROKER_URL =failover://tcp://localhost:61616
ConnectionFactory factory = new ActiveMQConnectionFactory(username, password, com.mq.base.queue.Constant.brokerURL);
// 创建连接
Connection connection = factory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息主题Topic,和Queue的区别就在此
Destination destination = session.createTopic(Constant.topicDestination);
// MessageProducer负责发送消息
MessageProducer producer = session.createProducer(destination);
// 消息不持久化
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage(); // createObjectMessage("hello world...");
message.setStringProperty("msgId","topicMessage");
producer.send(message);
// 只有commit之后,消息才会进入队列
session.commit();
System.out.println("send...");
// 测试状态,这里把关闭会话和连接注释掉了。
// session.close();
// connection.close();
}
}
3、编写消息消费者
package com.mq.base.topic; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /**
* created on 2015/6/4
* @author dennisit@163.com
* @version 1.0
*/
public class MqReceiver { public static void main(String[] args) throws JMSException {
// 默认的账号和密码为null
String username = ActiveMQConnection.DEFAULT_USER;
String password = ActiveMQConnection.DEFAULT_PASSWORD;
// 初始化连接工厂, DEFAULT_BROKER_URL =failover://tcp://localhost:61616
ConnectionFactory factory = new ActiveMQConnectionFactory(username, password, com.mq.base.queue.Constant.brokerURL);
// 创建连接
Connection connection = factory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic(Constant.topicDestination);
// MessageConsumer负责接受消息
MessageConsumer consumer = session.createConsumer(destination);
TextMessage message = (TextMessage)consumer.receive();
if (null != message) {
String messageString = message.getStringProperty("msgId");
System.out.println("Receive : " + messageString);
session.commit();
}
// 测试状态,这里把关闭会话和连接注释掉了。
// session.close();
// connection.close();
}
}
先启动消费者:

启动生产者,生产消息,此时会接收到消息如图:

观察topic后台管理

Queue模型消息和Topic模型消息区别

queue[点对点模型]
1、只有一个消费者
每条消息只有一个消费者,如果这条消息被消费,那么其它消费者不能接受到此消息。
2、时间无关性
消息的消费和时间无关,只要消息被发送了,在消息过期之前,如果没有其他消费者消费了这个消息,那么客户端可以在任何时候来消费这条消息。
3、消费者必须确认
消费者收到消息之后,必须向Message Provider确认,否则会被认为消息没有被消费,仍然可以被其他消费者消费。可以设置自动确认。这个特点其实也是保证一条消息只能由一个消费者来消费。
4、非持久化的消息只发一次
非持久化的消息,可能会丢失,因为消息会过期,另外Message Provider可能宕机。
5、持久化的消息严格发一次
消息可以被持久化,比如持久化在文件系统或者数据库中,这样可以避免Message Provider的异常或者其它异常导致消息丢失。 Topic[发布者/订阅者模型]
1、每条消息可以有多个订阅者
2、订阅者只能消费它们订阅topic之后的消息
3、非持久化订阅,订阅者必须保持为活动状态才能使用这些消息,如果一个订阅者A断开了10分钟,那么A就会收不到这10分钟内的消息。
4、持久化订阅,Message Provider会保存这些消息,即使订阅者因为网络原因断开了,再重新连接以后,能让消费这些消息。
5、是否使用持久化订阅,需要根据业务场景判断。
转载请注明出处:[http://www.cnblogs.com/dennisit/p/4551182.html]
activemq安装与简单消息发送接收实例的更多相关文章
- ActiveMQ(2)---ActiveMQ原理分析之消息发送
持久化消息和非持久化消息的发送策略 消息同步发送和异步发送 ActiveMQ支持同步.异步两种发送模式将消息发送到broker上.同步发送过程中,发送者发送一条消息会阻塞直到broker反馈一个确认消 ...
- php实现简单消息发送+极光推送系统
前几天刚写完的一个东西,写的比较简单,没有使用其他插件,原生php+计划任务实现 极光推送的代码 /* $receiver="registration_id" : [ " ...
- ActiveMQ安装与持久化消息
activityMQ官网:http://activemq.apache.org/ 有windows版与linux版 windows版启动 在bin目录下双击activemq.bat linux版的安 ...
- rocketmq简单消息发送
有以下3种方式发送RocketMQ消息 可靠同步发送 reliable synchronous 可靠异步发送 reliable asynchronous 单向发送 one-way transmissi ...
- C++ TCP客户端网络消息发送接收同步实现
废话不多说, 直入主题, 我们在写客户单的时候希望在哪里发消息出去,然后在哪里返回消息(同步), 然后继续往下运行-, 而不是在这里发送了一个消息给服务端, 在另一个地方接受消息(异步) , 也不知道 ...
- ActiveMQ JMS实现消息发送
一.创建配置消息发送接收目的地. ActiveMQ中间件地址 JMS_BROKER_URL=failover://(tcp://192.168.1.231:61616) QUEUE_BUSP_TP_S ...
- spring整合activemq发送MQ消息[Topic模式]实例
Topic模式消息发送实例 1.pom引入 <dependency> <groupId>junit</groupId> <artifactId>juni ...
- 【ActiveMQ】ActiveMQ在Windows的安装,以及点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...
- spring整合activemq发送MQ消息[queue模式]实例
queue类型消息 pom依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</ ...
随机推荐
- python基础语法(4)
十.Python标准库 Python标准库是随Pthon附带安装的,包含了大量极其有用的模块. 1. sys模块 sys模块包含系统对应的功能 sys.argv ---包含命令行参数,第一个参数是py ...
- 有关big.LITTLE,你需要知道的十件事情
来源 问题 1:该技术能够同时打开所有核心吗? 在早期的 big.LITTLE 软件模型中(集群迁移和 CPU 迁移),软件在核心之间切换,不能同时打开所有核心.在更新的软件模型“全局任务调度”中 ...
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- [SmartFoxServer概述]使用文档
如何使用文档和例子 这份文档提供了一份关于如何通过SmartFoxServer 2X(缩写SFS2X)文档掌握要点的快速教程.在使用例子和技术文档之前,我们建议先参考以下主题内容. 不管你是Smart ...
- LightOJ1027 A Dangerous Maze(期望)
题目大概说你正在起点,面前有$n$个门,每个门有一个数字$x$,正数表示开这个门$x$分钟后会出去,负数表示开这个门$-x$分钟后会回到起点.选择门的概率是一样的且每次选择互不影响.问出去的时间期望是 ...
- 【转】Android studio 解决64K超出链接数限制问题
http://my.oschina.net/gabriel1215/blog/602608 目录[-] 使用MultiDex支持库 注意事项 结论 如果你是一个android开发者,你至少听说过的Da ...
- How to Call SharePoint 2013 API Service to Query The Lists
How to Call SharePoint 2013 API In SharePoint 2013, we can query the list by it owner service, then ...
- 为什么不能把委托(delegate)放在一个接口(interface)当中?
stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...
- fgets函数执行完成后,文件指针如何移动?
用fgets执行之后,读取了文件中的一行,这时文件位置指针是自动指向文件的下一行的开头吗,还是指向当前行的结尾?如果一行的字符串没读取完会怎样? 实例结果: 如果一行的字符串没读取完会,下一次会接着上 ...
- Java程序员的日常 —— 工作一天的收获
看题目可能是扯皮,其实还是有很多专业知识的.从最开始没有注意到设计原则,到后面的jquery实战技巧,都是今天一天碰到的问题. 每天整理一点点,每天收获一点点. 关于软件设计 在设计系统结构的时候,一 ...