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

AcitveMQ的安装

官网下载地址:http://activemq.apache.org/download-archives.html

由于我是在Windows系统下学习AcitveMQ,所以我下载的是:apache-activemq-5.6.0-bin.zip,下载好进行解压缩,目录如下:

AcitveMQ的启动

在解压得到的bin目录中包含一个名为activemq.bat的脚本,直接运行这个脚本就可以启动一个broker,启动之前别忘了需先配置好%JAVA_HOME%环境变量。

AcitveMQ程序内部利用jetty容器部署了一个web管理界面,默认服务启动地址:http://127.0.0.1:8161/admin/

若要开启管理后台登录密码验证,可按以下步骤进行配置。

修改jetty服务器的配置文件,apache-activemq-5.6.0\conf\jetty.xml,需将"authenticate"属性设置为"true"。

    <bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint"><property name="name" value="BASIC" /><property name="roles" value="admin" /><property name="authenticate" value="true" /></bean>

再修改管理后台的登录用户名密码配置文件,apache-activemq-5.6.0\conf\jetty-realm.properties。

## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements.  See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License.  You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin

温馨提示:windows操作系统下,若开启Internet connection share(ICS)服务会占用61616端口,会导致ActiveMQ无法启动。

工程搭建

搭建ActiveMQ工程很简单,只需要创建一个Java project,并将ActiveMQ安装根目录下的 activemq-all-5.6.0.jar 包添加到项目的构建路径中,创建好的activeMQ_HelloWorld工程目录层级如下。

先创建生产者ActiveMQProducer,其完整代码如下。

