added configuration file for redis

This commit is contained in:
Konstantinos Spyrou 2024-09-09 12:58:59 +03:00
parent 6a4c939df2
commit efdeb01b4e
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
package eu.dnetlib.repo.manager.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
/**
* Custom RedisTemplate using as Value serializer the {@link Jackson2JsonRedisSerializer}
* to avoid serialization issues between versions.
*
* @param connectionFactory the redis connection factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
template.setKeySerializer(new StringRedisSerializer());
return template;
}
/**
* Custom RedisMessageListenerContainer to override error handling behaviour.
*
* @param connectionFactory the redis connection factory
* @return
*/
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setErrorHandler(e -> {
logger.error("An error occurred in Redis message listener: " + e.getMessage());
});
return container;
}
}