简体   繁体   English

使用 rabbitmq 时如何分别配置消费者和生产者 spring 启动应用程序?

[英]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.

相关问题 如何在Spring Boot Rabbitmq中分别配置生产者和消费者? - How to separately configure producer and consumer in spring boot rabbitmq? Spring Rabbitmq - 如何在不使用@RabbitListener 的情况下配置消费者 - Spring Rabbitmq - how to configure the consumer without using @RabbitListener 如何使用 Spring Boot 应用程序配置 ActiveMQ 独占消费者 - How to configure ActiveMQ exclusive consumer with Spring boot app Spring Cloud Stream 不会向 RabbitMQ 发送消息,而是在同一个 pod 中消费。 生产者和消费者在同一个应用程序中 - Spring Cloud Stream is not sending messages to RabbitMQ, instead consumes in the same pod. Producer and Consumer are in the same app 如何在Spring Boot API中实现RabbitMQ消费者部分(receiver) - How to Implement RabbitMQ consumer part(receiver ) inside the Spring boot API 如何在Spring Boot中设置amqp RabbitMQ消费者标签? - How to set amqp RabbitMQ consumer tag in Spring Boot? 如何为python发送者创建spring boot rabbitmq使用者? - How to create a spring boot rabbitmq consumer for a python sender? 如何在 Spring Cloud Stream 中同时具有消费者和生产者事务的应用程序中设置事务 ID 前缀 - How to set transaction-id-prefix in app which has both consumer and producer-only transactions in Spring Cloud Stream 如何在Tomcat上分别部署Spring Boot和angular项目 - How to deploy both Spring boot and angular project separately on apache tomcat 如何在Spring Boot和RabbitMQ中配置和receiveAndConvert jSON有效负载到域对象 - How to configure and receiveAndConvert jSON payload into domain Object in Spring Boot and RabbitMQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM