[英]How to configure separately both consumer and producer spring boot app when using rabbitmq?
I watched a tutorial that explains how to integrate RabbitMQ into Spring Boot app.我观看了一个教程,该教程解释了如何将 RabbitMQ 集成到 Spring 引导应用程序中。 In the tutorial, the
ReceiveMessageHandler.java
(Consumer) and SendMessageController.java
(Producer) classes were in the same project.在本教程中,
ReceiveMessageHandler.java
(消费者)和SendMessageController.java
(生产者)类在同一个项目中。 I want to implement them in two different Spring Boot application.我想在两个不同的 Spring 引导应用程序中实现它们。 However I can't split the tutorial project into two consumer and producer project because of the
ConfigureRabbitMq
class.但是,由于
ConfigureRabbitMq
class,我无法将教程项目分成两个消费者和生产者项目。 Because it is coupled both ReceiveMessageHandler.java
(Consumer) and SendMessageController.java
(Producer) classes.因为它耦合了
ReceiveMessageHandler.java
(消费者)和SendMessageController.java
(生产者)类。
How can I implement and configure two different cosnumer and producer Spring Boot application?如何实现和配置两个不同的 cosnumer 和 producer Spring 引导应用程序?
ConfigureRabbitMq.java
import com.example.demorabbitmq.rabbitmq.consumer.ReceiveMessageHandler;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigureRabbitMq {
public static final String EXCHANGE_NAME = "mikeexchange2";
public static final String QUEUE_NAME = "mikequeue2";
@Bean
Queue createQueue() {
return new Queue(QUEUE_NAME, true, false, false);
}
@Bean
TopicExchange exchange(){
return new TopicExchange(EXCHANGE_NAME);
}
@Bean
Binding binding(Queue q, TopicExchange exchange){
return BindingBuilder.bind(q).to(exchange).with("mike.#");
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory
, MessageListenerAdapter messageListenerAdapter){
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(QUEUE_NAME);
container.setMessageListener(messageListenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(ReceiveMessageHandler handler){
return new MessageListenerAdapter(handler, "handleMessage");
}
}
ReceiveMessageHandler.java
(Consumer) ReceiveMessageHandler.java
(消费者)
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class ReceiveMessageHandler {
public void handleMessage(String messageBody){
log.info("HandleMessage!!!");
log.info(messageBody);
}
}
SendMessageController.java
(Producer) SendMessageController.java
(生产者)
import com.example.demorabbitmq.rabbitmq.ConfigureRabbitMq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SendMessageController {
private final RabbitTemplate rabbitTemplate;
public SendMessageController(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@PostMapping("/send")
public String sendMessage(@RequestParam String themessage){
rabbitTemplate.convertAndSend(ConfigureRabbitMq.EXCHANGE_NAME,
"mike.springmessages", themessage);
return "We have sent a message! :" + themessage;
}
}
You need to configure RabbitMQ in both of the projects, however you DON'T need to create the following bean in Producer project's ConfigureRabbitMq.class:您需要在两个项目中配置 RabbitMQ,但是您不需要在 Producer 项目的 ConfigureRabbitMq.class 中创建以下 bean:
@Bean
MessageListenerAdapter listenerAdapter(ReceiveMessageHandler handler){
return new MessageListenerAdapter(handler, "handleMessage");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.