Through the understanding of the important classes of SPRING-AMQP, the following is the implementation of how to send messages and receive messages synchronously via SPRING-AMQP. Interested friends can go to the SPRING-AMQP official website to download examples.
First look at the Helloworldconfiguration class Java code package org.springframework.amqp.helloworld; import org.springframework.amqp.core.queue; import org.springframework.amqp.rabbit.config.abstractrabbitconfiguration; import org.springframework.amqp.rabbit.connection.connectionfactory; import org.springframework.amqp.rabbit.connection.singleconnectionfactory; import org.springframework.amqp.rabbit.core.rabbittemplate; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @Configuration public class helloworldconfiguration extends abstractrabbitconfiguration { protected final String helloWorldQueueName = "Hello.world.queue"; @Bean public ConnectionFactory ConnectionFactory () { singleconnectionfactory connectionfactory = new singleconnectionfactory ( "localhost"); connectionfactory.setusername ("Guest"); connectionfactory.setpassword ("Guest"); return connectionFactory; } @Override public rabbittemplate rabbittemplate () { rabbittemplate template = nEw rabbittemplate (ConnectionFactory ()); // the routing key is set to the name of the queue by the broker for the // default exchange. template.setroutingkey (this.helloworldqueuename); // // Where we will synchronously receive messages from Template.setqueue (This.helloworldqueuename); return template; } @Bean public queue helloworldqueue () { &NBsp; return new queue (This.helloworldqueuename); } }
Package Org.springframework.amqp.helloworld;
Import Org.springframework.amqp.core.Queue;
Import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;
Import Org.springframework.amqp.rabbit.connection.ConnectionFactory;
Import Org.springframework.amqp.rabbit.connection.SingleConnectionFactory;
Import Org.springframework.amqp.rabbit.core.RabbitTemplate;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration; @Configuration public class Helloworldconfiguration extends Abstractrabbitconfiguration {protected final String Hellowo
Rldqueuename = "Hello.world.queue"; @Bean public ConnectionFactory connectionfactory () {singleconnectionfactory connectionfactory = new Singleconnectionfa
Ctory ("localhost");
Connectionfactory.setusername ("Guest");
Connectionfactory.setpassword ("Guest");
return connectionfactory; } @Override Public Rabbittemplate rabbittemplate () {rabbittemplate template = new RabbittemPlate (ConnectionFactory ());
The routing key is set to the name of the ' queue by ' broker for the//default Exchange.
Template.setroutingkey (This.helloworldqueuename);
Where we'll synchronously receive messages from Template.setqueue (this.helloworldqueuename);
return template;
} @Bean Public Queue Helloworldqueue () {return new queue (this.helloworldqueuename); }
}
This class defines connectionfactory, Rabbittemplate, Queue
The program that sends the message is as follows: Java code package org.springframework.amqp.helloworld; import org.springframework.amqp.core.amqptemplate; import org.springframework.context.applicationcontext; import org.springframework.context.annotation.annotationconfigapplicationcontext; public class producer { Public static void main (String[] args) { applicationcontext context = new annotationconfigapplicationcontext ( Helloworldconfiguration.class); AmqpTemplate amqpTemplate = Context.getbean (Amqptemplate.class); amqptemplate.convertandsend ("Hello World");        &NBsp System.out.println ("Sent: hello world"); } }
package Org.springframework.amqp.helloworld;
Import Org.springframework.amqp.core.AmqpTemplate;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Producer {public static void main (string[] args) {ApplicationContext context = new ANNOTATIONCONFIGAPPL
Icationcontext (Helloworldconfiguration.class);
Amqptemplate amqptemplate = Context.getbean (Amqptemplate.class);
Amqptemplate.convertandsend ("Hello World");
System.out.println ("Sent:hello World"); }
}
synchronously receive messages as follows: Java code package org.springframework.amqp.helloworld; import org.springframework.amqp.core.amqptemplate; import org.springframework.context.applicationcontext; import org.springframework.context.annotation.annotationconfigapplicationcontext; public class consumer { public static void main (String[] args) { ApplicationContext context = new Annotationconfigapplicationcontext (Helloworldconfiguration.class); AmqpTemplate amqpTemplate = Context.getbean (Amqptemplate.class); system.out.println ("received: " + Amqptemplate.receiveandconvERT ()); } }
Package Org.springframework.amqp.helloworld;
Import org.springframework.amqp.core.AmqpTemplate;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Consumer {public
static void Main (string[] args) {
ApplicationContext context = new Annotationconfig ApplicationContext (helloworldconfiguration.class);
Amqptemplate amqptemplate = Context.getbean (amqptemplate.class);
System.out.println ("Received:" + Amqptemplate.receiveandconvert ());
}
}
This example is an exchange type of directexchange. The name of the Routingkey is the name of the queue by default.
The configuration of the Helloworldconfiguration class can also be configured through the spring XML file. For example
Rabbitconfiguration.xml
Java code <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http ://www.springframework.org/schema/beans " xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!-- Create connectionfactory --> <bean id= "ConnectionFactory" class= " Org.springframework.amqp.rabbit.connection.SingleConnectionFactory "> <constructor-arg value= "localhost" /> <property name= "username" value= "Guest" /> <property name= "Password" value= "Guest" /> </bean> <bean id= "Rabbitadmin" class= "Org.springframework.amqp.rabbit.core.RabbitAdmin" > <constructor-arg ref= " ConnectionFactory " /> </bean> <bean id= "Rabbittemplate" class= " Org.springframework.amqp.rabbit.core.RabbitTemplate "> <constructor-arg ref= "ConnectionFactory" ></constructor-arg> <property name= "queue" value= "Hello.world.queue" ></ Property> &nbsP; <property name= "Routingkey" value= "Hello.world.queue" ></property> </bean> <!-- Declare queue and set the name of the queue --> <bean id= " Helloworldqueue " class=" Org.springframework.amqp.core.Queue "> < Constructor-arg value= "Hello.world.queue" ></constructor-arg> </bean> </beans>
Reference: http://wubin850219.iteye.com/blog/1050328