How to create dynamic queues in rabbit mq using spring boot?
Asked Answered
R

2

16

I need some help.

I'm developing a spring boot application, and I want wo publish messages to a rabbitMQ. I want to send it to a queue, that is named in the message itself. This way i want to create queues dynamicly. I only found examples that use a "static" queue.

I have reserched some things but didn't find anything. I'm new to RabbitMQ and learned the basic concepts. I'm also fairly new to spring.

RabbotMQ Config

@Configuration
public class RabbitMQConfig {

    @Value("amq.direct")
    String exchange;

    @Value("queue-name") // Don't want to do this
    String queueName;

    @Value("routing-key") // Or this
    String routingkey;

    @Bean
    Queue queue() {
        return new Queue(queueName, true);
    }

    @Bean
    DirectExchange exchange() {
        return new DirectExchange(exchange);
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingkey);
    }

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

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

MessageSender

@Service
public class RabbitMQSender {

    @Autowired
    private AmqpTemplate template;

    @Value("amq.direct")
    private String exchange;

    public void send(MessageDTO message) {
        template.convertAndSend(exchange, message);

    }
}
Radiance answered 10/9, 2019 at 12:32 Comment(5)
have a look here: #24242380Melmela
also here: #46872774Melmela
Thanks! @BenjaminSlabbert That RabbitAdmin helped a lotRadiance
How can I tag this as closed?Radiance
best way would be to post your solution as an answer and then mark it as accepted or give it an upvoteMelmela
R
28

I came to a solution:

You need to create a AmqpAdmin in your config:

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory);
}

Then you add it to your service:

@Autowired
private AmqpAdmin admin;

Finally you can use it to create queues and bindings.

Queue queue = new Queue(queueName, durable, false, false);
Binding binding = new Binding(queueName, Binding.DestinationType.QUEUE, EXCHANGE, routingKey, null);
admin.declareQueue(queue);
admin.declareBinding(binding);

I found the solution here

Radiance answered 12/9, 2019 at 9:9 Comment(0)
O
0

Not sure which version of RabbitMQ you were on but, your original code was close. This works, too.

@Bean
Queue fanoutQueue() {
    // empty name, durable false, exclusive false, autoDelete false
    return new Queue("", false, false, true);
}

@Bean
FanoutExchange fanoutExchange() {
    return new FanoutExchange("fanout-exchange", true, false);
}

@Bean
Binding fanoutBinding(Queue fanoutQueue, FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
}
Orvil answered 13/6, 2022 at 18:59 Comment(1)
how this is dynamic Queue creation?Toggery

© 2022 - 2024 — McMap. All rights reserved.