In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Spring Boot integration
Environment:
RabbitMQ:3.7.4
Spring Boot:2.0.1.RELEASE
Because of Starter POMs, it is very easy to integrate RabbitMQ in Spring Boot, and the AMQP module can support RabbitMQ very well.
We can use Spring Intializr or https://start.spring.io/ to create a Spring Boot project and check RabbitMQ.
Or manually add it to the pom.xml file
Org.springframework.boot spring-boot-starter-amqp
Configure the connection and user information about RabbitMQ in application.yml. If you do not change the default configuration of RabbitMQ, you can start with zero configuration here. Here we also define some additional configuration backup.
Spring: profiles: active: usage_message rabbitmq: port: 5672tutorial: client: duration: 10000
Producer
Spring AMQP makes it easy to send and receive messages with a small amount of code. Messages are sent by injecting an instance of the AmqpTemplate interface, which defines a set of basic operations for the AMQP protocol. The concrete implementation is injected in Spring Boot based on configuration (the default implementation of AmqpTemplate is RabbitTemplate).
Public class Tut1Sender {@ Autowired private AmqpTemplate template; @ Autowired private Queue queue; / * uses scheduled tasks to simulate producer timed sending messages * / @ Scheduled (fixedDelay = 1000, initialDelay = 1000) public void send () {String message = "Hello World!" + new Date (); template.convertAndSend (queue.getName (), message) System.out.println ("[x] Sent'" + message + "'");}}
In this producer, we generate a string and send it to a queue named "hello-world".
Consumer
Create a consumer Receiver. This class's listening to the "hello-world" queue is defined through the @ RabbitListener annotation, and the @ RabbitHandler annotation is used to specify how to handle the message. Therefore, the consumer implements the consumption of the "hello-world" queue, which operates as the string content of the output message.
@ RabbitListener (queues = "hello-world") public class Tut1Receiver {@ RabbitHandler public void receive (String in) {System.out.println ("[x] Received'" + in + ");}}
Configuration class
Create a new JavaConfig file
@ Profile ({"tut1", "hello-world"}) @ Configurationpublic class Tut1Config {@ Bean public Queue queue () {return new Queue ("hello-world");} @ Profile ("receiver") @ Bean public Tut1Receiver receiver () {return new Tut1Receiver ();} @ Profile ("sender") @ Bean public Tut1Sender sender () {return new Tut1Sender ();}}
In the JavaConfig above, we use @ Configuration to let Spring know that this is a Java configuration and define producers, consumers, and a queue named "hello-world". Also, we use Spring Profiles to control which example it runs and whether it is a producer or a consumer, so that we can simply pass our configuration file through the startup parameters to launch the application correctly. To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59
As for the specific producers (Tut1Sender) and consumers (Tut1Receiver), we will only define them here and then implement them later.
Application main class
And then slightly modify the generated RabbitmqTutorialApplication.java.
@ SpringBootApplication@EnableSchedulingpublic class RabbitmqTutorialApplication {public static void main (String [] args) {new SpringApplicationBuilder () .sources (RabbitmqTutorialApplication.class) / / set to a non-web environment .web (WebApplicationType.NONE) .run (args) } @ Profile ("usage_message") @ Bean public CommandLineRunner usage () {return arg0-> {System.out.println ("This app uses Spring Profiles to control its behavior.\ n"); System.out.println ("Sample usage: java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-- spring.profiles.active=hello-world,sender");} @ Profile ("! usage_message") @ Bean public CommandLineRunner tutorial () {return new RabbitTutorialRunner ();}}
Here, I set the environment to WebApplicationType.NONE, that is, a non-WEB environment, because by default, Netty will listen on port 8080, and if it runs at the same time, it will cause interface conflicts to cause startup failure (of course, you can also directly bind different ports with parameters at startup to avoid conflicts).
The RabbitTutorialRunner is as follows
Public class RabbitTutorialRunner implements CommandLineRunner {@ Value ("${tutorial.client.duration:0}") private int duration; @ Autowiredprivate ConfigurableApplicationContext ctx; @ Override public void run (String...) Args) throws Exception {System.out.println ("Ready... running for" + duration + "ms"); Thread.sleep (duration); ctx.close ();}}
The main purpose of this Runner is to prevent the main thread from exiting. In addition to using Thread.sleep (millisecond), you can also use CountDownLatch to achieve the same goal.
Running
Compile
Mvn clean package-Dmaven.test.skip=true
Running
Java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-- spring.profiles.active=tut1,sender
Java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-- spring.profiles.active=tut1,receiver
Output
/ / Sender
Ready... Running for 10000ms
[X] Sent 'Hello Worldwide Thu Apr 12 16:56:01 CST 2018'
[X] Sent 'Hello Worldwide Thu Apr 12 16:56:03 CST 2018'
[X] Sent 'Hello Worldwide Thu Apr 12 16:56:04 CST 2018'
...
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.