|
| 1 | +package com.baeldung.spring.data.redis.config; |
| 2 | + |
| 3 | +import com.baeldung.spring.data.redis.queue.MessagePublisher; |
| 4 | +import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; |
| 5 | +import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; |
| 6 | +import org.springframework.context.annotation.Bean; |
| 7 | +import org.springframework.context.annotation.ComponentScan; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
| 10 | +import org.springframework.data.redis.core.RedisTemplate; |
| 11 | +import org.springframework.data.redis.listener.ChannelTopic; |
| 12 | +import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
| 13 | +import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; |
| 14 | +import org.springframework.data.redis.serializer.GenericToStringSerializer; |
| 15 | + |
| 16 | +@Configuration |
| 17 | +@ComponentScan("com.baeldung.spring.data.redis") |
| 18 | +public class RedisConfig { |
| 19 | + |
| 20 | + @Bean |
| 21 | + JedisConnectionFactory jedisConnectionFactory() { |
| 22 | + return new JedisConnectionFactory(); |
| 23 | + } |
| 24 | + |
| 25 | + @Bean |
| 26 | + public RedisTemplate<String, Object> redisTemplate() { |
| 27 | + final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); |
| 28 | + template.setConnectionFactory(jedisConnectionFactory()); |
| 29 | + template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class)); |
| 30 | + return template; |
| 31 | + } |
| 32 | + |
| 33 | + @Bean |
| 34 | + MessageListenerAdapter messageListener() { |
| 35 | + return new MessageListenerAdapter(new RedisMessageSubscriber()); |
| 36 | + } |
| 37 | + |
| 38 | + @Bean |
| 39 | + RedisMessageListenerContainer redisContainer() { |
| 40 | + final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); |
| 41 | + container.setConnectionFactory(jedisConnectionFactory()); |
| 42 | + container.addMessageListener(messageListener(), topic()); |
| 43 | + return container; |
| 44 | + } |
| 45 | + |
| 46 | + @Bean |
| 47 | + MessagePublisher redisPublisher() { |
| 48 | + return new RedisMessagePublisher(redisTemplate(), topic()); |
| 49 | + } |
| 50 | + |
| 51 | + @Bean |
| 52 | + ChannelTopic topic() { |
| 53 | + return new ChannelTopic("pubsub:queue"); |
| 54 | + } |
| 55 | +} |
0 commit comments