Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

RabbitMQ integrates with spring and fully configured producers and consumers

Shulou Source: shulou.com Published: 2022-06-02 02:07:23 09月22日 Update

This article introduces the integration of RabbitMQ and spring as well as the complete configuration of producers and consumers, the content is very detailed, interested friends can refer to, hope to be helpful to you.

For more information on RabbitMQ and AMQP protocols, you can take a look at this http://www.cnblogs.com/frankyou/p/5283539.html.

Here is the configuration for the integration of rabbitMQ and spring. I configured two kinds of ExCahange: topicExchange and directExChange

Spring public configuration

Generator demo example:

@ Controller @ RequestMapping ("/ amqpTest") public class AmqpTestController {@ Autowired private AmqpTemplate amqpTemplate; @ RequestMapping ("/ sendMsg") @ ResponseBody public String sendAmqbMsg (Model model,@RequestParam (value= "msg", defaultValue= "hello worldview!") String msg) {if (modelling empty boxes! "" .equals (msg)) {amqpTemplate.convertAndSend ("mq.asdfExChange", "mq.asdf.send", msg) } else {amqpTemplate.convertAndSend ("mq.asdfExChange", "mq.asdf.send", "hello world");} return "success" } @ RequestMapping ("/ sendMsg2") @ ResponseBody public String sendAmqbMsg2 (Model model,@RequestParam (value= "msg", defaultValue= "hello worldviews!") String msg) {if (modelling empty boxes! "" .equals (msg)) {amqpTemplate.convertAndSend ("mq.asdfExChange", "mq.asdf2.send", "it's a wonderful world!") } else {amqpTemplate.convertAndSend ("mq.asdfExChange", "mq.asdf2.send", "it's a wonderful world");} return "success" } @ RequestMapping ("/ sendMsg3") @ ResponseBody public String sendAmqbMsg3 (Model model,@RequestParam (value= "msg", defaultValue= "hello worldviews!") String msg) {if (modelling empty worlds! "" .equals (msg)) {amqpTemplate.convertAndSend ("mq.qwerExChange", "mq.qwer.send", "magical world!") } else {amqpTemplate.convertAndSend ("mq.qwerExChange", "mq.qwer.send", "Magic World");} return "success";}}

Consumers:

Here I list a Demo according to the configuration

Consumer

There is no problem with path matching. Public class AsdfConsumer implements MessageListener {

Static {System.out.println ("already dependent on success");} public void onMessage (Message message) {MessageProperties m=message.getMessageProperties (); / / System.out.println (m); String msg= new String (message.getBody ()); System.out.println ("consumed:" + msg+ "- >") }} this is the end of the integration of RabbitMQ and spring and the complete configuration of producers and consumers. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Configuration consumption consumer world producer production wonderful magical content more help good success examples interests guys partners articles knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Shulou Information Redmi Microsoft