Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
2015-02-04 16:03
1096 查看
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不正确,会导致语法校验不过,需要注意,此处经过几次调试,如下所示:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd"> <!-- <rabbit:connection-factory id="connectionFactory" host="localhost"/> <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="10.220.202.127"/> <property name="port" value="5672"/> <property name="username" value="ngmauser"/> <property name="password" value="ngmauser"/> <property name="channelCacheSize" value="25"/> </bean> --> <span style="color:#ff0000;"> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>file:///D:/cluster.properties</value> </list> </property> </bean></span> <!-- 三种方式都测试成功,问题是如何传入user/pwd,第二如何实现地址动态导入 --> <!-- <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"/> --> <rabbit:connection-factory id="connectionFactory" username="ngmauser" password="ngmauser" <span style="color:#ff0000;"> addresses="${host1},${host2},${host3}"/></span> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="myExchange" routing-key="kerry" /> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:queue name="myQueue" /> <!-- exchagne type is topic --> <rabbit:topic-exchange name="myExchange"> <rabbit:bindings> <!-- sender: routing-key receiver:binding-key --> <rabbit:binding queue="myQueue" pattern="kerry" /> </rabbit:bindings> </rabbit:topic-exchange> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="foo" method="listen" queue-names="myQueue" /> </rabbit:listener-container> <bean id="foo" class="com.springamqp.Foo" /> <bean id="test" class="com.abin.action.Test"> <property name="string" value="kerry" /> </bean> </beans>
3. 关键代码
package com.springamqp; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpringAmqp { public static void main(String[] args) throws InterruptedException { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml"); // AmqpTemplate template = (AmqpTemplate) ctx.getBean("amqpTemplate"); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); // Test test=(Test) ctx.getBean("test"); // test.say(); while(true){ template.convertAndSend("foo.bar"); Thread.sleep(3000); } } }
package com.springamqp; public class Foo { public void listen(String foo) { System.out.println("enter listen method"); System.out.println(foo); } }
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
相关文章推荐
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- spring-自动加载配置文件\使用属性文件注入
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- spring-自动加载配置文件\使用属性文件注入
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- spring-自动加载配置文件\使用属性文件注入
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- Spring根据XML配置文件 p名称空间注入属性的实例
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件