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.应用场景:

ActiveMq消息队列_第1张图片

ActiveMq消息队列_第2张图片

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

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

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


3.ActiveMQ消息生产方配置:

mq.xml:

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">
      
      
   
   id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" >
        
      name="brokerURL" value="tcp://192.168.200.128:61616"/>
      
      name="userName" value="admin"/>
      
      name="password" value="admin"/>
   
   
   id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
      name="connectionFactory" ref="activeMQConnectionFactory"/>
      
      name="maximumActive" value="2"/>
   
   
   id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
      name="targetConnectionFactory" ref="pooledConnectionFactoryBean"/>
   
   
   class="org.springframework.jms.core.JmsTemplate">
      name="connectionFactory" ref="singleConnectionFactory"/>
      
      name="defaultDestinationName" value="productId"/>
   

发送消息:

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

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">

      
      

   
   id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" >
        
      name="brokerURL" value="tcp://192.168.200.128:61616"/>
      
      name="userName" value="admin"/>
      
      name="password" value="admin"/>
   

   
   id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
      name="connectionFactory" ref="activeMQConnectionFactory"/>
      
      name="maximumActive" value="2"/>
   

   
   id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
      name="targetConnectionFactory" ref="pooledConnectionFactoryBean"/>
    

   


   
   id="customMessageListener" class="com.lyb.core.message.CustomMessageListener">

   

   
   class="org.springframework.jms.listener.DefaultMessageListenerContainer">
      
      name="connectionFactory" ref="singleConnectionFactory"/>
      
      name="destinationName" value="productId"/>
      
      name="messageListener" ref="customMessageListener"/>
   

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

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(); } }}




你可能感兴趣的:(ActiveMq消息队列)