zoukankan      html  css  js  c++  java
  • RabbitMQ学习之基于spring-rabbitmq的消息异步发送

    spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码。由于我使用的rabbitmq版本是3.0.4,部分代码做了调整。

    具体实例如下(创建自动删除非持久队列):

    1.资源配置application.properties

    [plain] view plain copy
     
     print?
    1. #============== rabbitmq config ====================  
    2. rabbit.hosts=192.168.36.102  
    3. rabbit.username=admin  
    4. rabbit.password=admin  
    5. rabbit.virtualHost=/  
    6. rabbit.exchange=spring-queue-async  
    7. rabbit.queue=spring-queue-async  
    8. rabbit.routingKey=spring-queue-async  


    2..发送端配置applicationContext-rabbitmq-async-send.xml

    [html] view plain copy
     
     print?
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:context="http://www.springframework.org/schema/context"  
    4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.        xsi:schemaLocation="  
    6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    8.   
    9.      <context:property-placeholder location="classpath:application.properties"/>  
    10.        
    11.      <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">  
    12.         <property name="connectionFactory">  
    13.             <bean class="com.rabbitmq.client.ConnectionFactory">  
    14.                <property name="username" value="${rabbit.username}"/>  
    15.                <property name="password" value="${rabbit.password}"/>  
    16.                <property name="virtualHost" value="${rabbit.virtualHost}"/>  
    17.             </bean>  
    18.         </property>  
    19.         <property name="hosts" value="${rabbit.hosts}"/>  
    20.     </bean>  
    21.      <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">  
    22.         <property name="connectionFactory" ref="rabbitConnectionFactory"/>  
    23.     </bean>  
    24.       
    25.     <bean id="rabbitTemplate" class="com.rabbitmq.spring.template.ASyncRabbitTemplate">  
    26.         <property name="channelFactory" ref="rabbitChannelFactory"/>  
    27.         <property name="exchange" value="${rabbit.exchange}"/>  
    28.         <property name="routingKey" value="${rabbit.routingKey}"/>  
    29.         <!--optional-->  
    30.         <property name="exchangeType" value="TOPIC"/>  
    31. <!--         mandatory是否强制发送       -->  
    32.         <property name="mandatory" value="false"/>  
    33. <!--         immediate是否立即发送   -->  
    34.         <property name="immediate" value="false"/>  
    35.     </bean>  
    36.       
    37. </beans>  

    3.接收端配置applicationContext-rabbitmq-async-receive.xml

    [html] view plain copy
     
     print?
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:context="http://www.springframework.org/schema/context"  
    4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.        xsi:schemaLocation="  
    6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    8.   
    9.      <context:property-placeholder location="classpath:application.properties"/>  
    10.        
    11.      <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">  
    12.         <property name="connectionFactory">  
    13.             <bean class="com.rabbitmq.client.ConnectionFactory">  
    14.                <property name="username" value="${rabbit.username}"/>  
    15.                <property name="password" value="${rabbit.password}"/>  
    16.                <property name="virtualHost" value="${rabbit.virtualHost}"/>  
    17.             </bean>  
    18.         </property>  
    19.         <property name="hosts" value="${rabbit.hosts}"/>  
    20.     </bean>  
    21.      <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">  
    22.         <property name="connectionFactory" ref="rabbitConnectionFactory"/>  
    23.     </bean>  
    24.       
    25.     <bean id="receiveMsgHandler" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async.ReceiveMsgHandler"/>  
    26.       
    27.      <bean id="quotingParamtersTopicAdapter" class="com.rabbitmq.spring.listener.RabbitMessageListenerAdapter">  
    28.         <property name="channelFactory" ref="rabbitChannelFactory"/>  
    29.         <property name="delegate" ref="receiveMsgHandler"/>  
    30.         <property name="listenerMethod" value="handleMessage"/>  
    31.         <property name="exchange" value="${rabbit.exchange}"/>  
    32.         <!--optional-->  
    33.         <property name="exchangeType" value="TOPIC"/>  
    34.         <property name="routingKey" value="${rabbit.routingKey}"/>  
    35.         <property name="queueName" value="${rabbit.queue}"/>  
    36.         <property name="poolsize" value="5"/>  
    37.     </bean>  
    38. </beans>  

    4.消息处理服务ReceiveMsgHandler.Java

    [java] view plain copy
     
     print?
    1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;  
    2.   
    3. public class ReceiveMsgHandler {  
    4.   
    5.     public void handleMessage(String text) {  
    6.         System.out.println("Received: " + text);  
    7.     }  
    8. }  


    5.发送端启动代码Send.java

    [java] view plain copy
     
     print?
    1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;  
    2.   
    3. import org.springframework.context.ApplicationContext;  
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    5.   
    6. import com.rabbitmq.spring.template.ASyncRabbitTemplate;  
    7.   
    8. public class Send {  
    9.   
    10.     public static void main(String[] args) throws InterruptedException {  
    11.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-send.xml");    
    12.         ASyncRabbitTemplate amqpTemplate = context.getBean(ASyncRabbitTemplate.class);    
    13.         for(int i=0;i<10000;i++){  
    14.             amqpTemplate.send("test spring async=>"+i);   
    15.             Thread.sleep(100);  
    16.         }  
    17.     }  
    18. }  

    6.接收端启动代码Send.java

    [java] view plain copy
     
     print?
    1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;  
    2.   
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    4.   
    5. public class Receive {  
    6.   
    7.     public static void main(String[] args) {  
    8.          new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-receive.xml");  
    9.     }  
    10. }  

    先启动接收端,再启动发送端。接收到消息如下:

    [plain] view plain copy
     
     print?
    1. Received: test spring async=>0  
    2. Received: test spring async=>1  
    3. Received: test spring async=>2  
    4. Received: test spring async=>3  
    5. Received: test spring async=>4  
    6. Received: test spring async=>5  
    7. Received: test spring async=>6  
    8. Received: test spring async=>7  
    9. ......  

    实例代码:http://download.csdn.net/detail/tianwei7518/8135637

  • 相关阅读:
    mac下使用brew安装mongodb
    从零构建vue+webpack (一)
    常用软件集合(2018/08/22)
    solr集群安装部署
    zookeeper集群部署
    redis集群部署
    linux 安装jdk
    zTab layui多标签页组件
    spring boot集成swagger2
    SSH客户端,FinalShell服务器管理,远程桌面加速软件,支持Windows,Mac OS X,Linux,版本2.6.3.1
  • 原文地址:https://www.cnblogs.com/telwanggs/p/7124785.html
Copyright © 2011-2022 走看看