ActiveMQ入门

来源:互联网 发布:淘宝女装宣活动传语 编辑:程序博客网 时间:2024/06/05 09:32

word版本下载

ActiveMQ入门

  1. ActiveMQ简介

定义:消息中间件是在分布式系统中完成消息的发送和接收的基础软件

消息队列:是在消息的传输过程中保存消息的容器。

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

JMSJMSJava消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。

常见的消息中间件:

  1. ActiveMQ作用

个人认为最大的作用就是解耦,分压和事物分发

 

传统的流程中:加入消费者买东西,接收到消费者的请求,所有的操作都在一个事物中进行,假如在中途计算成本出错,事物便要回滚,那我们要退钱给消费者么?在假如,如果遇到双十一这种大量订单聚集的时候,每个消费者发送一次消费请求,我们就要把整个流程走完,那在那么高的并发下我们需要多久的时间,需要多大的性能,最大的可能是支持不住而宕机。

有了消息队列之后:我们可以在客户发送消费请求后记录下来相应的信息,发送消息给MQ服务器,让其在后台处理后边的事宜,并给客户返回消费成功的提醒。就可以避免上边的一些列问题。

  1. ActiveMQ工作场景

 

  1. 消息列表发送消息的方式或模式?

二种方式

 

第一种:点对点: 一对一 相当于QQ 私聊

 

第二种: 发布、订阅模式 一对多 相当于QQ的群聊

        

 

  1. ActiveMQ 的应用时名词解释

 

  1. ActiveMQ应用小案例(基于spring)

业务简介:

1:上架 service-product 项目中

    1)更改商品状态

2)发送消息 到ActiveMQ (商品ID)

ActiveMQ有消息

2、service-solr 服务

1)获取MQ中的消息 (商品ID)

2)保存商品信息到Solr服务器

 

MQ采用点对点方式,

ActiveMQ的搭建请自行找教程,ActiveMQ是Java语言写的 Liunx必须安装JDK并配置环境变量

  1. 发送消息到ActiveMQ服务器中

ActiveMQ的Maven坐标

 

 

配置连接工厂(由Apache原厂商提供)

Product项目配置Mq.xml

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

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

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

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

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

    xmlns:task="http://www.springframework.org/schema/task"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://www.springframework.org/schema/context

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

        http://www.springframework.org/schema/aop

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

        http://www.springframework.org/schema/tx

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

        http://www.springframework.org/schema/task

        http://www.springframework.org/schema/task/spring-task-4.0.xsd

        http://code.alibabatech.com/schema/dubbo

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

        

        <!--配置连接MQ工厂,apache提供 -->

        <beanid="activeMQConnectionFactory"class="org.apache.activemq.ActiveMQConnectionFactory">

            <propertyname="brokerURL"value="tcp://192.168.200.128:61616"/>

            <propertyname="userName"value="admin"/>

            <propertyname="password"value="admin"/>

</bean>

 

连接池(由Apache原厂商提供)

<!--配置工厂的连接池 -->

<beanid="pooledConnectionFactoryBean"class="org.apache.activemq.pool.PooledConnectionFactoryBean">

    <propertyname="connectionFactory"ref="activeMQConnectionFactory"/>

    <propertyname="maxConnections"value="20"/>

</bean>

 

配置上面工厂交由Spring管理(工厂)单例

<!--将上面的工厂交由spring管理 -->

<beanid="singleConnectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory">

    <propertyname="targetConnectionFactory"ref="pooledConnectionFactoryBean"></property>

</bean>

 

配置Spring的JmsTemplate

<!--配置Spring jmsTemplete -->

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

    <propertyname="connectionFactory"ref="singleConnectionFactory"></property>

    <!--指定默认的目标地点 -->

    <propertyname="defaultDestinationName"value="productId"></property>

</bean>

 

上架方法发送消息

            Product项目中ProductServiceImpl.java

            @Override

publicvoid isShow(Long[]ids) throws Exception {

    Productproduct = new Product();

    product.setIsShow(true);

    if(ids !=null){

        for(final Long id : ids){

            product.setId(id);

            productDao.updateByPrimaryKeySelective(product);

            

            //发送消息

            jmsTemplate.send(new MessageCreator() {

                    

                @Override

                public Message createMessage(Sessionsession) throws JMSException {

                    //TODO Auto-generated method stub

                    returnsession.createTextMessage(String.valueOf(id));

                }

            });

        }

    }    

}

 

 

  1. 接收消息 从ActiveMQ服务器

在service-solr 项目中 SearchServiceImpl 中创建一个添加方法取名叫insertProductToSolr()

 

创建自定义消息处理类

        创建消息监听处理类

import javax.jms.Message;

import javax.jms.MessageListener;

import org.apache.activemq.command.ActiveMQTextMessage;

import org.springframework.beans.factory.annotation.Autowired;

import cn.core.service.SearchService;

 

publicclass CustomMessageListenerimplements MessageListener {

    @Autowired

    private SearchServicesearchService;

 

    @Override

    publicvoid onMessage(Message msg) {

        //强转成activeMQ的消息

        ActiveMQTextMessageatm = (ActiveMQTextMessage)msg;

        try {

            Stringid = atm.getText();

            //保存商品信息到solr服务器

            searchService.insertProductToSolr(Long.parseLong(id));

        }catch (Exceptione) {

            e.printStackTrace();

        }

    }

}

 

配置ActiveMQ的监听器

 

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

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

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

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

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

    xmlns:task="http://www.springframework.org/schema/task"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://www.springframework.org/schema/context

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

        http://www.springframework.org/schema/aop

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

        http://www.springframework.org/schema/tx

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

        http://www.springframework.org/schema/task

        http://www.springframework.org/schema/task/spring-task-4.0.xsd

        http://code.alibabatech.com/schema/dubbo

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

        

        <!--配置连接MQ工厂,apache提供 -->

        <beanid="activeMQConnectionFactory"class="org.apache.activemq.ActiveMQConnectionFactory">

            <propertyname="brokerURL"value="tcp://192.168.200.128:61616"/>

            <propertyname="userName"value="admin"/>

            <propertyname="password"value="admin"/>

        </bean>

        

        <!--配置工厂的连接池 -->

        <beanid="pooledConnectionFactoryBean"class="org.apache.activemq.pool.PooledConnectionFactoryBean">

            <propertyname="connectionFactory"ref="activeMQConnectionFactory"/>

            <propertyname="maxConnections"value="20"/>

        </bean>

        

        <!--将上面的工厂交由spring管理 -->

        <beanid="singleConnectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory">

            <propertyname="targetConnectionFactory"ref="pooledConnectionFactoryBean"></property>

        </bean>

        

        <!--自定义的处理消息的类 -->

        <beanid="customMessageListener"class="cn.core.service.message.CustomMessageListener"></bean>

 

        <!--监听ActiveMQ消息服务器 -->

        <beanclass="org.springframework.jms.listener.DefaultMessageListenerContainer">

            <!-- 1.连接MQ -->

            <propertyname="connectionFactory"ref="singleConnectionFactory"/>

            <!-- 2.监听目标 -->

            <propertyname="destinationName"value="productId"></property>

            <!-- 3.自定义的处理消息的类 -->

            <propertyname="messageListener"ref="customMessageListener"></property>

        </bean>

</beans>

 

 

好了,大功告成;

原创粉丝点击