项目中使用的介绍

一、运行ActiveMQ

在文件路径下...\apache-activemq-5.13.3\bin\win64

运行activemq.bat

这是系统中的使用,运行后还可以访问相应的页面。

二、项目中的实践

1.依赖的jar包

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-server</artifactId>
</dependency>

2.配置ActiveMQ的配置,要和运行的保持一致,在commom.properties文件中。

activemq.broker.url=tcp://127.0.0.1:61616
activemq.user=admin
activemq.password=admin
activemq.name=datacenter.test
activemq.name.access=datacenter.access

3.applicationContext-mq.xml 中配置

<!-- 1.配置ActiveMQ 连接工厂 -->
<amq:connectionFactory id="creditMQConnectionFactory"
brokerURL="${activemq.broker.url}" userName="${activemq.user}" password="${activemq.password}" /> <!-- 2.配置缓存 ConnectionFactory -->
<bean id="creditMQconnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="creditMQConnectionFactory"/>
<property name="sessionCacheSize" value="100" />
</bean> <!-- 定义JmsTemplate的Queue类型 -->
<bean id="creditJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="creditMQConnectionFactory" />
<property name="pubSubDomain" value="false" />
</bean> <!-- 定义授权队列消费者
<bean id="creditConsumer" class="com.itom.mq.consumer.CreditConsumer"/>--> <!-- 定义Queue监听器 -->
<jms:listener-container destination-type="queue" container-type="default" connection-factory="creditMQconnectionFactory" acknowledge="auto">
</jms:listener-container>
<!--运营相关配置结束-->

4.编写生产者和消费者

1) 生产者

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component; import javax.annotation.Resource;
import javax.jms.Queue;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; @Component
public class CreditProducer { private Map<String ,Queue> cacheQueue = new ConcurrentHashMap<String ,Queue>(); @Resource
private JmsTemplate creditJmsTemplate; public void pushCreditData(String queueName ,Object message){ Queue queue = cacheQueue.get(queueName);
if(queue == null){
queue = new ActiveMQQueue(queueName);
cacheQueue.put(queueName,queue);
}
this.creditJmsTemplate.convertAndSend(queue, message);
}
}

2) 消费者:

public class MessageReceiver implements MessageListener {

    //自己定义的类,使用线程池来处理消息
@Resource(name = "messageComputePool")
MessageComputePool pool; public void onMessage(Message message) {
if(message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(String.format("Received: %s",text));
JSONObject jsonObj = (JSONObject)JSONObject.parse(text);
...
//消费者接收到消息就可以进行处理了
} catch (JMSException e) {
e.printStackTrace();
}
} }
}

3)使用生产者推消息

@Service("collectService ")
public class CollectServiceImpl implements CollectService {
public static final Logger LOG = LoggerFactory.getLogger(CollectServiceImpl.class); @Resource
private CreditProducer creditProducer; @Override
public void collect(CollectParameter parameter) {
Map dataMap = new HashMap();
dataMap.put("sourceCode", parameter.getValue());
dataMap.put("userId",parameter.getUserid());
creditProducer.pushCreditData("itom.activemq.access", JSON.toJSONString(dataMap));
}
}
}

推荐个ActiveMQ入门教程:

http://blog.csdn.net/jolingogo/article/category/1658165

ActiveMQ的简单使用的更多相关文章

  1. dubbo 图片服务器(FastDFS) redis solr ActiveMQ等简单配置使用

    一.dubbo 项目基于soa的架构,表现层和服务层是不同的工程.所以要实现商品列表查询需要两个系统之间进行通信. 1.1如何实现远程通信? 1.Webservice:效率不高基于soap协议.项目中 ...

  2. ActiveMQ此例简单介绍基于docker的activemq安装与集群搭建

    ActiveMQ拓展连接 此例简单介绍基于Docker的activemq安装与集群搭建 一 :安装 1.获取activemq镜像 docker pull webcenter/activemq 2.启动 ...

  3. Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)

    首先声明:以下内容均是在网上找别人的博客综合学习而成的,可能会发现某些代码与其他博主的相同,由于参考的文章比较多,这里对你们表示感谢,就不一一列举,如果有侵权的地方,请通知我,我可以把该文章删除. 1 ...

  4. ActiveMQ的简单例子应用

    ActiveMQ是一种消息中间件,它实现了JMS规范,提供点对点和订阅-发布两种模式.下面介绍下ActiveMQ的使用: 一.环境的搭建 首先我们需要下载ActiveMQ的安装包,下载地址http:/ ...

  5. activeMQ的简单介绍

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

  6. 遇见JMS[1] —— activeMQ的简单使用

    1.JMS Java Message Service,提供API,供两个应用程序或者分布式应用之间异步通信,以传送消息. 2.相关概念 提供者:实现JMS规范的消息中间件服务器客户端:发送或接收消息的 ...

  7. 消息中间件 ActiveMQ的简单使用

    一.AactiveMQ的下载和安装 1. 下载ActiveMQ 地址:http://activemq.apache.org/activemq-5152-release.html 我这里下载的是wind ...

  8. 消息中间件系列之ActiveMQ的简单安装

    本次测试使用一台ip为192.168.2.12的虚拟机 一.解压压缩包 tar -zxvf apache-activemq-5.14.4-bin.tar.gz 二.启动activemq 进入到bin目 ...

  9. JMS - ActiveMQ的简单使用

    首先需要下载ActiveMQ,下面的链接给我们列出了所有版本:http://activemq.apache.org/download-archives.html每个版本为不同的OS提供了链接: 公司电 ...

随机推荐

  1. VUE 生成二维码插件

    原文:https://www.jianshu.com/p/496fd1cbee8d npm install qrcodejs2 --save 页面中引入 dom 结构 JS 方法编写 export d ...

  2. 迁移数据时 timestamp类型字段报错: 1067 - Invalid default value for 'login_time'

    MySQL数据库升级 8.0.13,原版本5.5:执行导出来的SQL文件时报错 1067 - Invalid default value for 'login_time' 原因:MySQL 5.6以后 ...

  3. 在使用mybatis中指定字段查询

    1:需求:查询学过“叶平”老师所教的所有课的同学的学号.姓名: List<Map<String,Object>> selectYepingAllCourse(@Param(&q ...

  4. web应用,http协议简介,web框架

    一.web应用 web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C/S是客户端 ...

  5. 关于富文本复制word,里面掺杂图片上传的问题

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM ...

  6. 【Java】JSONObject学习

    介绍 JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素.JSONObject可以很方便的转换成字符串,也可以 ...

  7. 【HDOJ6621】K-th Closest Distance(主席树,二分)

    题意:给定一个长为n的序列,有m次强制在线的询问,每次询问位置[L,R]中abs(a[i]-p)第k小的值 n,m<=1e5,a[i]<=1e6,p<=1e6,k<=169 思 ...

  8. ReactNative的学习笔记

    一.安装nodejs 查看是否安装:npm -v 二.安装react-native命令工具 npm install -g react-native-cli 三.查看 react-native --he ...

  9. 组件Component详解

    [转]https://www.cnblogs.com/moqiutao/p/8328931.html

  10. 432D Prefixes and Suffixes

    题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 ...