A Spring Boot starter for Redis messaging with annotation-based listeners.
This library was developed by a beginner developer.
Use with caution in production environments.
✅ Tested with Spring Boot 3.5 and JDK 21.
- Simple annotation-based Redis message listeners
- Automatic JSON deserialization of messages
- Support for both channel and pattern subscriptions
- Spring Boot auto-configuration
<dependency>
<groupId>io.github.juungmini0601</groupId>
<artifactId>redis-messaging</artifactId>
<version>1.0.1</version>
</dependency>
implementation 'io.github.juungmini0601:redis-messaging:1.0.1'
This starter requires
- Spring Boot 3.5
- Spring Data Redis
- Jackson for JSON serialization/deserialization
- Add the dependency to your project
- Configure Redis connection in your application.properties or application.yml
- Create a RedisMessageListenerContainer bean in your configuration
- Use the @RedisListener annotation on your methods
@Configuration
public class RedisConfig {
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
@Service
public class NotificationService {
private static final Logger log = LoggerFactory.getLogger(NotificationService.class);
// Listen to a specific channel
@RedisListener(channels = "notifications")
public void handleNotification(String message) {
log.info("Received notification: {}", message);
}
// Listen to a specific channel with automatic JSON deserialization
@RedisListener(channels = "user-events")
public void handleUserEvent(UserEvent event) {
log.info("Received user event: {} for user {}", event.getType(), event.getUserId());
}
// Listen to multiple channels
@RedisListener(channels = {"alerts", "warnings"})
public void handleAlerts(String message, String channel) {
log.info("Received {} on channel {}", message, channel);
}
// Listen to patterns
@RedisListener(patterns = "user:*:notifications")
public void handleUserNotifications(String message, String channel) {
log.info("Received notification on channel {}: {}", channel, message);
}
}
The @RedisListener annotation supports the following method signatures:
-
No parameters:
@RedisListener(channels = "channel") public void handleMessage() { // Just notified that a message was received }
-
Single parameter (message content):
@RedisListener(channels = "channel") public void handleMessage(String message) { // Receive message as String } @RedisListener(channels = "channel") public void handleMessage(UserEvent event) { // Automatic JSON deserialization to UserEvent }
-
Two parameters (message content and channel):
@RedisListener(channels = "channel") public void handleMessage(String message, String channel) { // Receive message as String and the channel name } @RedisListener(channels = "channel") public void handleMessage(UserEvent event, String channel) { // Automatic JSON deserialization to UserEvent and the channel name }
Apache License 2.0