ActiveMQ安装配置及使用

ActiveMQ介绍

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。 
特性列表: 
⒈ 多种语言和协议编写客户端。语言: Java,C,C++,C#,Ruby,Perl,Python,PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP 
⒉ 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务) 
⒊ 对spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性 
⒋ 通过了常见J2EE服务器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上 
⒌ 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA 
⒍ 支持通过JDBC和journal提供高速的消息持久化 
⒎ 从设计上保证了高性能的集群,客户端-服务器,点对点 
⒏ 支持Ajax 
⒐ 支持与Axis的整合 
⒑ 可以很容易的调用内嵌JMS provider,进行测试


(二)ActiveMQ安装、配置、启动、可视化界面

1、安装 
下载地址:http://activemq.apache.org/download.html 
2、配置(conf目录下) 
1)用户名密码设置 
 
2)开启jmx监控 
activemq.xml中进行如下修改 

注:这里的配置不是必须,根据需要自行配置 
3、启动 
直接运行bin目录下:activemq.bat 
4、可视化界面 
浏览器中:http://localhost:8161/admin/index.jsp 
用户名,密码在:jetty-realm.properties中设置


(三)点对点式消息队列(Queue)

消息生产者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class QueueProducer { public static void main(String[] args) {
//连接信息设置
String username = "system";
String password = "manager";
String brokerURL = "failover://tcp://localhost:61616";
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息的目的地
Destination destination = null;
//消息生产者
MessageProducer messageProducer = null;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL); try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建一个名称为QueueTest的消息队列
destination = session.createQueue("QueueTest");
//创建消息生产者
messageProducer = session.createProducer(destination);
//发送消息
TextMessage message = null;
for (int i=0; i<10; i++) {
//创建要发送的文本信息
message = session.createTextMessage("Queue消息测试" +(i+1));
//通过消息生产者发出消息
messageProducer.send(message);
System.out.println("发送成功:" + message.getText());
}
session.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null != connection){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
} } }

消息消费者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class QueueConsumer { public static void main(String[] args) {
//连接信息设置
String username = "system";
String password = "manager";
String brokerURL = "failover://tcp://localhost:61616";
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息的目的地
Destination destination = null;
//消息消费者
MessageConsumer messageConsumer = null;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL); try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建一个连接QueueTest的消息队列
destination = session.createQueue("QueueTest");
//创建消息消费者
messageConsumer = session.createConsumer(destination); while (true) {
TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
if(textMessage != null){
System.out.println("成功接收消息:" + textMessage.getText());
}else {
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}

(四)主题发布订阅式(Topic)

主题发布者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic; import org.apache.activemq.ActiveMQConnectionFactory; public class TopicProducer { public static void main(String[] args) {
//连接信息设置
String username = "system";
String password = "manager";
String brokerURL = "failover://tcp://localhost:61616";
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息的主题
Topic topic = null;
//消息生产者
MessageProducer messageProducer = null;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);
try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建名为TopicTest的主题
topic = session.createTopic("TopicTest");
//创建主题生产者
messageProducer = session.createProducer(topic);
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//不将数据持久化
//发送主题
TextMessage message = null;
for (int i=0; i<10; i++) {
//创建要发送的文本信息
message = session.createTextMessage("Topic主题测试" +(i+1));
//通过主题生产者发出消息
messageProducer.send(message);
System.out.println("发送成功:" + message.getText());
}
session.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null != connection){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
} }

主题订阅者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic; import org.apache.activemq.ActiveMQConnectionFactory; public class TopicConsumer { public static void main(String[] args) {
//连接信息设置
String username = "system";
String password = "manager";
String brokerURL = "failover://tcp://localhost:61616";
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//主题的目的地
Topic topic = null;
//主题消费者
MessageConsumer messageConsumer = null;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL); try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建一个连接TopicTest的主题
topic = session.createTopic("TopicTest");
//创建主题消费者
messageConsumer = session.createConsumer(topic); messageConsumer.setMessageListener(new MyMessageListener());
} catch (JMSException e) {
e.printStackTrace();
}
} } class MyMessageListener implements MessageListener { @Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收订阅主题:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
} }

注: 
1、代码中所需额外jar包在下载的mq文件夹中,例如我使用的:activemq-all-5.9.0.jar 
2、对于消息队列,异步生产和消费;对于主题发布订阅要先启动订阅者进行监听,然后在发布方可接收到订阅主题 
3、关于Queue与Topic的具体区别,详见http://blog.csdn.net/qq_21033663/article/details/52458305

