1.Spring Boot Dependency Introduction
<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.tzm & lt;/groupid> <artifactId>rabbitmq</artifactId> <version>0.0.1-SNAPSHOT</version> &L T;packaging>jar</packaging> <name>rabbitmq</name> <description>demo Project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> < Artifactid>spring-boot-starter-parent</artifactid> <version>1.5.1.RELEASE</version> & Lt;relativepath/> <!--lookup parent from repository--</parent> <properties> < Project.build.sourceencoding>utf-8</project.Build.sourceencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependen Cy> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-start er-amqp</artifactid> </dependency> <dependency> <groupid>org.springfra Mework.boot</groupid> <artifactId>spring-boot-starter</artifactId> </DEPENDENCY&G
T <dependency> <groupId>org.springframework.boot</groupId> <artifactid>sprin g-boot-starter-test</artifactid> <scope>test</scope> </dependency> </de pendencies> <build> <plugins> <plugin> <groupid>org.sp Ringframework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> </plugi
n> </plugins> </build> </project>2. Create a configuration file
@Configuration public
class Rabbitconfig {public
static final String EXCHANGE = "Spring-fanout-exchange";
}
3. Configure the Factory
@Bean public
cachingconnectionfactory connectionfactory () {
cachingconnectionfactory connectionfactory = new Cachingconnectionfactory ();
Connectionfactory.setaddresses ("127.0.0.1:5672");
Connectionfactory.setusername ("Guest");
Connectionfactory.setpassword ("Guest");
Connectionfactory.setvirtualhost ("/");
Connectionfactory.setpublisherconfirms (TRUE); You must set the
return connectionfactory;
}
4. Create a pair of columns
@Bean Public Queue Queue
() {
return new queue ("Spring-queue", true);//queue Persistent
}
@Bean
public Queue Queue2 () {
return new queue ("Spring-queue2", true);//Queues Persistent
}
5. Defining the Exchanger
@Bean public
Fanoutexchange Fanoutexchange () {
return new Fanoutexchange (EXCHANGE);
}
6. Define Consumer
@Bean public Simplemessagelistenercontainer Messagecontainer () {Simplemessagelistenercontainer container = n
EW Simplemessagelistenercontainer (ConnectionFactory ());
Container.setqueues (Queue ());
Container.setexposelistenerchannel (TRUE);
Container.setmaxconcurrentconsumers (1);
Container.setconcurrentconsumers (1); Container.setacknowledgemode (acknowledgemode.manual); Set Confirmation mode manually confirm Container.setmessagelistener (new Channelawaremessagelistener () {@Override PU
Blic void onMessage (Message message, channel Channel) throws Exception {byte[] body = message.getbody ();
SYSTEM.OUT.PRINTLN ("Receive MSG B:" + New String (body));
Thread.Sleep (30000); Channel.basicack (Message.getmessageproperties (). Getdeliverytag (), false);
Confirmation message successfully consumed});
return container; } @Bean Public SimplemessagelistenerContainer MessageContainer2 () {Simplemessagelistenercontainer Container = new Simplemessagelistenercontainer (conn
Ectionfactory ());
Container.setqueues (Queue2 ());
Container.setexposelistenerchannel (TRUE);
Container.setmaxconcurrentconsumers (1);
Container.setconcurrentconsumers (1); Container.setacknowledgemode (acknowledgemode.manual); Set Confirmation mode manually confirm Container.setmessagelistener (new Channelawaremessagelistener () {@Override PU
Blic void onMessage (Message message, channel Channel) throws Exception {byte[] body = message.getbody ();
SYSTEM.OUT.PRINTLN ("Receive msg A:" + new String (body));
Thread.Sleep (30000); Channel.basicack (Message.getmessageproperties (). Getdeliverytag (), false);
Confirmation message successfully consumed});
return container; }
At this point we start the project, open the RABBITMQ admin interface to see the factory created a link for us,
We have the channel, too.
To columns:
Switch:
But we find that there are no pairs of columns bound on the exchanger:
So we need to bind the next step. 7. Binding to columns and switches
@Bean Public
Binding binding () {
return Bindingbuilder.bind (Queue ()). to (Fanoutexchange ());
@Bean public
Binding binding2 () {
return Bindingbuilder.bind (Queue2 ()). to (Fanoutexchange ());
Start the Project Watch background management again, the column is already bound to the exchanger
8. Finally
This code will be implemented in the next section to implement one of the three switching modes Fanoutexchange feature detection