ActiveMq消息队列

1.什么是Activemq

官网:http://activemq.apache.org/

Apache ActiveMQ ™ is the most popular and powerful open source messaging and Integration Patterns server.

Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols, comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. Apache ActiveMQ is released under the Apache 2.0 License.

Apache ActiveMQ是最流行、最强大的开源消息传递和集成模式服务器。Apache ActiveMQ是快速的,它支持许多跨语言客户端和协议,在完全支持JMS 1.1和J2EE 1.3的同时,很容易使用企业集成模式和许多高级特性。Apache ActiveMQ是在Apache 2.0许可下发布的。

2.应用场景:



异步处理:当用户提交交易请求,消息中间进行记录,保证事务最终执行完成。

在分布式系统中,通过消息中间件,应用程序或者组件之间可以进行可靠的异步通讯,从而降低系统之间的耦合度,

提高系统的可扩展性和可用性。


3.ActiveMQ消息生产方配置:

mq.xml:

<beans xmlns="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">
      <!--消息生产方-->
      <!-- 配置 ActiveMQ   四步获取JmsTemplate-->
   <!--1.工厂  原厂商提供-->
   <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" >
      <!--设置链接URL-->  <!--浏览器访问地址: http://192.168.200.128:8161 -->
      <property name="brokerURL" value="tcp://192.168.200.128:61616"/>
      <!--用户名-->
      <property name="userName" value="admin"/>
      <!--密码-->
      <property name="password" value="admin"/>
   </bean>
   <!--2.工厂连接池-->
   <bean id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
      <property name="connectionFactory" ref="activeMQConnectionFactory"/>
      <!--最大连接数-->
      <property name="maximumActive" value="2"/>
   </bean>
   <!--3.Spring管理以上工厂-->
   <bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
      <property name="targetConnectionFactory" ref="pooledConnectionFactoryBean"/>
   </bean>
   <!--4.获取jmdTemplate 由spring提供-->
   <bean class="org.springframework.jms.core.JmsTemplate">
      <property name="connectionFactory" ref="singleConnectionFactory"/>
      <!--指定默认目标   管道名称-->
      <property name="defaultDestinationName" value="productId"/>
   </bean>
</beans>

发送消息:

spring扫描mq.xml文件,JmsTemplate对象就有spring管理,直接注入使用:

@Autowired
private JmsTemplate jmsTemplate;

jmsTemplate.send(new MessageCreator() {
    @Override
    public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage(String.valueOf(id));  //发送消息
    }
});
4.ActiveMQ消息消费方配置:

mq.xml

<beans xmlns="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">

      <!--消息接收方-->
      <!-- 配置 ActiveMQ -->

   <!--1.工厂  原厂商提供-->
   <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" >
      <!--设置链接URL-->  <!--浏览器访问地址: http://192.168.200.128:8161 -->
      <property name="brokerURL" value="tcp://192.168.200.128:61616"/>
      <!--用户名-->
      <property name="userName" value="admin"/>
      <!--密码-->
      <property name="password" value="admin"/>
   </bean>

   <!--2.工厂连接池-->
   <bean id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
      <property name="connectionFactory" ref="activeMQConnectionFactory"/>
      <!--最大连接数-->
      <property name="maximumActive" value="2"/>
   </bean>

   <!--3.Spring管理以上工厂-->
   <bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
      <property name="targetConnectionFactory" ref="pooledConnectionFactoryBean"/>
   </bean> 

   <!--4.获取jmdTemplate 由spring提供-->
<!--   <bean class="org.springframework.jms.core.JmsTemplate">
      <property name="connectionFactory" ref="singleConnectionFactory"/>
      &lt;!&ndash;指定默认目标   管道名称&ndash;&gt;
      <property name="defaultDestinationName" value="productId"/>
   </bean>-->

   <!--处理消息的类 自定义-->
   <bean id="customMessageListener" class="com.lyb.core.message.CustomMessageListener">

   </bean>

   <!--Spring 监听器 监听ActiveMQ中是否有消息-->
   <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
      <!--链接上ActiveMQ-->
      <property name="connectionFactory" ref="singleConnectionFactory"/>
      <!--指定监听的目标-->
      <property name="destinationName" value="productId"/>
      <!--监听到消息后有个类处理此信息  处理信息的类-->
      <property name="messageListener" ref="customMessageListener"/>
   </bean>
</beans>

接收消息:(消息处理类(自定义的))

package com.lyb.core.message;
import com.lyb.core.service.SearchService;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;


public class CustomMessageListener implements MessageListener{
    @Autowired
    private SearchService searchService;
    @Override
    public void onMessage(Message message) {
        ActiveMQTextMessage am=(ActiveMQTextMessage) message;//转换类型
ActiveMQTextMessage
try { System.out.println("ActiveMQ中消息内容:"+am.getText()); //获取消息 searchService.insertProductToSolr(Long.parseLong(am.getText())); } catch (JMSException e) { e.printStackTrace(); } }}




猜你喜欢

转载自blog.csdn.net/AaronLin6/article/details/79854297