1.   Activemq整合spring

1.1. 使用方法

第一步:引用相关的jar包。

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

</dependency>

第二步:配置Activemq整合spring。配置ConnectionFactory

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->

<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">

<property name="brokerURL" value="tcp://192.168.25.168:61616" />

</bean>

<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->

<bean id="connectionFactory"

class="org.springframework.jms.connection.SingleConnectionFactory">

<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->

<property name="targetConnectionFactory" ref="targetConnectionFactory" />

</bean>

</beans>

第三步:配置生产者。

使用JMSTemplate对象。发送消息。

第四步:在spring容器中配置Destination。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->

<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">

<property name="brokerURL" value="tcp://192.168.25.168:61616" />

</bean>

<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->

<bean id="connectionFactory"

class="org.springframework.jms.connection.SingleConnectionFactory">

<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->

<property name="targetConnectionFactory" ref="targetConnectionFactory" />

</bean>

<!-- 配置生产者 -->

<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->

<property name="connectionFactory" ref="connectionFactory" />

</bean>

<!--这个是队列目的地,点对点的 -->

<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">

<constructor-arg>

<value>spring-queue</value>

</constructor-arg>

</bean>

<!--这个是主题目的地,一对多的 -->

<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">

<constructor-arg value="topic" />

</bean>

</beans>

第五步:代码测试

@Test

public void testSpringActiveMq() throws Exception {

//初始化spring容器

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");

//从spring容器中获得JmsTemplate对象

JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);

//从spring容器中取Destination对象

Destination destination = (Destination) applicationContext.getBean("queueDestination");

//使用JmsTemplate对象发送消息。

jmsTemplate.send(destination, new MessageCreator() {

@Override

public Message createMessage(Session session) throws JMSException {

//创建一个消息对象并返回

TextMessage textMessage = session.createTextMessage("spring activemq queue message");

return textMessage;

}

});

}

1.2. 代码测试

1.2.1.    发送消息

第一步:初始化一个spring容器

第二步:从容器中获得JMSTemplate对象。

第三步:从容器中获得一个Destination对象

第四步:使用JMSTemplate对象发送消息,需要知道Destination

@Test

public void testQueueProducer() throws Exception {

// 第一步:初始化一个spring容器

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");

// 第二步:从容器中获得JMSTemplate对象。

JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);

// 第三步:从容器中获得一个Destination对象

Queue queue = (Queue) applicationContext.getBean("queueDestination");

// 第四步:使用JMSTemplate对象发送消息,需要知道Destination

jmsTemplate.send(queue, new MessageCreator() {

@Override

public Message createMessage(Session session) throws JMSException {

TextMessage textMessage = session.createTextMessage("spring activemq test");

return textMessage;

}

});

}

1.2.2.    接收消息

e3-search-Service中接收消息。

第一步:把Activemq相关的jar包添加到工程中

第二步:创建一个MessageListener的实现类。

public class MyMessageListener implements MessageListener {

@Override

public void onMessage(Message message) {

try {

TextMessage textMessage = (TextMessage) message;

//取消息内容

String text = textMessage.getText();

System.out.println(text);

} catch (JMSException e) {

e.printStackTrace();

}

}

}

第三步:配置spring和Activemq整合。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->

<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">

<property name="brokerURL" value="tcp://192.168.25.168:61616" />

</bean>

<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->

<bean id="connectionFactory"

class="org.springframework.jms.connection.SingleConnectionFactory">

<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->

<property name="targetConnectionFactory" ref="targetConnectionFactory" />

</bean>

<!--这个是队列目的地,点对点的 -->

<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">

<constructor-arg>

<value>spring-queue</value>

</constructor-arg>

</bean>

<!--这个是主题目的地,一对多的 -->

<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">

<constructor-arg value="topic" />

</bean>

<!-- 接收消息 -->

<!-- 配置监听器 -->

<bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener" />

<!-- 消息监听容器 -->

<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">

<property name="connectionFactory" ref="connectionFactory" />

<property name="destination" ref="queueDestination" />

<property name="messageListener" ref="myMessageListener" />

</bean>

</beans>

