Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2222

Spring-data-redis Jedis RedisMessageListenerContainer -Connection failure occurred. Restarting subscription task after 5000 ms

$
0
0

I found some similar problems here but the solutions does not fix mine, there seems to be a mistake in my message listener (subscription) configuration.

I am pretty new with redis and I would like to lister/read/subscribe to an existing redis server which does not have any password.

I can listen to that server via redis-cli on my local, but when I try to add a redis listener to my spring boot app, it seems the message listener container cannot establish a connection.

I am getting this repeating message:

2021-01-18 22:12:32 [redisContainer-31] ERROR o.s.d.r.l.RedisMessageListenerContainer -Connection failure occurred. Restarting subscription task after 5000 ms

Below are some of the snippets on how I add spring-data-redis (2.3.3.RELEASE)+jedis (2.10.0) spring boot to my application.

pom.xml

  ...<properties>    ...<redis.version>2.3.3.RELEASE</redis.version><jedis.version>2.10.0</jedis.version></properties><dependencies>    ...  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>${redis.version}</version></dependency><!-- There is no redis.clients.util.SafeEncoder class definition in version 3.1.0 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${jedis.version}</version></dependency></dependencies>  ...

application.properties

# Redisspring.redis.database=0spring.redis.host=sabong-dev.abs3252.8888.use69.cache.amazonaws.comspring.redis.port=6379spring.redis.password=spring.redis.timeout=60000spring.redis.channel.sabongEvents=SABONG_EVENTS

please note that there is no password for connecting to the existing redis server, and I can read/listener to SABONG_EVENTS locally using redis-cli.

RedisConfig.java

@Configurationpublic class RedisConfig {    @Value("${spring.redis.host}")    private String redisHost;    @Value("${spring.redis.port}")    private Integer redisPort;    @Value("${spring.redis.channel.sabongEvents}")    private String channelTopic;    @Bean    JedisConnectionFactory jedisConnectionFactory() {        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort);        return new JedisConnectionFactory(redisStandaloneConfiguration);    }    @Bean    public RedisTemplate<String, Object> redisTemplate() {        RedisTemplate<String, Object> template = new RedisTemplate<>();        template.setConnectionFactory(jedisConnectionFactory());        return template;    }    @Bean    MessageListenerAdapter messageListener() {        return new MessageListenerAdapter(new MyRedisMessageSubscriber());    }    @Bean    RedisMessageListenerContainer redisContainer() {        RedisMessageListenerContainer container = new RedisMessageListenerContainer();        container.setConnectionFactory(jedisConnectionFactory());        container.addMessageListener(messageListener(), channelTopic());        return container;    }    @Bean    ChannelTopic channelTopic() {        return new ChannelTopic(channelTopic);    }}

MyRedisMessageSubscriber.java

package package.my.sample;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import org.springframework.stereotype.Service;...@Servicepublic class RedisMessageSubscriber implements MessageListener {    private static final Logger logger = LoggerFactory.getLogger(MyRedisMessageSubscriber.class);    public void onMessage(Message message, byte[] pattern) {        String msg = message.toString();        logger.info("\n[topic-message]: {}\n", msg);    }}

I am not sure what is wrong with my codes, I know this is very simple since I just want to display sysout/logger.info the messages I get from redis channel.

The application runs but it cannot establish a connection inorder to make read/listen.

I may be missing some codes or configuration here but I am not sure where or what.

Thanks!

Update (Solution)It seems spring-boot did not automagically set my redis host and port, maybe because I missed something or some wrong property names causing it not set automatically.

In my redis configuration class, I set the redis host and port in my JedisConnectionFactory using RedisStandaloneConfiguration. Since my problem is just establishing the connection to my message listener container.. it was able to make a connection to my external redis server with this sample.

Instead of removing this post, I just updated it with answer cause I know I might be needing this again since I am very forgetful :)


Viewing all articles
Browse latest Browse all 2222

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>