Spring AMQP instances and spring configuration file Dynamic injection properties use combat

Source: Internet
Author: User
Tags aop xmlns
1. Project Structure



The key is the jar package, how the jar package is referenced improperly, and there are many problems. The jar package is as follows:

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 In addition to this above the jar package, due to the error, I also added a few jars such as AOP. may be different from the spring version. The final jar package is shown in the screenshot below. 2. Configuration Files

The namespace of the configuration file is incorrect, which results in a syntax check, and it is important to note that there are several debugs here, as follows:


<?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 "/> & Lt;bean id= "ConnectionFactory" class= "Org.springframework.amqp.rabbit.connection.CachingConnectionFactory" > &L T;constructor-arg value= "10.220.202.127"/> <property name= "port" value= "5672"/> <pro Perty name= "username" value= "ngmauser"/> <property name= "password" value= "Ngmauser"/> &L T;property name= "channelcachesize" value= "/> </be"An>--<span style= "color: #ff0000;" > <bean id= "propertyplaceholderconfigurer" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Ignoreresourcenotfound "value=" true "/> <property name=" Locations "> <list> <val ue>file:///d:/cluster.properties</value> </list> </property> &LT;/BEAN&GT;&LT;/SPAN&G
    T <!--three ways to test success, the problem is how to pass in User/pwd, the second how to implement the address dynamic import-<!--<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 St yle= "COLOR: #ff0000;" > addresses= "${host1},${host2},${host3}"/></span> <rabbit:template id= "AmqpTemplate" Connection-fa ctory= "ConnectionFactory" exchange= "Myexchange "routing-key=" Kerry "/> <rabbit:admin connection-factory=" ConnectionFactory "/> <rabbit:queue Name= "Myqueue"/> <!--exchagne type is topic and <rabbit:topic-exchange name= "Myexchange" > <rab bit:bindings> <!--sender:routing-key Receiver:binding-key-<rabbit:binding queue= "MyQueue" Patt Ern= "Kerry"/> </rabbit:bindings> </rabbit:topic-exchange> <rabbit:listener-container Connection -factory= "ConnectionFactory" > <rabbit:listener ref= "foo" method= "Listen" queue-names= "Myqueue"/> </rabb it:listener-container> <bean id= "foo" class= "Com.springamqp.Foo"/> <bean id= "test" class= "Com.abin.actio N.test "> <property name=" string "value=" Kerry "/> </bean> </beans>

3. Key Code

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 (+);}}}
		
	


Package COM.SPRINGAMQP;
public class Foo {public

    void Listen (String Foo) {
    	System.out.println ("Enter Listen Method");
        System.out.println (foo);
    }
}


4. Test results


Dynamically inject the server configuration file into the spring configuration file, Cluster.properties. As shown below:


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


Spring is a perfect support for failover between three servers. Because the project is released as a war or jar, the lab information needs to be changed and the configuration file cannot be written to die in spring, so attribute injection is done.


Log can be viewed under the VAR/LOGRABBITMQ directory


Full code download Address: http://download.csdn.net/detail/hanruikai/8423285

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.