第四步:测试代码。

@Test

public void testQueueConsumer() throws Exception {

//初始化spring容器

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");

//等待

System.in.read();

}

2.   添加商品同步索引库

2.1. Producer

e3-manager-server工程中发送消息。

当商品添加完成后发送一个TextMessage,包含一个商品id。

@Override

public e3Result addItem(TbItem item, String desc) {

// 1、生成商品id

final long itemId = IDUtils.genItemId();

// 2、补全TbItem对象的属性

item.setId(itemId);

//商品状态,1-正常,2-下架,3-删除

item.setStatus((byte) 1);

Date date = new Date();

item.setCreated(date);

item.setUpdated(date);

// 3、向商品表插入数据

itemMapper.insert(item);

// 4、创建一个TbItemDesc对象

TbItemDesc itemDesc = new TbItemDesc();

// 5、补全TbItemDesc的属性

itemDesc.setItemId(itemId);

itemDesc.setItemDesc(desc);

itemDesc.setCreated(date);

itemDesc.setUpdated(date);

// 6、向商品描述表插入数据

itemDescMapper.insert(itemDesc);

//发送一个商品添加消息

jmsTemplate.send(topicDestination, new MessageCreator() {

@Override

public Message createMessage(Session session) throws JMSException {

TextMessage textMessage = session.createTextMessage(itemId + "");

return textMessage;

}

});

// 7、e3Result.ok()

return e3Result.ok();

}

2.2. Consumer

2.2.1.    功能分析

1、接收消息。需要创建MessageListener接口的实现类。

2、取消息,取商品id。

3、根据商品id查询数据库。

4、创建一SolrInputDocument对象。

5、使用SolrServer对象写入索引库。

6、返回成功,返回e3Result。

2.2.2.    Dao层

根据商品id查询商品信息。

映射文件:

<select id="getItemById" parameterType="long" resultType="cn.e3mall.common.pojo.SearchItem">

SELECT

a.id,

a.title,

a.sell_point,

a.price,

a.image,

b. NAME category_name,

c.item_desc

FROM

tb_item a

JOIN tb_item_cat b ON a.cid = b.id

JOIN tb_item_desc c ON a.id = c.item_id

WHERE a.status = 1

AND a.id=#{itemId}

</select>

2.2.3.    Service层

参数:商品ID

业务逻辑:

1、根据商品id查询商品信息。

2、创建一SolrInputDocument对象。

3、使用SolrServer对象写入索引库。

4、返回成功,返回e3Result。

返回值:e3Result

public e3Result addDocument(long itemId) throws Exception {

// 1、根据商品id查询商品信息。

SearchItem searchItem = searchItemMapper.getItemById(itemId);

// 2、创建一SolrInputDocument对象。

SolrInputDocument document = new SolrInputDocument();

// 3、使用SolrServer对象写入索引库。

document.addField("id", searchItem.getId());

document.addField("item_title", searchItem.getTitle());

document.addField("item_sell_point", searchItem.getSell_point());

document.addField("item_price", searchItem.getPrice());

document.addField("item_image", searchItem.getImage());

document.addField("item_category_name", searchItem.getCategory_name());

document.addField("item_desc", searchItem.getItem_desc());

// 5、向索引库中添加文档。

solrServer.add(document);

solrServer.commit();

// 4、返回成功,返回e3Result。

return e3Result.ok();

}

2.2.4.    Listener

public class ItemChangeListener implements MessageListener {

@Autowired

private SearchItemServiceImpl searchItemServiceImpl;

@Override

public void onMessage(Message message) {

try {

TextMessage textMessage = null;

Long itemId = null;

//取商品id

if (message instanceof TextMessage) {

textMessage = (TextMessage) message;

itemId = Long.parseLong(textMessage.getText());

}

//向索引库添加文档

searchItemServiceImpl.addDocument(itemId);

} catch (Exception e) {

e.printStackTrace();

}

}

}

2.2.5.    Spring配置监听

2.2.6.    实现流程

