什么是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. Mysql字符串字段中是否包含某个字符串,用 find_in_set

    有这样一个需求,在Mysql数据库字符串字段(权限)中,有范围在 1 到 N 之间代表不同权限的值,分别被‘,’分开,现在要取出具有某权限的所有成员列表. 创建表:   1 CREATE TABLE ...

  2. ( 转 )超级惊艳 10款HTML5动画特效推荐

    今天我们要来推荐10款超级惊艳的HTML5动画特效,有一些是基于CSS3和jQuery的,比较实用,特别是前几个HTML5动画,简直酷毙了,现在将它们分享给大家,也许你能用到这些HTML5动画和jQu ...

  3. APACHE多个服务器的配置

    APACHE 多个服务器的配置? 网站目录:d:www 下设两个站点:1.D:wwwszbw  2.D:wwwweb 注意前面,要开启 Vhost 及 vhos 相关 so <VirtualHo ...

  4. 字节顺序:高位优先(big-endian)和低位优先(little-endian)

    网络字节序: MSB 高字节前存法 Most Significant Bit   (Big Edian) 主机字节序: LSB 低字节前存法 Lest Significant Bit  (Little ...

  5. 在Android Studio中查看Sqlite的方法

    只说最好的方法,使用工具stetho:http://facebook.github.io/stetho/ 1.在Gragle中加上如下语句: dependencies { // Stetho core ...

  6. mysql性能调整三板斧

    大意是,用2/8原则,多快好省的解决大部分事情.所以三板斧,仅限整体调整,不牵扯具体细节. 1.innodb 使用innodb引擎 2.innodb_buffer_pool 调整和innodb有关的参 ...

  7. DownloadProvider源码解析——与Volley对比

    1.AndroidHttpClient的创建 DownloadManager: 在DownloadThread的run方法里 public void run() { Process.setThread ...

  8. 【SpringCloud错误】错误记录

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates ...

  9. 11:HTML5 发展史

    11:HTML5 发展史 HTML5草案的前身名为 Web Applications 1.0,于2004年被WHATWG提出,于2007年被W3C接纳,并成立了新的 HTML 工作团队. HTML 5 ...

  10. 标准库 os、sys、logging、configparser、time、requests

    os : 与操作系统交互的模块 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于 ...