【问题标题】:How to create dynamic queues in rabbit mq using spring boot?如何使用spring boot在rabbit mq中创建动态队列?
【发布时间】:2020-01-12 05:12:47
【问题描述】:

我需要一些帮助。

我正在开发一个 Spring Boot 应用程序,我想将消息发布到 rabbitMQ。我想将它发送到一个队列,该队列在消息本身中命名。这样我想动态创建队列。 我只找到了使用“静态”队列的示例。

我已经研究了一些东西,但没有找到任何东西。 我是 RabbitMQ 的新手,学习了基本概念。 我对春天也很陌生。

RabbotMQ 配置

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

消息发送者

@Service
public class RabbitMQSender {

    @Autowired
    private AmqpTemplate template;

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

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

    }
}

【问题讨论】:

标签: java spring rabbitmq queue amqp


【解决方案1】:

我想出了一个解决办法:

您需要在配置中创建 AmqpAdmin:

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

然后你将它添加到你的服务中:

@Autowired
private AmqpAdmin admin;

最后你可以使用它来创建队列和绑定。

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

我找到了解决方案here

【讨论】:

    猜你喜欢
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2010-12-19
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多