什么是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的安装和使用的更多相关文章

  1. activemq的安装与使用

    一.activemq的安装 环境:CentOS 6.JDK8 1. 确保系统已安装了可用的jdk版本2. 从网上下载 Linux 版的 ActiveMQ( apache-activemq-5.11.1 ...

  2. ActiveMQ的安装与配置

    ActiveMQ的安装与配置详情 (1)ActiveMQ的简介 MQ: (message queue) ,消息队列,也就是用来处理消息的,(处理JMS的).主要用于大型企业内部或与企业之间的传递数据信 ...

  3. 170516、ActiveMQ 的安装与使用(单节点)

    ActiveMQ 的安装与使用(单节点)IP: 192.168.4.101环 境: CentOS 6.6 . JDK71. 安装 JDK 并配置环境变量(略)JAVA_HOME=/usr/local/ ...

  4. activemq的安装使用

    近期有项目中用到消息队列,JMS规范中实现最好的开源框架就是activemq.所以选择它(当然这是我老大决定的,像我这样的刚入职场的小菜鸟考虑问题还不太全面)作为消息队列数据传输.公司有有成型的消息队 ...

  5. 淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记

    文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...

  6. ActiveMQ的安装与使用。

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

  7. Dubbo入门到精通学习笔记(八):ActiveMQ的安装与使用(单节点)、Redis的安装与使用(单节点)、FastDFS分布式文件系统的安装与使用(单节点)

    文章目录 ActiveMQ的安装与使用(单节点) 安装(单节点) 使用 目录结构 edu-common-parent edu-demo-mqproducer edu-demo-mqconsumer 测 ...

  8. Active-MQ的安装

    (1)首先就是下载软件 wget http://archive.apache.org/dist/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0 ...

  9. 分布式架构实战--ActiveMQ的安装与使用(单节点)

    具体内容请参考样例代码和视频教程: http://www.roncoo.com/course/view/85d6008fe77c4199b0cdd2885eaeee53 IP:192.168.4.10 ...

  10. ActiveMQ——activemq的安装详情,修改密码

    1.安装 下载 http://activemq.apache.org/download-archives.html, [推荐]ActiveMQ 5.13.4 Release与jdk1.7搭配(其它版本 ...

随机推荐

  1. Java知多少(89)列表和组合框

    有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...

  2. Zookeeper系列四:Zookeeper实现分布式锁、Zookeeper实现配置中心

    一.Zookeeper实现分布式锁 分布式锁主要用于在分布式环境中保证数据的一致性. 包括跨进程.跨机器.跨网络导致共享资源不一致的问题. 1. 分布式锁的实现思路 说明: 这种实现会有一个缺点,即当 ...

  3. Hbase学习笔记——基本CRUD操作

    进入Hbase的安装目录,启动Hbase bin/start-hbase.sh 打开shell命令行模式 bin/hbase shell 关闭Hbase bin/stop-hbase.sh 一个cel ...

  4. @ResponseBody的作用

    由于之前一直用struts2,对springMvc的注解并不太了解.新公司的项目用的是springMvc+hibernate,看到了@ResponseBody注解 @ResponseBody作用类似于 ...

  5. [Node.js] 09 - Connect with Database

    简介两个数据库: Node.js 连接 MySQL Node.js 连接 MongoDB Node.js 连接 MySql 导入已有数据库: unsw@unsw-UX303UB$ mysql -u r ...

  6. JavaScript隐藏的坑一,隐式调用toString

    最近在重新学习JavaScript,看动态原型对象的时候,打印了两个用同一个构造函数生成的对象,但是打印结果却不一样,请看代码: var box1=new Box(); console.log(box ...

  7. Win10系统安装过程小记

    1.网上下载ghost系统http://win10.jysmac.cn/win1064.html 2.使用系统自带的激活工具激活 3.到windows官网下载更新工具更新系统,重新安装https:// ...

  8. JS =>处理单击事件与拖动事件并存

    使用click事件,很难确定在拖动开始和结束的时候触发,所以使用了onmouseup(释放鼠标的时候),释放鼠标的时候,即在拖动还未结束,但是准备处理的阶段,此时mouseButtonFlag按照逻辑 ...

  9. RabbitMQ随笔

    不管是官方还是能搜到的文章,使用MQ的基本思路都是这样: static void Main(string[] args) { //通过工厂建立连接 using (IConnection connect ...

  10. 如何用JQuery实现单元格 循环变背景色

    要不断循环最好用 setInterval 吧var i = 0;var obj = $("tr td");setInterval(function(){ obj.css(" ...