A lightweight annotation-based Redis Pub/Sub library for simplified subscription handling.
Ensure you have added the Redis Extensions dependency to your pom.xml
(Maven) or build.gradle
(Gradle).
Maven:
<!-- pom.xml -->
<dependency>
<groupId>io.github.jihongkim98</groupId>
<artifactId>redis-extensions</artifactId>
<version>0.0.1</version>
</dependency>
Gradle:
// build.gradle
dependencies {
implementation 'io.github.jihongkim98:redis-extensions:0.0.1'
}
See maven.org
To use Redis Extensions in your Spring project, follow these steps:
You must register a RedisMessageListenerContainer
as a Spring Bean in your configuration class.
This is required to enable Redis Pub/Sub message processing.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@Configuration
public class RedisConfig {
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
// ...
}
Use the @EnableRedisBroadcast
annotation in your configuration class to activate the Redis Extensions library.
import io.github.jihongkim98.redisextensions.EnableRedisBroadcast;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableRedisBroadcast
public class RedisExtensionConfig {
}
After completing the configuration,
you can define methods annotated with @RedisBroadcastListener
in your beans to handle Redis Pub/Sub messages.
import io.github.jihongkim98.redisextensions.annotation.RedisBroadcastListener;
import org.springframework.stereotype.Service;
@Service
public class SomeHandler {
@RedisBroadcastListener(channels = "channel:example")
public void listener(String message) {
System.out.println("Received message: " + message);
}
}
The above example demonstrates how you can use the @RedisBroadcastListener
annotation to listen to messages published to a specific Redis channel (channel:example
).
Below is an example of how to publish messages using Redis Pub/Sub to test the SomeHandler
class
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class SomeBroadcastHandler {
private final StringRedisTemplate redisTemplate;
public void execute() {
redisTemplate.convertAndSend(
"channel:example", // channel name
"Hello World!" // message to be sent
);
}
}
To test the functionality:
- Execute the
execute()
method from theSomeBroadcastHandler
class. This method sends a message ("Hello World!"
) to the Redis channel named"channel:example"
. - If everything is configured correctly, the
listener
method in theSomeHandler
class will be triggered automatically. - You should see the following output in the console:
This confirms that the message was successfully published and received through the Redis Pub/Sub system.
Received message: Hello World!