0

I have a challenge to set up a service in SpringBoot, where it will be listening to several queues. I searched a lot and couldn't find what I was looking for. I have queues, which can grow dynamically.

Exemple: queue-1, queue-2, queue-3...

What could I use in this service to get this service up by listening to these queues dynamically?

gstbia
  • 25
  • 2

1 Answers1

0

Using spring JMS you can do this like it is done here Your config file is like this:

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

Another way is using RabbitListenerConfigurer. You can get more idea from here code from this link: for 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());
   }
}

service you can find here

Neeraj
  • 121
  • 3