package com.pengjunlee.activemq;import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class ActiveMQProducer {public static void main(String[] args) throws Exception {// 第一步:创建ConnectionFactory,需要指定用户名、密码、以及要连接的地址ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,ActiveMQConnectionFactory.DEFAULT_PASSWORD,"tcp://localhost:61616");// 第二步:利用ConnectionFactory创建一个Connection连接,并且调用Connection的start()方法开启连接Connection connection = connectionFactory.createConnection();connection.start();// 第三步:通过Connection对象创建Session会话(上下文环境对象),用于接收消息// createSession的两个参数:(是否启用事务,消息确认模式)Session session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);// 第四步:通过Session创建Destination对象,目的地指的是生产消息的目标和消费消息的来源// 在PTP模式中,Destination被称作Queue队列,在Pub/Sub模式中,Destination被称作Topic主题Destination destination = session.createQueue("queue1");// 第五步:通过Session对象创建消息生产者和消息消费者(MessageProducer/MessageConsumer)MessageProducer messageProducer = session.createProducer(destination);// 第六步:利用messageProducer的setDeliveryMode()方法设置消息是否需要持久化messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 第七步:通过Session创建Message消息TextMessage textMessage = session.createTextMessage("this is a activeMQ Message!");// 第八步:调用MessageProducer的send()方法发送消息messageProducer.send(textMessage);// 第九步:关闭连接,释放资源if (connection != null) {connection.close();}}}

运行ActiveMQProducer,登录http://127.0.0.1:8161/admin/管理后台,其队列信息如下。

再创建消息消费者ActiveMQConsumer,其完整代码如下。

package com.pengjunlee.activemq;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 ActiveMQConsumer {public static void main(String[] args) throws JMSException {// 第一步:创建ConnectionFactory,需要指定用户名、密码、以及要连接的地址ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,ActiveMQConnectionFactory.DEFAULT_PASSWORD,"tcp://localhost:61616");// 第二步:利用ConnectionFactory创建一个Connection连接,并且调用Connection的start()方法开启连接Connection connection = connectionFactory.createConnection();connection.start();// 第三步:通过Connection对象创建Session会话(上下文环境对象),用于接收消息// createSession的两个参数:(是否启用事务,消息确认模式)Session session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);// 第四步:通过Session创建Destination对象,目的地指的是生产消息的目标和消费消息的来源// 在PTP模式中,Destination被称作Queue队列,在Pub/Sub模式中,Destination被称作Topic主题Destination destination = session.createQueue("queue1");// 第五步:通过Session对象创建消息生产者和消息消费者(MessageConsumer/MessageConsumer)MessageConsumer messageConsumer = session.createConsumer(destination);// 第六步:调用MessageConsumer的receive()方法获取消息TextMessage textMessage = (TextMessage) messageConsumer.receive();System.out.println(textMessage.getText());// 第七步:关闭连接,释放资源if (connection != null) {connection.close();}}
}

运行ActiveMQConsumer,登录后台再次查看队列信息。

ConnectionFactory连接密码认证

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,ActiveMQConnectionFactory.DEFAULT_PASSWORD,"tcp://localhost:61616");

在new ActiveMQConnectionFactory时若要开启登录密码验证,可按以下步骤进行配置。

修改apache-activemq-5.6.0\conf\activemq.xml,在<systemUsage>节点前面添加如下配置。

<plugins>  <simpleAuthenticationPlugin>  <users>  <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>  </users>  </simpleAuthenticationPlugin>  </plugins>  <!--The systemUsage controls the maximum amount of space the broker willuse before slowing down producers. For more information, see:http://activemq.apache.org/producer-flow-control.htmlIf using ActiveMQ embedded - the following limits could safely be used:<systemUsage><systemUsage><memoryUsage><memoryUsage limit="20 mb"/></memoryUsage><storeUsage><storeUsage limit="1 gb"/></storeUsage><tempUsage><tempUsage limit="100 mb"/></tempUsage></systemUsage></systemUsage>-->

在apache-activemq-5.6.0\conf\credentials.properties文件中指定用户名和密码。

## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements.  See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License.  You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------# Defines credentials that will be used by components (like web console) to access the brokeractivemq.username=activeMQ
activemq.password=123456
guest.password=password

在new ActiveMQConnectionFactory时需指定用户名和密码。

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("activeMQ","123456","tcp://localhost:61616");

AcitveMQ--HelloWorld相关推荐

  1. Docker安装Apache与运行简单的web服务——httpd helloworld

    Docker运行简单的web服务--httpd helloworld目录[阅读时间:约5分钟] 一.Docker简介 二.Docker的安装与配置[CentOS环境] 三.Docker运行简单的web ...

  2. CentOS Docker安装配置部署Golang web helloworld

    目录[阅读时间:约5分钟] 一.Docker简介 二.Docker的安装与配置[CentOS环境] 三.Docker部署Golang web helloworld 四.Docker与虚拟机的区别 五. ...

  3. 简单图文配置golang+vscode【win10/centos7+golang helloworld+解决install failed等情况】

    博客目录(阅读时间:10分钟) 一.win10 0.系统环境 1. win10配置golang环境 ①下载相关软件 ②创建gowork工作空间 ③配置环境变量(GOPATH+PATH) ④验证环境配置 ...

  4. idea java jni 调试_IntelliJ IDEA平台下JNI编程(一)—HelloWorld篇

    JNI(Java Native Interface),出于学习JNI的目的,为了能够更方便快速地运行程序.本文的是在IDEA中进行,而不在AndroidStudio,这样能够对NDK的工作过程有个更深 ...

  5. Apache模块开发helloworld无错版

    环境:CentOS 5.4 第一步:安装Apache的apxs 首先来介绍下apache的一个工具apxs.apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程 ...

  6. 1.1GTK+ 的简单程序HelloWorld

    1.1GTK+ 的简单程序HelloWorld 编译执行如图所看到的:

  7. 项目构建之maven篇:2.HelloWorld项目构建过程

    文件结构说明: 项目构建生命周期: 清理 编译 測试 打包 执行 部署 清理与编译 hello\pom.xml POM:Project Object Model,项目对象模型 pom.xml与ant的 ...

  8. Python基础01-Python环境搭建与HelloWorld

    目录 从今天开始学习Python Python环境搭建 安装gcc Python源码包安装 开始Python第一个代码HelloWorld! 从今天开始学习Python 为啥选择Python,可能是跟 ...

  9. 从HelloWorld看Knative Serving代码实现

    为什么80%的码农都做不了架构师?>>>    摘要: Knative Serving以Kubernetes和Istio为基础,支持无服务器应用程序和函数的部署并提供服务.我们从部署 ...

  10. flask源码学习-helloworld与本地启动流程

    Flask源码分析 本文环境python3.5.2,flask-1.0.2. Flask的初探 首先,在项目文件夹下建立flask_run.py文件,然后写入如下, from flask import ...

最新文章

  1. iOS常用CGRect的交错,边缘,中心的检测
  2. php $interval,如何在PHP中使用setInterval?
  3. vins中imu融合_VINS-Mono代码分析与总结(最终版)
  4. 2-4 测试案例helloWorld
  5. SAP CRM呼叫中心和社交媒体集成的所有BC set实现列表
  6. 图论--二分图--二分图的定义及其判断定
  7. h2 mysql 兼容_H2内存数据库对sql语句的支持问题 sql放到mysql数据库中能跑
  8. python 文件写入多个参数_pandas 把数据写入txt文件每行固定写入一定数量的值方法...
  9. 橡皮筋还能发动载人飞行器?
  10. 解决Unity3D导出apk失败:Failed to re-package resources
  11. android aar jar制作,AndroidStudio aar、jar生成及其引用
  12. OpenGL ES (三)着色器和程序
  13. 计算机网络 第一章 计算机网络和因特网
  14. 递归装饰器正则运算模块
  15. 【图像配准】基于matlab Harris+SIFT图像配准【含Matlab源码 1532期】
  16. 关于在联想电脑管家更新网卡驱动后无法显示可用网络wifi的问题!
  17. linux安装富士施乐打印机驱动下载,linux s1810打印机驱动肿么安装
  18. Linux daemontools安装及使用
  19. 搭建STM32开发环境
  20. 计算机关机 休眠睡眠有什么区别,电脑“关机”、“睡眠”、“休眠”三者区别是什么...

热门文章

  1. JetBrains又出编程神器!
  2. 基于Oneplus 7 Pro的Flyme9&MIUI12.5刷机教程
  3. Centos 7搭建DNS(5)智能DNS
  4. 电动车电池放电口能冲电吗充电口和放电口是同一个吗
  5. Java 使用Reactive Redis
  6. MCUXpresso开发RT1060(1)——使用RGB接口LCD
  7. emmc和MMC的区别
  8. .NET 程序员有家了,微软推出官方技术社区论坛
  9. 重磅 !微软官方出了免费 Python 视频教程
  10. Time-Ordered Recent Event (TORE) Volumes for Event Cameras论文笔记