如何在使用RabbitMQ时分别配置消费者和生产者的Spring Boot应用程序?

huangapple go评论71阅读模式
英文:

How to configure separately both consumer and producer spring boot app when using rabbitmq?

问题

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)

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)

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;
    }
}
英文:

I watched a tutorial that explains how to integrate RabbitMQ into Spring Boot app. In the tutorial, the ReceiveMessageHandler.java (Consumer) and SendMessageController.java (Producer) classes were in the same project. I want to implement them in two different Spring Boot application. However I can't split the tutorial project into two consumer and producer project because of the ConfigureRabbitMq class. Because it is coupled both ReceiveMessageHandler.java (Consumer) and SendMessageController.java (Producer) classes.

How can I implement and configure two different cosnumer and producer Spring Boot application?

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)

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)

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;
    }
}

答案1

得分: 1

你需要在这两个项目中配置RabbitMQ,但是在生产者项目的ConfigureRabbitMq类中不需要创建以下的bean:

@Bean
MessageListenerAdapter listenerAdapter(ReceiveMessageHandler handler){
    return new MessageListenerAdapter(handler, "handleMessage");
}
英文:

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 :

@Bean
MessageListenerAdapter listenerAdapter(ReceiveMessageHandler handler){
     return new MessageListenerAdapter(handler, "handleMessage");
}

huangapple
  • 本文由 发表于 2020年8月16日 23:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63438793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定