您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