ActiveMQ整合spring、同步索引库的更多相关文章

  1. JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存

    1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...

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

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

  3. 商城08——activeMQ 使用消息队列同步索引库

    1.  课程计划 1.什么是MQ 2.MQ的应用场景 3.ActiveMQ的使用方法. 4.使用消息队列实现商品同步. 2.  同步索引库分析 方案一:在taotao-manager中,添加商品的业务 ...

  4. e3mall商城的归纳总结9之activemq整合spring、redis的缓存

    敬给读者 本节主要给大家说一下activemq整合spring,该如何进行配置,上一节我们说了activemq的搭建和测试(单独测试),想看的可以点击时空隧道前去查看.讲完了之后我们还说一说在项目中使 ...

  5. 应用activeMQ消息中间件同步索引库

    mq是一个消息服务器: 安装包内置了tomcat,直接登录访问,登录:http://ip:8161/admin/    (相当于dubbo的moniter监控中心) admin admin传统串行化, ...

  6. ActiveMQ学习笔记(6)----ActiveMQ整合Spring开发

    1. 添加依赖 spring 提供了对JMS的支持,需要添加Spring支持jms的包和Spring的核心包,如下: <dependency> <groupId>org.apa ...

  7. ActiveMQ整合spring结合项目开发流程(生产者和消费者)总结

    一:生产者代码编写: 1.配置pom.xml引入相关坐标 <dependencies> <!-- spring开发测试 --> <dependency> <g ...

  8. ActiveMQ 整合 spring

    一.添加 jar 包 <dependency> <groupId>org.apache.activemq</groupId> <artifactId>a ...

  9. activemq整合spring

随机推荐

  1. 嵌入式开发之UDP 丢包--- UDP 丢包控制方法

    0. 发送端可以,发送五次左右,再Sleep 1.调用recv方法接收端收到数据后,处理数据花了一些时间,处理完后再次调用recv方法,在这二次调用间隔里,发过来的包可能丢失.对于这种情况可以修改接收 ...

  2. 将Java web应用部署到Tomcat 及部署到Tomcat根目录 的三种方式

    Tomcat作为Servlet/JSP容器(服务器)挺不错的,开源免费,需要知道的是Tomcat是一个Web服务器,其符合Servlet/JSP规范,但是却没有实现所有JavaEE规范,所以我们还是应 ...

  3. win7下安装Office2010老是出现提示安装MSXML6.10.1129.0,下载官方MSXML后提示安装成功却也安装不了

    在注册表中增加以下信息: [HKEY_CLASSES_ROOT\TypeLib\{F5078F18-C551-11D3-89B9-0000F81FE221}][HKEY_CLASSES_ROOT\Ty ...

  4. 仿迅雷播放器教程 -- C++界面制作方法的对比 (9)

        上一个教程对比的5个方向共7个界面框架,都是非常权威,应用很广泛的库,绝对是非常稳定,并且能够做出常见的界面出来,可以放心大胆的用在项目里.     但那7个界面框架再好,也总是没有绝对的优势 ...

  5. Git 学习笔记--删除错误提交的commit

    如果不小心把错误的commit给commit了,可以对其进行撤销 1.使用git log查看commit日志,找到错误提交前一版本commit的哈希值; 2.使用git reset --hard co ...

  6. windows cmd命令显示UTF8设置

    windows cmd命令显示UTF8设置   在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况 ...

  7. 四、K3 Cloud 开发插件《K3 Cloud事件、方法、函数》

    1.简单帐表/动态表单几个主要事件 //初始化 public override void Initialize() //构建动态列 public override BOS.Core.Report.Re ...

  8. Log4j2 中format增加自定义的参数

    首先收藏一下log4j2的官网:http://logging.apache.org/log4j/2.x/manual/plugins.html#TypeConverters 对于这种需要,可以使用lo ...

  9. 剖析RAC中的@weakify、@strongify

    0.很长的前言 1.问题 2.RAC是怎么解决的 2.weakify.strongify的定义 预备知识 一层层展开weakify 3.RAC装逼宏 metamacro_argcount 的定义 me ...

  10. 使用SpringContextHolder获取bean实例

    public static IConstantFactory me(){ return SpringContextHolder.getBean(beanName:"constantFacto ...