Azure 服务总线队列动态 Spring

Azure Service Bus queues dynamic Spring

我很难在 SpringBoot 中设置一个服务,它将监听多个队列。我搜索了很多,找不到我要找的东西。我有队列,可以动态增长。

示例:queue-1、queue-2、queue-3...

通过动态侦听这些队列,我可以在此服务中使用什么来启动此服务?

使用 spring JMS 你可以做到这一点 你的配置文件是这样的:

 @Configuration
 @EnableJms
 public class AppConfig implements JmsListenerConfigurer {

@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
    List<QueueInformation> queueInformationList = consumersStatic.getQueueInformationList();
    int i = 0;
    for (QueueInformation queueInformation :
            queueInformationList) {
        SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
        endpoint.setId("myJmsEndpoint-" + i++);
        endpoint.setDestination(queueInformation.getMqQueueName());
        endpoint.setMessageListener(message -> {
            logger.debug("***********************************************receivedMessage:" + message);
        });
        registrar.registerEndpoint(endpoint);
        logger.debug("registered the endpoint for queue" + queueInformation.getMqQueueName());
   }
 }

另一种方法是使用 RabbitListenerConfigurer。您可以从 here 获得更多想法 link 中的代码: 对于 rabbitconfig:

    @Configuration
    public class RabbitMqConfiguration implements RabbitListenerConfigurer {
    @Autowired
    private ConnectionFactory connectionFactory;
    @Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
        return new MappingJackson2MessageConverter();
    }
    @Bean
    public RabbitTemplate rabbitTemplate() {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
        return rabbitTemplate;
    }
    @Bean
    public RabbitAdmin rabbitAdmin() {
        return new RabbitAdmin(connectionFactory);
    }
    @Bean
    public RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry() {
        return new RabbitListenerEndpointRegistry();
    }
    @Bean
    public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        factory.setMessageConverter(consumerJackson2MessageConverter());
        return factory;
    }
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    @Override
    public void configureRabbitListeners(final RabbitListenerEndpointRegistrar registrar) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(1);
        factory.setConsecutiveActiveTrigger(1);
        factory.setConsecutiveIdleTrigger(1);
        factory.setConnectionFactory(connectionFactory);
        registrar.setContainerFactory(factory);
        registrar.setEndpointRegistry(rabbitListenerEndpointRegistry());           
  registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
   }
}

您可以找到的服务