Proficient in SpringBoot – integrate RabbitMQ message queues

developlee

  • java
  • The message queue
  • springboot
  • string
  • static
  • Annotation
  • class
  • void

Abstract: Springboot integrates message queue RabbitMQ

Recently, due to personal reasons, I haven’t updated my blog for several days. Learn how to integrate RabbitMQ with SpringBoot. There are three main message components:.ActivemQ, RabbitMQ and Kafka. All three have their advantages and disadvantages, and RabbitMQ is a messaging component in between the other two. RabbitMQ relies on Erlang, and to install RabbitMQ on Linux, you must install the Erlang environment first. Let’s see how SpringBoot integrates RabbitMQ.

  1. Pom dependencies are necessary to use RabbitMQ
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>Copy the code

2. Look again at the contents of the application.yml file

spring: rabbitmq: username: rabbit password: 123456 host: localhost port: 5672 virtual-host: / # manual ACK Indicates that automatic ACK mode is not enabled. The default value is None listener: simple: acknowledge-mode: manualCopy the code

The content of the RabbitMQConfig

import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { public static final String DEFAULT_MAIL_QUEUE = "dev.mail.register.default.queue"; public static final String MANUAL_MAIL_QUEUE = "dev.mail.register.manual.queue"; @bean public Queue defaultMailQueue (){// Queue Queue = new Queue(Queue name, Return new Queue(DEFAULT_MAIL_QUEUE, true); } @Bean public Queue manualMailQueue(){ return new Queue(MANUAL_MAIL_QUEUE, true); }}Copy the code

Use two listeners (@rabbitListener) to listen for these two messages.

import com.developlee.rabbitmq.config.RabbitMQConfig; import com.developlee.rabbitmq.entity.MailEntity; import com.rabbitmq.client.Channel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.io.IOException; @Component public class MailHandler { private static final Logger logger = LoggerFactory.getLogger(MailHandler.class); /** * <p>TODO This is the default mode of spring-boot-data-amqp, which is not recommended. ListenerManualAck ()</p> * By default, if manual ACK is not configured, Spring Data AMQP will automatically ACK the message after it has been consumed. If an error is reported, the message will not be lost, but it will be infinite loop consumption, always reported errors, if enabled error logging is very easy to run out of disk space * Solution: ACK manually, or try-catch then speak in catch error message transfer to other series of * spring in the rabbitmq. Listener. Simple. Acknowledge - mode = manual * < p > * * */ @rabbitListener (queues = {rabbitmqconfig.default_mail_queue}) public void listenerAutoAck(MailEntity mail, Message message, Channel Channel) {//TODO If you manually ACK, the message will be monitored for consumption, but the message still exists in the queue. If no acknowledge-mode is configured, the final long deliveryTag will be automatically ACK after consumption = message.getMessageProperties().getDeliveryTag(); Try {logger.info("listenerAutoAck listens for messages -{}", mail.toString()); //TODO notifies MQ that the message has been successfully consumed and can be ACK channel.basicack (deliveryTag, false); } catch (IOException e) {// failed to process, re-press mq.try {channel.basicRecover(); } catch (IOException e1) { e1.printStackTrace(); } } } @RabbitListener(queues = {RabbitMQConfig.MANUAL_MAIL_QUEUE}) public void listenerManualAck(MailEntity mail, Message Message, Channel Channel) {logger.info("listenerManualAck listens for messages -{}", mail.toString()); Try {/ / TODO inform MQ message has been successfully consumption, can the ACK channel. BasicAck (message. GetMessageProperties () getDeliveryTag (), false); } catch (Exception e) {}Copy the code

One more wave of test code, test……

import com.developlee.rabbitmq.config.RabbitMQConfig; import com.developlee.rabbitmq.entity.MailEntity; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Lee * @// TODO 2018/6/22-11:20 * @description */ @RestController @RequestMapping(value = "/mail") public class MailController { private final RabbitTemplate rabbitTemplate; @Autowired public MailController(RabbitTemplate rabbitTemplate) { this.rabbitTemplate = rabbitTemplate; } /** * this.rabbitTemplate.convertAndSend(RabbitConfig.DEFAULT_MAIL_QUEUE, mailEntity); * corresponding {@link MailHandler#listenerAutoAck}; * this.rabbitTemplate.convertAndSend(RabbitConfig.MANUAL_MAIL_QUEUE, mailEntity); * corresponding {@link MailHandler#listenerManualAck}; */ @GetMapping("/default") public void defaultMailMsg() { MailEntity mailEntity = new MailEntity(); mailEntity.setId("1"); mailEntity.setName("First Mail Message"); mailEntity.setTitle("RabbitMQ with Spring boot!" ); mailEntity.setContent("Come on! Let's study Micro-Service together!" ); this.rabbitTemplate.convertAndSend(RabbitMQConfig.DEFAULT_MAIL_QUEUE, mailEntity); this.rabbitTemplate.convertAndSend(RabbitMQConfig.MANUAL_MAIL_QUEUE, mailEntity); }}Copy the code

MailEntity.java

import java.io.Serializable; public class MailEntity implements Serializable { private static final long serialVersionUID = -2164058270260403154L; private String id; private String name; private String title; private String content; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; }}Copy the code

Start the project, the browser address bar enter http://localhost:8080/mail. Something you will find in your heart.

Typhoon and rainstorm today, same tomorrow. But the day after tomorrow it may be sunny and maybe a rainbow. — By a struggling programmer.

Use the cloud habitat community APP, comfortable ~

For details, please click
Comments (0)

To:


Related articles

  • The RabbitMQ SpringBoot integration
  • Spring Boot 1.5.4 Integrate rabbitMQ…
  • Spring Boot uses RabbitMQ
  • Proficient in SpringBoot – integrate Redis implementation cache
  • Springboot (8) : RabbitMQ details
  • Springboot (8) : RabbitMQ details
  • Springboot (8) : RabbitMQ details
  • On August 3, the cloud selected night | alibaba announced Sentinel…
  • Springboot uses rabbitMQ fanout…
  • SpringBoot Development Case: The Whole…

The net friend comment on