activeMQ的安装和使用
什么是ActiveMQ?
一款开源的JMS具体实现,是一个易于使用的消息中间件,一个消息容器
安装
下载
官方网站:http://activemq.apache.org/
解压
linux下的安装,解压命令:tar zxvf activemq-x.x.x-bin.tar.gz
启动
- 前端进程的方式启动(控制台关闭则服务关闭)
cd [activemq_install_dir]/bin./activemq console
- 后台进程的方式启动
d [activemq_install_dir]/bin./activemq start
测试是否启动成功
浏览器中输入 http://127.0.0.1:8161/admin/登录名/密码: admin/admin
Linux下ActiveMQ默认监听的端口号:61616,可以通过netstat -nl|grep 61616 查看
关闭
如果启动的是前端进程,那么可以直接在控制台 ctrl + C 关闭
如果启动的是后端进程 cd [activemq_install_dir]/bin./activemq stop
目录结构

bin存放的是脚本文件
conf存放的是基本配置文件
data存放的是日志文件
docs存放的是说明文档
examples存放的是简单的实例
lib存放的是activemq所需jar包
webapps用于存放项目的目录
与spring的整合
直接上代码
所需jar包
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0</version>
</dependency>
<!-- spring-jms API -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- active-mq核心包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- 配置连接ActiveMQ的ConnectionFactory -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean> <!--为了提高效率,配置一个spring提供的缓存连接池-->
<bean id="cachedConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory"
p:targetConnectionFactory-ref="amqConnectionFactory"
p:sessionCacheSize="10"/> <!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="cachedConnectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
<!-- 指定默认的destination -->
<property name="defaultDestination" ref="topicDestination"/>
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置explicitQosEnabled为true,默认false-->
<property name="explicitQosEnabled" value="true" />
<!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久-->
<property name="deliveryMode" value="2" />
</bean>
<!--Spring JmsTemplate 的消息生产者 end--> <!-- 配置queue的destination目的地-->
<!-- 接收者 -->
<bean id="activeMqReceiverDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 指定队列的名称 -->
<constructor-arg value="activeMqReceiver"/>
</bean> <!-- 评论消息 -->
<!-- <bean id="commentMessageDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="commentMessage"/>
</bean> --> <!-- 发布任务消息 -->
<!-- <bean id="releaseMessageDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="releaseMessage"/>
</bean> -->
<!-- 发布任务批量保存 -->
<!-- <bean id="batchSaveTaskDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="batchSaveTask"/>
</bean> -->
<!-- 更新评论数量 -->
<!-- <bean id="updateCommentNumberDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="updateCommentNumber"/>
</bean> -->
<!-- 回帖相关 -->
<!-- <bean id="repliesDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="repliesDestination"/>
</bean> --> <!-- 配置topic的Destination地址 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="myTopic"/>
</bean> <!-- Spring JmsTemplate 的消息生产者 start-->
<!-- 定义JmsTemplate的Queue类型 -->
<bean id="queueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="cachedConnectionFactory" />
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false" />
<!-- 指定默认的destination
<property name="defaultDestination" ref="queueDestination"/>-->
</bean> <!-- 消息消费者相关配置 start-->
<!-- 鲜花消息监听类 -->
<!-- <bean id="flowerMessageConsumerService" class="com.tfedu.discuss.service.mq.FlowerMessageConsumerService"/>
评论消息监听类
<bean id="commentMessageConsumerService" class="com.tfedu.discuss.service.mq.CommentMessageConsumerService"/>
发布消息监听类
<bean id="releaseMessageConsumerService" class="com.tfedu.discuss.service.mq.ReleaseMessageConsumerService"/>
批量保存发布任务
<bean id="batchSaveTaskConsumerService" class="com.tfedu.discuss.service.mq.BatchSaveTaskConsumerService"/>
评论数维护监听类
<bean id="commentNumberMessageConsumerService" class="com.tfedu.discuss.service.mq.CommentNumberMessageConsumerService"/> -->
<bean id="activeMqReceiverService" class="com.activemq.ActiveMqReceiverService"></bean>
<!-- 定义Queue监听器 -->
<jms:listener-container destination-type="queue" container-type="default" connection-factory="cachedConnectionFactory" acknowledge="transacted">
<!-- <jms:listener destination="flowerMessageDestination" ref="flowerMessageConsumerService"/>
<jms:listener destination="commentMessageDestination" ref="commentMessageConsumerService"/>
<jms:listener destination="releaseMessageDestination" ref="releaseMessageConsumerService"/>
<jms:listener destination="batchSaveTaskDestination" ref="batchSaveTaskConsumerService"/>
<jms:listener destination="updateCommentNumber" ref="commentNumberMessageConsumerService"/>
<jms:listener destination="repliesDestination" ref="repliesMessageConsumerService"/> -->
<jms:listener destination="activeMqReceiverDestination" ref="activeMqReceiverService"/>
</jms:listener-container>
<!-- 消息消费者相关配置 end-->
</beans>
消息生产者代码
package com.activemq; import javax.annotation.Resource; import org.springframework.jms.core.JmsOperations;
import org.springframework.stereotype.Service; @Service
public class ActiveMqSenderService {
//JmsTemplate为JmsOperations的具体实现,一般注入接口解耦
@Resource(name = "queueTemplate")
private JmsOperations queueTemplate; /**
* 发送鲜花消息
* <p>
* 赠送鲜花时触发
*
* @param messageEntity 消息实体
*/
public void sendFlowerMessage(MQMessageEntity messageEntity) {
System.out.println("准备发送消息");
queueTemplate.convertAndSend("activeMqReceiverDestination", messageEntity);
}
}
消息接收者
package com.activemq; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage; import org.springframework.beans.factory.annotation.Autowired; public class ActiveMqReceiverService implements MessageListener{ @Autowired
private MessageService messageService;
@Override
public void onMessage(Message message) {
ObjectMessage ObjectMessage = (ObjectMessage) message;
MQMessageEntity messageEntity;
try {
messageEntity = (MQMessageEntity) ObjectMessage.getObject();
messageService.messageFlower(messageEntity.getSourceId(), messageEntity.getSourceType(),
messageEntity.getSendId());
} catch (JMSException e) {
e.printStackTrace();
} } }
activeMQ的安装和使用的更多相关文章
- activemq的安装与使用
一.activemq的安装 环境:CentOS 6.JDK8 1. 确保系统已安装了可用的jdk版本2. 从网上下载 Linux 版的 ActiveMQ( apache-activemq-5.11.1 ...
- ActiveMQ的安装与配置
ActiveMQ的安装与配置详情 (1)ActiveMQ的简介 MQ: (message queue) ,消息队列,也就是用来处理消息的,(处理JMS的).主要用于大型企业内部或与企业之间的传递数据信 ...
- 170516、ActiveMQ 的安装与使用(单节点)
ActiveMQ 的安装与使用(单节点)IP: 192.168.4.101环 境: CentOS 6.6 . JDK71. 安装 JDK 并配置环境变量(略)JAVA_HOME=/usr/local/ ...
- activemq的安装使用
近期有项目中用到消息队列,JMS规范中实现最好的开源框架就是activemq.所以选择它(当然这是我老大决定的,像我这样的刚入职场的小菜鸟考虑问题还不太全面)作为消息队列数据传输.公司有有成型的消息队 ...
- 淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记
文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...
- ActiveMQ的安装与使用。
1.什么是ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE .4规范的 JMS Provider实现,尽 ...
- Dubbo入门到精通学习笔记(八):ActiveMQ的安装与使用(单节点)、Redis的安装与使用(单节点)、FastDFS分布式文件系统的安装与使用(单节点)
文章目录 ActiveMQ的安装与使用(单节点) 安装(单节点) 使用 目录结构 edu-common-parent edu-demo-mqproducer edu-demo-mqconsumer 测 ...
- Active-MQ的安装
(1)首先就是下载软件 wget http://archive.apache.org/dist/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0 ...
- 分布式架构实战--ActiveMQ的安装与使用(单节点)
具体内容请参考样例代码和视频教程: http://www.roncoo.com/course/view/85d6008fe77c4199b0cdd2885eaeee53 IP:192.168.4.10 ...
- ActiveMQ——activemq的安装详情,修改密码
1.安装 下载 http://activemq.apache.org/download-archives.html, [推荐]ActiveMQ 5.13.4 Release与jdk1.7搭配(其它版本 ...
随机推荐
- zookeeper 入门(一)
1 下载安装 wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gzcp zookeeper-3. ...
- 【HTTPS】自签CA证书 && nginx配置https服务
首先,搭建https服务肯定需要一个https证书.这个证书可以看做是一个应用层面的证书.之所以这么说是因为https证书是基于CA证书生成的.对于正式的网站,CA证书需要到有资质的第三方证书颁发机构 ...
- 3D点云的深度学习
使用卷积神经网络(CNN)架构的深度学习(DL)现在是解决图像分类任务的标准解决方法.但是将此用于处理3D数据时,问题变得更加复杂.首先,可以使用各种结构来表示3D数据,所述结构包括: 1 体素网格 ...
- 小程序返回顶部top滚动
wxjs const app = getApp(); Page({ data:{ // top标签显示(默认不显示) backTopValue:false }, // 监听滚动条坐标 onPageSc ...
- CALayer 知识:创建带阴影效果的圆角图片图层和创建自定义绘画内容图层
效果如下: KMLayerDelegate.h #import <UIKit/UIKit.h> @interface KMLayerDelegate : NSObject @end KML ...
- PHP利用ImageMagick把PDF转成PNG
http://blog.csdn.net/longaction2012/article/details/12257867
- Android使用Fragment实现TabHost效果
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- [转]linux 调用动态库so文件
记录一个面试被问到的问题. extern 有什么用途? 除了多文件共享全局变量外还有呢? extern "C" 的功能? 我想看完这篇文章就可以知道第三个问题了. 关于动态调用动态 ...
- Maven传递依赖的坑:父pom中dependencyManagement版本优先级高于传递依赖版本
一.由来 之前同事问了个问题,就是当前工程为spring boot项目,假设版本号为2.0.3 这个项目中依赖了一个spring boot项目依赖(先别管为啥有这么奇葩的依赖,这个版本是1.5.9). ...
- include_once与require_once的区别
①作用及用法 可以减少代码的重复 include(_once)("文件的路径")与require(_once)("文件的路径") ②理解 说白了,就是用包含进 ...