Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战

来源:互联网 发布:java 当前时间加2小时 编辑:程序博客网 时间:2024/06/06 01:57
http://blog.csdn.net/hanruikai/article/details/43487565

pring AMQP实例 以及Spring 配置文件动态注入属性使用实战

分类: RabbitMQ spring 1254人阅读 评论(0) 收藏 举报

目录(?)[+]

1. 项目结构



关键是jar包,jar包如何引用不当,会出现许多问题。jar包如下:

  • spring-amqp-1.0.0.M1.jar
  • spring-erlang-1.0.0.M1.jar
  • spring-rabbit-1.0.0.M1.jar
  • spring-rabbit-admin-1.0.0.M1.jar
  • spring-aop-3.0.3.RELEASE.jar
  • spring-beans-3.0.3.RELEASE.jar
  • spring-context-3.0.3.RELEASE.jar
  • spring-context-support-3.0.3.RELEASE.jar
  • spring-core-3.0.3.RELEASE.jar
  • spring-expression-3.0.3.RELEASE.jar
  • commons-cli-1.1.jar
  • commons-io-1.2.jar
  • rabbitmq-client.jar除此上面的jar包外,由于报错,我还加入了aop等几个jar。 可能跟spring版本不同有关系。目前最后的jar包如截图所示。

    2. 配置文件

    配置文件的namespace不正确,会导致语法校验不过,需要注意,此处经过几次调试,如下所示:


    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
    5.     http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">  
    6.   
    7.     <!--  
    8.     <rabbit:connection-factory id="connectionFactory"  host="localhost"/>  
    9.        
    10.     <bean id="connectionFactory"  class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">  
    11.        <constructor-arg value="10.220.202.127"/>  
    12.              <property name="port" value="5672"/>  
    13.              <property name="username" value="ngmauser"/>  
    14.              <property name="password" value="ngmauser"/>  
    15.             <property name="channelCacheSize" value="25"/>  
    16.    </bean>  
    17.     -->  
    18.    <span style="color:#ff0000;">  <bean id="propertyPlaceholderConfigurer"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    19.         <property name="ignoreResourceNotFound" value="true" />  
    20.         <property name="locations">  
    21.         <list>  
    22.             <value>file:///D:/cluster.properties</value>  
    23.         </list>  
    24.     </property>  
    25.     </bean></span>  
    26.     <!-- 三种方式都测试成功,问题是如何传入user/pwd,第二如何实现地址动态导入 -->  
    27.   <!--   
    28.    <rabbit:connection-factory id="connectionFactory" username="ngmauser" password="ngmauser"  addresses="10.220.202.126:5672,10.220.202.127:5672,10.220.202.128:5672"/> 
    29.    -->  
    30.    <rabbit:connection-factory id="connectionFactory" username="ngmauser" password="ngmauser" <span style="color:#ff0000;"> addresses="${host1},${host2},${host3}"/></span>  
    31.      
    32.      
    33.     <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"  
    34.         exchange="myExchange" routing-key="kerry" />  
    35.   
    36.     <rabbit:admin connection-factory="connectionFactory" />  
    37.   
    38.     <rabbit:queue name="myQueue" />  
    39.       
    40.       
    41.     <!-- exchagne type is topic -->  
    42.     <rabbit:topic-exchange name="myExchange">  
    43.         <rabbit:bindings>  
    44.             <!-- sender: routing-key  receiver:binding-key -->  
    45.             <rabbit:binding queue="myQueue" pattern="kerry" />  
    46.         </rabbit:bindings>  
    47.     </rabbit:topic-exchange>  
    48.   
    49.   
    50.     <rabbit:listener-container  
    51.         connection-factory="connectionFactory">  
    52.         <rabbit:listener ref="foo" method="listen"  
    53.             queue-names="myQueue" />  
    54.     </rabbit:listener-container>  
    55.   
    56.     <bean id="foo" class="com.springamqp.Foo" />  
    57.   
    58.   
    59.     <bean id="test" class="com.abin.action.Test">  
    60.         <property name="string" value="kerry" />  
    61.   
    62.     </bean>  
    63.   
    64. </beans>  

    3. 关键代码

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. package com.springamqp;  
    2.   
    3. import org.springframework.amqp.rabbit.core.RabbitTemplate;  
    4. import org.springframework.context.support.AbstractApplicationContext;  
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    6.   
    7. public class TestSpringAmqp {  
    8.       
    9.     public static void main(String[] args) throws InterruptedException {  
    10.           
    11.           AbstractApplicationContext ctx =  
    12.                     new ClassPathXmlApplicationContext("application-context.xml");  
    13. //        AmqpTemplate  template = (AmqpTemplate) ctx.getBean("amqpTemplate");  
    14.           RabbitTemplate template = ctx.getBean(RabbitTemplate.class);  
    15. //              Test test=(Test) ctx.getBean("test");  
    16. //              test.say();  
    17.                 while(true){  
    18.                 template.convertAndSend("foo.bar");  
    19.                 Thread.sleep(3000);  
    20.                 }  
    21.     }  
    22.           
    23.     }  

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. package com.springamqp;  
    2. public class Foo {  
    3.   
    4.     public void listen(String foo) {  
    5.         System.out.println("enter listen method");  
    6.         System.out.println(foo);  
    7.     }  
    8. }  


    4. 测试结果


    在spring配置文件中动态注入server配置文件,cluster.properties. 如下所示:


    host1=10.220.202.126:5672
    host2=10.220.202.127:5672
    host3=10.220.202.128:5672


    spring完美支持在三台server之间进行failover。由于项目发布的时候是个war或者jar,所以需要更改lab信息,配置文件不能在spring中写死,因此进行属性注入。


    可以在var/lograbbitmq 目录下查看log


  •  完整代码下载地址:http://download.csdn.net/detail/hanruikai/8423285

  • 0 0
    原创粉丝点击