<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.example.spring</groupid> <artifactid>redis</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceencoding>UTF-8</project.build.sourceencoding> <spring.version>3.1.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-redis</artifactid> <version>1.0.0.RELEASE</version> </dependency> <dependency> <groupid>cglib</groupid> <artifactid>cglib-nodep</artifactid> <version>2.2</version> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.16</version> </dependency> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>2.0.0</version> <type>jar</type> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> </dependencies> </project>
package com.example.redis.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericToStringSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class AppConfig { @Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); } @Bean RedisTemplate< String, Object > redisTemplate() { final RedisTemplate< String, Object > template = new RedisTemplate< String, Object >(); template.setConnectionFactory( jedisConnectionFactory() ); template.setKeySerializer( new StringRedisSerializer() ); template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) ); template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) ); return template; } }
@Autowired private RedisTemplate< String, Object > template; public Object getValue( final String key ) { return template.opsForValue().get( key ); } public void setValue( final String key, final String value ) { template.opsForValue().set( key, value ); }
Optionally, the key could be set to expire (yet another useful feature of Redis), f.e. let our keys expire in 1 second:
public void setValue( final String key, final String value ) { template.opsForValue().set( key, value ); template.expire( key, 1, TimeUnit.SECONDS ); }
Arbitrary objects could be saved into Redis as hashes (maps), f.e. let save instance of some class User
public class User { private final Long id; private String name; private String email; // Setters and getters are omitted for simplicity }
into Redis using key pattern “user:<id>”:
public void setUser( final User user ) { final String key = String.format( "user:%s", user.getId() ); final Map< String, Object > properties = new HashMap< String, Object >(); properties.put( "id", user.getId() ); properties.put( "name", user.getName() ); properties.put( "email", user.getEmail() ); template.opsForHash().putAll( key, properties); }
Respectively, object could easily be inspected and retrieved using the id.
public User getUser( final Long id ) { final String key = String.format( "user:%s", id ); final String name = ( String )template.opsForHash().get( key, "name" ); final String email = ( String )template.opsForHash().get( key, "email" ); return new User( id, name, email ); }
@Bean MessageListenerAdapter messageListener() { return new MessageListenerAdapter( new RedisMessageListener() ); } @Bean RedisMessageListenerContainer redisContainer() { final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory( jedisConnectionFactory() ); container.addMessageListener( messageListener(), new ChannelTopic( "my-queue" ) ); return container; }
package com.example.redis.impl; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; public class RedisMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] paramArrayOfByte) { System.out.println( "Received by RedisMessageListener: " + message.toString() ); } }
@Autowired private RedisTemplate< String, Object > template; public void publish( final String message ) { template.execute( new RedisCallback< Long >() { @SuppressWarnings( "unchecked" ) @Override public Long doInRedis( RedisConnection connection ) throws DataAccessException { return connection.publish( ( ( RedisSerializer< String > )template.getKeySerializer() ).serialize( "queue" ), ( ( RedisSerializer< Object > )template.getValueSerializer() ).serialize( message ) ); } } ); }
That’s basically it for very quick introduction but definitely enough to fall in love with Redis.
Reference: Using Redis with Spring from our JCG partner Andrey Redko at the Andriy Redko {devmind} blog.
Hi Andrey,
Thanks for this post. could you help me to have a sample code for key expiration notification using spring data redis. I tried to look in to http://redis.io/topics/notifications, but it didn’t help me out. Your RedisMessageListener looks closer to what I was looking for. but I could not co-relate it with expire.
Thanks
Amit
Hi Amit,
Thank you for your comment. Right, key expiration notification became available in Redis 2.8.0, way after this blog post has been published. Essentially, I think once you run Redis with notifications enabled, you can subscribe on __keyspace@* or __keyevent@* topics to get notified about expired keys. Is it what you have tried and it didn’t work out?
Thank you.
Best Regards,
Andriy Redko
Hi Andriy, Thanks for reply. No I couldn’t get that how to use it in my code. I am trying to write a service which stores all the social count for my site in redis for next two minutes and serves the result from redis as per request. But some how when my keys expire after 2 minutes, and my service doesn’t have way to know, if key is already expired and it should insert the new results. That is why I was planning to implement notification on expire. I read in redis documentation , if you want redis DB… Read more »
Hi Amit, Thanks a lot for providing more details. It is actually very easy to enable Redis key expiration notifications in Redis using configuration or just running this command in Redis CLI: CONFIG SET notify-keyspace-events KEx On the Spring/Java side, you just create pattern topic and subscribe either to __keyspace or __keyevent, whatever is easier to you, for example: @Bean RedisMessageListenerContainer redisNotificationsContainer() { final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory( jedisConnectionFactory() ); container.addMessageListener( messageListener(), new PatternTopic( “__keyspace@*” ) ); return container; } With that, you should be able to consume key expiration events from Redis (you also can limit it… Read more »
Hi Andriy! I’m still very new to Spring Redis. I have the same issue as Amir. However, I don’t know how to configure redis message listener. On what file should I place the configuration for “notify-keyspace-events”? And where should I place the creation of pattern topics? I tried using RedisMessageListenerContainer without manually configuring it (I assumed that spring redis has already configured that. Just like it’s automatic configuration for redis itself). However, I got an error “Field redisMessageListenerContainer in com.sprobe.iaoy.pdf.file.redis.RedisUploadService required a bean of type ‘org.springframework.data.redis.listener.RedisMessageListenerContainer’ that could not be found.”. Problem is I don’t know where to place the… Read more »
Hey, Thank you for the great example. Used it successfully. But I have trouble writing unit tests for it.
Hey!
Thanks a lot for your comment. That’s right, usually Redis integration is mocked in the unit test cases.
I think Mockito is going to help you a lot there.
Thanks!
Best Regards,
Andriy Redko
Thanks.
Hi Kesh, Thanks a lot for your comment. You can place the configuration for “notify-keyspace-events” in any file which is annotated with Spring @Configuration annotation, f.e. @Configuration @Import( value = ApplicationConfiguration.class ) public class PubsubConfiguration { @Bean RedisMessageListenerContainer redisNotificationsContainer() { final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory( jedisConnectionFactory() ); container.addMessageListener( messageListener(), new PatternTopic( “__keyspace@*” ) ); return container; } @Bean public MessageListener listener() { return new RedisMessageListener(); } } If you are using Spring Boot, the Redis configuration could be discovered either from application.yml or from default settings. The MessageListener instance is the one which you need to implement, you… Read more »
Hi Andrey, Can i get the source code for this?
Hi Rakesh,
Yes, sure, https://github.com/reta/spring-examples
Thank you!
Best Regards,
Andriy Redko