Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
来源:互联网 发布:java 当前时间加2小时 编辑:程序博客网 时间:2024/06/06 01:57
http://blog.csdn.net/hanruikai/article/details/43487565spring-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包如截图所示。
pring AMQP实例 以及Spring 配置文件动态注入属性使用实战
分类: RabbitMQ spring2015-02-04 16:03 1254人阅读 评论(0) 收藏 举报
目录(?)[+]
1. 项目结构
关键是jar包,jar包如何引用不当,会出现许多问题。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
0 0
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- spring属性的注入实例
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- spring mvc注入配置文件里的属性
- spring mvc注入配置文件里的属性
- spring注入配置文件属性到java类
- spring mvc注入配置文件里的属性
- Spring实战-注入Bean属性(二)
- 使用Spring注解注入属性
- spring使用案例 属性注入
- 使用spring注解注入属性
- Spring AMQP
- spring 注入 动态注入
- 使用spring进行属性注入的实例解析(spring面向切面和控制反转)。
- spring-依赖注入 配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- oldssoj2672Y sequence(mobius函数容斥+迭代)
- MVC拦截器加密解密
- The influence of depreciation on plywood export
- 黑马程序员——C语言中的标识符
- 每天进步一点点————事务控制(1)
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- 使用 Twitter Storm 处理实时的大数据
- 计算机网络组网与配置技术—— 读书笔记6、路由器、交换机的配置
- 常见的字符串匹配算法对比实现C语言版本
- jquery的validate最基础使用总结
- Unity之弹床(蹦床效果)
- 改变tableview的分割线长、颜色问题 以及自定义分割线
- Html5画布canvas小例
- 韦东山视频