美文网首页
第三课 ActiveMQ在项目中的使用

第三课 ActiveMQ在项目中的使用

作者: Arroganter | 来源:发表于2018-08-27 08:40 被阅读46次

分析:
在后台每次添加或修改文章的时候,发送一个消息,内容为新添加或修改的文章id,solr服务层接收到消息后取出文章id,根据id去数据库查询文章信息,然后更新索引库,从而达到同步效果。修改的话同时还要删除redis缓存,如果不使用ActiveMQ,同时做这许多事会导致客户端响应慢。

配置文件中:配置连接工厂生产者,目的地。

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.128: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="itemAddTopic" />
    </bean>
Controller:

  @Controller
public class HomeController {
    @Autowired
    private JmsTemplate jmsTemplate;
    //注入主题目的地(发布/订阅模式)
    @Autowired
    private Destination topicDestination;
    @RequestMapping("/")
    public ModelAndView home() throws IOException, SolrServerException {


        jmsTemplate.send(topicDestination, new MessageCreator() {

            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage(99+"");
                return message;
            }
        });

     
        return null;
    }
}

接收消息,实现solr索引库文章信息与数据库文章信息同步

配置文件中:配置连接工厂,目的地,自定义消息监听器和消息监听容器

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.128: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="itemAddTopic" />
    </bean>

    <bean id="itemAddMessageListener" class="cn.e3mall.search.message.ItemAddMessageListener"></bean>
    <!-- 监听商品添加消息,同步索引库 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicDestination" />
        <property name="messageListener" ref="itemAddMessageListener" />
    </bean>

自定义文章修改消息监听器

package com.neusoft.util;

import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * Created by Administrator on 2018/8/15.
 */

public class ItemAddMessageListener implements MessageListener {

    public void onMessage(Message message) {
        //从消息中取商品id
        TextMessage textMessage = (TextMessage) message;
        try {
            String text = textMessage.getText();
            Long itemId = Long.parseLong(text);
            System.out.println("onMessageonMessageonMessageonMessageonMessage"+itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
```

相关文章

  • ActiveMQ在项目中的使用

    分析:在后台每次添加或修改文章的时候,发送一个消息,内容为新添加或修改的文章id,solr服务层接收到消息后取出文...

  • 第三课 ActiveMQ在项目中的使用

    分析:在后台每次添加或修改文章的时候,发送一个消息,内容为新添加或修改的文章id,solr服务层接收到消息后取出文...

  • activeMQ实例在项目中的运用

    activeMQ实例在项目中的运用【项目实战系列】

  • ActiveMQ RabbitMQ RocketMQ KafKa

    ActiveMQ和 RabbitMq 以及Kafka在之前的项目中都有陆续使用过,当然对于三者没有进行过具体的对比...

  • 【JAVA】ActiveMQ

    消息队列ActiveMQ的使用详解 ActiveMQ安装 下载地址:http://activemq.apache....

  • ActiveMQ嵌入Tomcat

    在一些项目中,单独开启一个ActiveMQ,对于项目实施来说有时略显繁琐。所以我们将ActiveMQ内嵌到Tomc...

  • kafka的安装与使用

    目前项目中使用的是activemq和rabbitmq,现在简单学习一下kafka.搭了个zokeeper集群,ka...

  • ActiveMq伪集群设置

    自己学习ActiveMq集群化的时候可以使用多台虚拟机模拟真实环境。也可以在单机上使用多个ActiveMq节点,也...

  • SpringBoot集成ActiveMQ

    Spring Boot 集成ActiveMQ 使用ActiveMQ版本5.14.0,spring Boot版本1....

  • 第二课 ActiveMQ与Spring的整合使用

    在介绍ActiveMQ特性的时候有一条就是,对spring的支持,ActiveMQ可以很容易内嵌到使用Spring...

网友评论

      本文标题:第三课 ActiveMQ在项目中的使用

      本文链接:https://www.haomeiwen.com/subject/yphiiftx.html