added configuration file for redis
This commit is contained in:
parent
6a4c939df2
commit
efdeb01b4e
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue