Home > Net >  Can one Queue Deliver messages to multiple Consumer In fanout exchange?
Can one Queue Deliver messages to multiple Consumer In fanout exchange?

Time:04-04

I want to know do we need to create three different queues to deliver message to three different consumer? Like here I have created 3 queues and bind it to the fanout exchange and then I in publisher class I am passing list of Product Model from POSTMAN and I am creating 3 consumer for that.

Here is the Config Class

@Configuration
public class MessagingConfig {
    public static final String QUEUE1 = "queue_one";
    public static final String QUEUE2 = "queue_two";
    public static final String QUEUE3 = "queue_three";
    public static final String EXCHANGE = "fanout-exchange";

    @Bean
    public Queue queue1() {
        return new Queue(QUEUE1);
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE2);
    }

    @Bean
    public Queue queue3() {
        return new Queue(QUEUE3);
    }

    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(EXCHANGE);
    }

    @Bean
    public org.springframework.amqp.core.Binding binding1(Queue queue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue1).to(fanoutExchange);
    }

    @Bean
    public org.springframework.amqp.core.Binding binding2(Queue queue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue2).to(fanoutExchange);
    }

    @Bean
    public org.springframework.amqp.core.Binding binding3(Queue queue3, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue3).to(fanoutExchange);
    }

    @Bean
    public MessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate template(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(converter());
        return rabbitTemplate;
    }
}

Here is the Publisher Class

@RestController
@RequestMapping("/check")
public class ProductPublisher {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/inventory")
    public String checkInventory(@RequestBody List<Product> product) {
        for (Product product1 : product) {
            if (product1.getQuantity() < 10) {
                ProductInventory productInventory = new ProductInventory(product1, "Low Quantity",
                        "This Product is low on Quantity in Inventory");
                rabbitTemplate.convertAndSend(MessagingConfig.EXCHANGE, "", productInventory);
                break;
            }
        }
        return "Success !!";
    }
}

CodePudding user response:

No.

In amqp, if you need the same message to be received by multiple consumers, you'd need need multiple queues.

A queue could be shared by many consumers but then each consumer would get its share of messages in a round-robin fashion (useful for distributed workers).

The tutorials at https://www.rabbitmq.com/getstarted.html should clarify these concepts, especially the # 3, titled "Sending messages to many consumers at once".

  • Related