From 66eb5d7270fc9d7eac0a13a0a76c6612bea2d713 Mon Sep 17 00:00:00 2001 From: sameira Date: Thu, 18 Feb 2016 22:48:42 +0530 Subject: [PATCH] Added publish subscribe example using redis --- spring-data-redis/pom.xml | 124 +++++++++--------- .../spring/data/redis/config/RedisConfig.java | 31 ++++- .../data/redis/queue/MessagePublisher.java | 7 + .../redis/queue/RedisMessagePublisher.java | 28 ++++ .../redis/queue/RedisMessageSubscriber.java | 19 +++ .../data/redis/repo/StudentRepository.java | 4 +- .../data/redis/RedisMessageListenerTest.java | 51 +++++++ 7 files changed, 198 insertions(+), 66 deletions(-) create mode 100644 spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/MessagePublisher.java create mode 100644 spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessagePublisher.java create mode 100644 spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java create mode 100644 spring-data-redis/src/test/java/org/baeldung/spring/data/redis/RedisMessageListenerTest.java diff --git a/spring-data-redis/pom.xml b/spring-data-redis/pom.xml index 98da69934c..3f9eb705f4 100644 --- a/spring-data-redis/pom.xml +++ b/spring-data-redis/pom.xml @@ -1,76 +1,76 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.baeldung - sprint-data-redis - 0.0.1-SNAPSHOT - jar + org.baeldung + sprint-data-redis + 0.0.1-SNAPSHOT + jar - - UTF-8 - 4.2.2.RELEASE - 1.6.2.RELEASE - 0.8.0 - + + UTF-8 + 4.2.2.RELEASE + 1.6.2.RELEASE + 0.8.0 + - - - org.springframework.data - spring-data-redis - ${spring-data-redis} - + + + org.springframework.data + spring-data-redis + ${spring-data-redis} + - - cglib - cglib-nodep - 2.2 - + + cglib + cglib-nodep + 2.2 + - - log4j - log4j - 1.2.16 - + + log4j + log4j + 1.2.16 + - - redis.clients - jedis - 2.5.1 - jar - + + redis.clients + jedis + 2.5.1 + jar + - - org.springframework - spring-core - ${spring.version} - + + org.springframework + spring-core + ${spring.version} + - - org.springframework - spring-context - ${spring.version} - + + org.springframework + spring-context + ${spring.version} + - - junit - junit - 4.12 - test - + + junit + junit + 4.12 + test + - - org.springframework - spring-test - ${spring.version} - test - + + org.springframework + spring-test + ${spring.version} + test + - - com.lordofthejars - nosqlunit-redis - ${nosqlunit.version} - + + com.lordofthejars + nosqlunit-redis + ${nosqlunit.version} + - + diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java index a7e75a438a..0b64afe56c 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java @@ -1,10 +1,16 @@ package org.baeldung.spring.data.redis.config; +import org.baeldung.spring.data.redis.queue.RedisMessageSubscriber; +import org.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import org.baeldung.spring.data.redis.queue.MessagePublisher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; 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.listener.ChannelTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; +import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @Configuration @ComponentScan("org.baeldung.spring.data.redis") @@ -17,8 +23,31 @@ public class RedisConfig { @Bean public RedisTemplate redisTemplate() { - final RedisTemplate< String, Object> template = new RedisTemplate(); + final RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(jedisConnectionFactory()); return template; } + + @Bean + MessageListenerAdapter messageListener() { + return new MessageListenerAdapter(new RedisMessageSubscriber()); + } + + @Bean + RedisMessageListenerContainer redisContainer() { + final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(jedisConnectionFactory()); + container.addMessageListener(messageListener(), topic()); + return container; + } + + @Bean + MessagePublisher redisPublisher() { + return new RedisMessagePublisher(redisTemplate(), topic()); + } + + @Bean + ChannelTopic topic() { + return new ChannelTopic("pubsub:queue"); + } } diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/MessagePublisher.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/MessagePublisher.java new file mode 100644 index 0000000000..a05f524f60 --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/MessagePublisher.java @@ -0,0 +1,7 @@ +package org.baeldung.spring.data.redis.queue; + + +public interface MessagePublisher { + + void publish(String message); +} diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessagePublisher.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessagePublisher.java new file mode 100644 index 0000000000..4eb7f69cdb --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessagePublisher.java @@ -0,0 +1,28 @@ +package org.baeldung.spring.data.redis.queue; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.stereotype.Service; + +@Service +public class RedisMessagePublisher implements MessagePublisher { + + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private ChannelTopic topic; + + public RedisMessagePublisher() { + } + + public RedisMessagePublisher(RedisTemplate redisTemplate, + ChannelTopic topic) { + this.redisTemplate = redisTemplate; + this.topic = topic; + } + + public void publish(String message) { + redisTemplate.convertAndSend(topic.getTopic(), message); + } +} diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java new file mode 100644 index 0000000000..4bc60849fb --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java @@ -0,0 +1,19 @@ +package org.baeldung.spring.data.redis.queue; + +import org.springframework.data.redis.connection.Message; +import org.springframework.data.redis.connection.MessageListener; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class RedisMessageSubscriber implements MessageListener { + + public static List messageList = new ArrayList(); + + public void onMessage(final Message message, final byte[] pattern) { + messageList.add(message.toString()); + System.out.println("Message received: " + message.toString()); + } +} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java index 9e5502f8e0..2a1f6afcce 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java @@ -1,15 +1,13 @@ package org.baeldung.spring.data.redis.repo; import org.baeldung.spring.data.redis.model.Student; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Component; import java.util.Map; public interface StudentRepository { void saveStudent(Student person); - + void updateStudent(Student student); Student findStudent(String id); diff --git a/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/RedisMessageListenerTest.java b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/RedisMessageListenerTest.java new file mode 100644 index 0000000000..7308424a90 --- /dev/null +++ b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/RedisMessageListenerTest.java @@ -0,0 +1,51 @@ +package org.baeldung.spring.data.redis; + +import org.baeldung.spring.data.redis.config.RedisConfig; +import org.baeldung.spring.data.redis.queue.RedisMessageSubscriber; +import org.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.UUID; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = RedisConfig.class) +public class RedisMessageListenerTest { + + + @Autowired + private RedisMessagePublisher redisMessagePublisher; + + @Test + public void testOnMessage() throws Exception { + String message = "Message " + UUID.randomUUID(); + redisMessagePublisher.publish(message); + Thread.sleep(100); + assertTrue(RedisMessageSubscriber.messageList.get(0).contains(message)); + } + + public void testOnPMessage() throws Exception { + + } + + public void testOnSubscribe() throws Exception { + + } + + public void testOnUnsubscribe() throws Exception { + + } + + public void testOnPUnsubscribe() throws Exception { + + } + + public void testOnPSubscribe() throws Exception { + + } +} \ No newline at end of file