Search code examples
javaspringredisspring-data-redis

Spring: RedisMessageListenerContainer not working when running app from command line via java -jar


I have the following RedisConfig in my spring app

@Configuration
public class RedisConfig {

    @Value("${redis.hostname}")
    private String redisHostName;

    @Value("${redis.port}")
    private String redisPort;

    @Value("${intercom.chats.sync.enable}")
    Boolean enableSync;

    private static final Logger log = LoggerFactory.getLogger(RedisConfig.class.getName());

    @Bean
    public RedisMessageSubscriber redisMessageSubscriber() {
        log.debug("Entered redisMessageSubscriber()");
        return new RedisMessageSubscriber();
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter() {
        log.debug("Entered messageListenerAdapter()");
        MessageListenerAdapter listener = new MessageListenerAdapter(redisMessageSubscriber());
        listener.setSerializer(new StringRedisSerializer());
        return listener;
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisFactory) {
        log.debug("Entered redisMessageListenerContainer");

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisFactory);
        if (!enableSync) {
            log.debug("Subscribing to the redis queue {}");
            // Only set the listener if the sync is not enabled
            container.addMessageListener(messageListenerAdapter(),
                    Arrays.asList(new ChannelTopic(Constants.EVENT_MESSAGE_KEY)));
        }
        container.afterPropertiesSet();
        return container;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(redisHostName);

        // Checking if redisPort string is an integer or not, this is used in
        // the case of CI environment
        if (redisPort.matches("^-?\\d+$")) {
            factory.setPort(Integer.parseInt(redisPort));
        }
        factory.setUsePool(true);
        return factory;
    }

}

The redisMessageSubscriber() prints the log and so does messageListenerAdapter() but redisMessageListenerContainer() doesn't print any which means that it is never entered. The function is not even entered and so none of the 2 logs inside it are displayed.

The result is that it doesn't subscribe to any events. I don't understand why its not working for me. Can someone share their thoughts. Thanks !!

Edit: I found that this happens only if I run the project from the terminal using jar -jar command. If I start it from eclipse it works fine :/


Solution

  • I was able to make it work. I had added the annotation @EnableRedisHttpSession on a config class and this was causing the issue. I guess both of them (@EnableRedisHttpSession and RedisMessageListenerContainer) don't work at the same time, though I would like to get a better answer. :/