项目中使用的介绍

一、运行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. php object

    一.访问控制 <?php class Computer{ public $cpu = 880; private $name = 'xiaomi'; public function getname ...

  2. k8s+jenkins

    1 server 的port , targetport, nodeport的区别 targetport为容器的暴露端口,为最后端的端口 port可以理解为pod的端口,pod是容器的外层,该端口可以在 ...

  3. Python PEP8代码书写规范

    摘自: 规范 https://blog.csdn.net/ratsniper/article/details/78954852

  4. python 日期生成和时间格式化

    记录下日期时间的转换和生成:1.这个是使用datetime 函数生成往后几天的时间,比如当前日期是2019-07-01 那么控制days=1然后再和当前的时间相加,就能得到明天的日期def time_ ...

  5. UITextField 长按文本框指定删除某个位置内容

    普通的光标移动,点键盘的删除键,会从最后一位删除,加一UITextField的分类即可 #import <UIKit/UIKit.h> @interface UITextField (Ex ...

  6. Linux内核设计与实现 总结笔记(第六章)内核数据结构

    内核数据结构 Linux内核实现了这些通用数据结构,而且提倡大家在开发时重用. 内核开发者应该尽可能地使用这些数据结构,而不要自作主张的山寨方法. 通用的数据结构有以下几种:链表.队列.映射和二叉树 ...

  7. SSD_mobilenet

    mobilenet_ssd caffe模型可视化地址:MobileNet_ssd conv13是mobilenet的最后一个卷积层,作者仿照VGG-SSD的结构,在MobileNet的conv13后面 ...

  8. 【HDOJ6630】permutation 2(递推)

    题意:给定x,y,n,有标号从1到n的n个数组,求合法的排列个数模998244353使得 1:p[1]=x 2:p[n]=y 3:相邻两项的差的绝对值<=2 n<=1e5 思路: #inc ...

  9. C++ Map相同key是否覆盖问题分析

    C++的标准库关联容器map是不允许有key相同的键值对存在的.那么当key已经存在的情况下,我们再次插入相同的key,那么key的value会被覆盖吗? 测试代码: 测试结果: 从测试结果我们可以得 ...

  10. 心形陀螺案例css3

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="UTF-8&qu ...