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搭配(其它版本 ...
随机推荐
- USI和USCI的区别
在 MSP430 系列中微控制器中有三种串行通讯模块.它们分别是 USART . USI 和 USCI . USART 支持同一硬件模块的两种串行模式,分别是 UART 和 SPI . USART 实 ...
- UIInterfaceOrientation over iOS6 (应用旋转屏幕)
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrien ...
- 自己开发chrome插件生成二维码
摘要: 最近在开发微信项目时,需要在微信调试,所以经常会在微信中输入本地服务地址,输入起来特别麻烦,所以自己就想了想微信中的扫一扫,然后开发了这款chrome插件,将当前url生成二维码,用微信扫一扫 ...
- 【hive】 hive 加载数据
1. insert 插入数据 要保证启动了jobhistory 否则会抛出异常 hdfs中查看内容 2. create table 表名字 select 字段... from 表名 hdfs查看数据 ...
- [Hinton] Neural Networks for Machine Learning - Basic
Link: Neural Networks for Machine Learning - 多伦多大学 Link: Hinton的CSC321课程笔记1 Link: Hinton的CSC321课程笔记2 ...
- Sysfs文件系统接口调试
首先需要初始化操作: s32 gtp_sysfs_init(void) { s32 ret ; debug_kobj = kobject_create_and_add("gtp", ...
- c++ union内存
看一个例子: #include <iostream> #include <stdio.h> using namespace std; union A { int a; stru ...
- Unity3D 批处理场景的工具
//场景的批量处理器 public static class OperateScene { public const string SceneDir = "Assets/Scene/&quo ...
- Win10配置分屏显示
新买的电脑是17.3的,单独打开一个界面总是感觉地方有点浪费,研究了下分屏使用. 以下是现在分屏后的电脑界面. 设置说明 目的:将三个窗口分屏布满屏幕,便于多任务操作. 步骤: 1.按住鼠标左键,将w ...
- 如何用JQuery实现单元格 循环变背景色
要不断循环最好用 setInterval 吧var i = 0;var obj = $("tr td");setInterval(function(){ obj.css(" ...