项目中使用的介绍

一、运行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. git的HEAD指针操作

    学习操作HEAD指针,具体如下: - 查看Git版本信息 - 移动指针 - 通过移动HEAD指针恢复数据 - 合并版本 拓扑图:

  2. 关于discuz论坛邮箱配置

    Discuz后台可以进行邮件设置,实现网站自动发送邮件给用户的邮箱. 在Discuz邮件设置,经常使用25端口普通发送邮件.为了数据安全,我们也可以使用SSL加密发送,设置方法很简单,只需按照下图进行 ...

  3. php注解

    官方全部注解:https://docs.phpdoc.org/references/phpdoc/tags/index.html 1.@var 您可以使用@var标记来记录属性的“类型”,有时也称为类 ...

  4. windows命令整理

    本文只是作为知识整理,尽可能的收集一些常用的内网指令.本人原伸手党一枚,希望这些内容对新人有用,大牛可自行忽略. 0x00 内网信息收集 一.单机基础信息收集 如果是获得第一台初始主机的权限的话,我们 ...

  5. Test 3.27 T2 旅行

    Description FGD 想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说 FGD 不希望在刚吃过一顿 ...

  6. hdu 5212 : Code【莫比乌斯】

    题目链接 题给代码可以转化为下面的公式 然后用F[n]记录公约数为n的(a[i],a[j])对数,用f[n]记录最大公约数为n的(a[i],a[j])对数 之后枚举最大公约数d 至于求F[n],可以先 ...

  7. 【leetcode】1054. Distant Barcodes

    题目如下: In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i]. Rearrange t ...

  8. [luogu] P3809 【模板】后缀排序 (SA)

    板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...

  9. BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常

    Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...

  10. brew安装指定版本的软件

    原文:https://www.jianshu.com/p/aadb54eac0a8 在mac中使用 brew install 安装的软件默认都是最新版本的.有时候我们需要旧版本(指定版本)的时候,应该 ...