ActiveMQ安装配置及使用 转发 https://www.cnblogs.com/hushaojun/p/6016709.html的更多相关文章

  1. ActiveMQ安装配置及使用

    ActiveMQ介绍 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管J ...

  2. 1、windows安装npm教程 --参考自https://www.cnblogs.com/jianguo221/p/11487532.html

    windows安装npm教程   1.在使用之前,先类掌握3个东西,明白它们是用来干什么的: npm:  nodejs 下的包管理器. webpack: 它主要用途是通过CommonJS 的语法把所有 ...

  3. ActiveMQ安装配置及实例

    本文可作为吴水成老师,dubbo课程第21节的学习笔记. ActiveMQ的介绍及功能 参考百度 ActiveMQ的下载 https://activemq.apache.org/activemq-51 ...

  4. (jms)ActiveMQ 安装配置.

    前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...

  5. [转帖]Nginx安装及配置详解 From https://www.cnblogs.com/zhouxinfei/p/7862285.html

    Nginx安装及配置详解   nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP ...

  6. Nginx 安装配置证书,设置HTTPS站点

    详细配置如下: server { listen 80; server_name shwww.net www.shwww.net; return 301 https://www.shwww.net$re ...

  7. ActiveMQ发布订阅模式 转发 https://www.cnblogs.com/madyina/p/4127144.html

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  8. Centos7:ActiveMQ安装,配置及使用

    解压缩ActiveMQ 的压缩包 使用 命令在bin目录下 ./activemq stat//开启 ./activemq stop//关闭 ./activemq status//状态 进入管理后台 U ...

  9. ElasticSearch(站内搜索) 转发 https://www.cnblogs.com/xibei666/p/5929970.html

    简介 Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据.它可以用于全文搜索,结构化搜索以及分析,当然你也可以将这三者进行组合.Elasticse ...

随机推荐

  1. MVC模式-----struts2框架

    MVC模式-----struts2框架 一.struts MVC模式 1.概述 MVC是模型(model)---视图model(view)---控制器(control)的缩写,是一种用于将逻辑.数据和 ...

  2. 大事记 - 安卓微信浏览器 video 标签层级过高

    // 为什么叫<大事记>? // 以前总有面试官问这样一个问题:“你在项目中遇到过最头疼的问题是什么,是怎么解决的?” // 当时总觉得,已解决的问题都算不上头疼,所以回答总是不尽人意. ...

  3. 为什么用bower 安装bootstrap而不用npm来安装?

    NPM(node package manager),通常称为node包管理器.顾名思义,它的主要功能就是管理node包,包括:安装.卸载.更新.查看.搜索.发布等. npm的背后,是基于couchdb ...

  4. 移动前端—H5实现图片先压缩再上传

    在做移动端图片上传的时候,用户传的都是手机本地图片,而本地图片一般都相对比较大,拿iphone6来说,平时拍很多图片都是一两M的,如果直接这样上传,那图片就太大了,如果用户用的是移动流量,完全把图片上 ...

  5. 小米手机Toast显示带应用名称问题解决方法

    近期为了适配刘海屏,向公司申购了一步小米8的手机,然后测试人员那边测出来一堆适配的问题,其中有一个每一个Toast会显示app的名称+显示的内容,然后网上查找了一下解决方法记录一下,顺便封装了Toas ...

  6. python语言学习--2

    第三天1. python代码缩进规则:具有相同缩进的代码被视为代码块,4个空格, 不要使用Tab,更不要混合Tab和空格,否则很容易造成因为缩进引起的语法错误. 2.list:[...] 用(名称任意 ...

  7. git 入门教程之github 教程

    github 教程 github 是一个基于 git 的代码托管平台,是平时工作学习的好帮手,学会如何用好 github 网站能够帮助我们更好分享代码或者与其他开发人员合作. 注册 github 账号 ...

  8. You (root) are not allowed to access to (crontab) because of pam configuration

    巡检发现一台Linux服务器上的作业没有如期发送邮件,登录服务器检查后发现作业并没有执行,于是检查一下crontab的设置.结果发现如下错误: [root@mylnx2 ~]# crontab -l ...

  9. spring4笔记----spring4国际化

    <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www. ...

  10. linux 环境变量设置

    sudo gedit ~/.bashrc source ~/.bashrc