Reprint please indicate the source:
http://blog.csdn.net/forezp/article/details/71023692
This article comes from Fang Zhibong's blog
This article takes you through how to integrate the RABBITMQ server and how to send and receive messages through it. I will build a springboot project to subscribe to a Pojo-type message via the Rabbittemplate messagelisteneradapter. ready to work 15min idea maven 3.0
Before starting to build the project, the machine needs to install RABBITMQ, you can go to the website to download, http://www.rabbitmq.com/download.html, if you are using the Mac (programmers should use Mac Bar), you can download:
Brew Install RABBITMQ
When the installation is complete, turn on the server:
Rabbitmq-server
To enable the server to succeed, you can see the following information:
RABBITMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.
# # # # # licensed under the MPL. http://www.rabbitmq.com/# # # # #
########## Logs:/usr/local/var/log/rabbitmq/ Rabbit@localhost.log
###### # # /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##### #####
starting broker ... completed with 6 plugins.
Build the Project
The architecture of a Springboot project, its pom file dependencies plus Spring-boot-starter-amqp's start dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-amqp</artifactid>
</dependency>
Create message Recipients
In any Message Queuing program, you need to create a message receiver to respond to the message sent.
@Component public
class Receiver {
private countdownlatch latch = new Countdownlatch (1);
public void ReceiveMessage (String message) {
System.out.println ("Received <" + message + ">");
Latch.countdown ();
}
Public Countdownlatch Getlatch () {return
latch;
}
}
The message receiver is a simple Pojo class that defines a method to receive messages, and when you register it to receive messages, you can give it any name. Among them, it has countdownlatch such a class, it is used to tell the sender that the message has been received, you do not need to implement it specifically in the application, only need Latch.countdown () on the line. Create a message listener and send a message
In the spring program, Rabbittemplate provides all the methods for sending and receiving messages. All you need is a simple configuration: A message listening container declares a quene, an exchange, and binds them to a component to send a message
The code listing is as follows:
Package Com.forezp;
Import Com.forezp.message.Receiver;
Import org.springframework.amqp.core.Binding;
Import Org.springframework.amqp.core.BindingBuilder;
Import Org.springframework.amqp.core.Queue;
Import Org.springframework.amqp.core.TopicExchange;
Import Org.springframework.amqp.rabbit.connection.ConnectionFactory;
Import Org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
Import Org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.context.annotation.Bean;
@SpringBootApplication public class Springbootrabbitmqapplication {final static String QueueName = "Spring-boot";
@Bean Queue Queue () {return new queue (QueueName, false);
@Bean Topicexchange Exchange () {return new Topicexchange ("Spring-boot-exchange"); } @Bean Binding Binding (QueueQueue, Topicexchange Exchange) {return Bindingbuilder.bind (queue). to (Exchange). with (QueueName);
} @Bean Simplemessagelistenercontainer container (connectionfactory connectionfactory, Messagelisteneradapter listeneradapter) {simplemessagelistenercontainer container = new Simplem
Essagelistenercontainer ();
Container.setconnectionfactory (ConnectionFactory);
Container.setqueuenames (QueueName);
Container.setmessagelistener (ListenerAdapter);
return container; } @Bean messagelisteneradapter ListenerAdapter (Receiver Receiver) {return new Messagelisteneradapter (rec
Eiver, "ReceiveMessage");
public static void Main (string[] args) {Springapplication.run (springbootrabbitmqapplication.class, args);
}
}
To create a test method:
@Component public
class Runner implements Commandlinerunner {
private final rabbittemplate rabbittemplate;
Private final Receiver Receiver;
Private final configurableapplicationcontext context;
Public Runner (Receiver Receiver, Rabbittemplate rabbittemplate,
configurableapplicationcontext context) {
This.receiver = receiver;
This.rabbittemplate = rabbittemplate;
This.context = context;
}
@Override public
void Run (String ... args) throws Exception {
System.out.println ("Sending message ...");
Rabbittemplate.convertandsend (Application.queuename, "Hello from rabbitmq!");
Receiver.getlatch (). Await (10000, timeunit.milliseconds);
Context.close ();
}
Start the program and you'll find the console print:
Sending message ...
Received
Summary
Congratulations. You've just learned how to build a program for sending and subscribing to messages through spring RAABITMQ. This is just a good start, you can do more with SPRING-RABBITMQ, click here.
SOURCE Download: https://github.com/forezp/SpringBootLearning Reference
https://spring.io/guides/gs/messaging-rabbitmq/ Excellent article recommendation: more Springboot Tutorials: Springboot Unofficial Tutorials | Article summary more Springcoud tutorials: The simplest springcloud tutorials in history | Article Summary