Search code examples
springspring-integrationspring-integration-dsl

Spring Integration Default Message Listener Container IsRunning false


I have a JMS Message Listener defined in Java DSL using the below configuration. The listener works fine. It processes the messages from queue as expected. I had not configured autostartup as the default value is true and I need the listener to be running once the application starts. When i try to invoke the isRunning() method during application startup, isRunning method returns false. Is there some configuration that I am missing here

/**
Configuration for  the Message Listener
**/
@Bean
public DefaultMessageListenerContainer inListener(){
DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
defaultMessageListenerContainer.setConnectionFactory((ConnectionFactory)connectionFactory().getObject());
defaultMessageListenerContainer.setDestinationName(queueName);
defaultMessageListenerContainer.setIdleTaskExecutionLimit(100);
defaultMessageListenerContainer.setMaxConcurrentConsumers(1);
defaultMessageListenerContainer.setSessionTransacted(true);
defaultMessageListenerContainer.setPubSubDomain(false);
defaultMessageListenerContainer.setDestinationResolver(jndiDestResolver);
}

@Bean
public JndiObjectFactoryBean connectionFactory(){
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate);
jndiObjectFactoryBean.setJndiName(jndiName);
return jndiObjectFactoryBean;
}

Message Listener

@Bean
public IntegrationFlow adapter(){
return IntegrationFlow.from(Jms.messageDrivenChannelAdapter(inListener()).channel(inputChannel()).get();
}

Code for IsRunning: This code is being used in the postconstruct method of another class. The first print statement for Autostartup is printing true while the isRunning print statement is printing false. Is there some configuration that is missing here

AbstractMessageListenerContainer listener = (AbstractMessageListenerContainer) applicationContext.getBean("inListener");
System.out.println("Autostartup: "+ listener.isAutoStartup());
System.out.println("Running: "+ listener.isRunning());


Solution

  • The @PostConstruct is too early for lifecycle control. This one is fully an equivalent to the afterPropertiesSet() for particular bean. The start() for autoStartup lifecylces is called a bit later when all the beans in the application context is ready already.

    For your logic you should consider really to implement a SmartLifecycle to perform a desired logic.

    See more info in docs: https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-factory-lifecycle-processor