0

I know how to create a single JMS listener with Springboot with annotations. But now I want to create several JMS listeners listening at several brokers sending same kind of messages at server startup, reading in a file the properties of brokers.

How can I achieve this ? Is it possible to ask SpringBoot to create the beans with java statements instead of annotations ? With a factory or something like that ?

I know there won't be more than 10 brokers in the system. Is there a solution to define statically 10 JMS listeners with annotations but deactivating some listeners if they are not used so that they don't cause errors ?

mvera
  • 904
  • 12
  • 23

1 Answers1

1

My answer relates to: "Is there a solution to define statically 10 JMS listeners with annotations but deactivating some listeners if they are not used so that they don't cause errors ?" and not the Dynamic portion of creating JMSListeners on the fly.

You can use @ConditionalOnProperty to enable/disable your consumer and use profiles to specify when they are enabled.

Example:

@Slf4j
@Service
@ConditionalOnProperty(name = "adapter-app-config.jms.request.enable-consumer", havingValue = "true")
public class RequestMessageConsumer {

    @Autowired
    private AdapterConfig adapterConfig;

    @Autowired
    private RequestMessageHandler requestMessageHandler;


    @JmsListener(concurrency = "1", destination = "${adapter-app-config.jms.request.InQueue}")
    @Transactional(rollbackFor = { Exception.class })
    public void onMessage(TextMessage message) {

        requestMessageHandler.handleMessage(message);
    }

}

application.properties

adapter-app-config:
  jms:
    sync:
      enable-consumer: true
    request:
      enable-consumer: false

For Dynamic, please see:

Adding Dynamic Number of Listeners(Spring JMS)

https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#jms-annotated-programmatic-registration

JCompetence
  • 6,997
  • 3
  • 19
  • 26