Skip to content

Juungmini0601/jungmini-redis-messaging

Repository files navigation

Simple Redis Messaging Spring Starter

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.

Features

  • Simple annotation-based Redis message listeners
  • Automatic JSON deserialization of messages
  • Support for both channel and pattern subscriptions
  • Spring Boot auto-configuration

Installation

Maven

<dependency>
    <groupId>io.github.juungmini0601</groupId>
    <artifactId>redis-messaging</artifactId>
    <version>1.0.1</version>
</dependency>

Gradle

implementation 'io.github.juungmini0601:redis-messaging:1.0.1'

Prerequisites

This starter requires

  • Spring Boot 3.5
  • Spring Data Redis
  • Jackson for JSON serialization/deserialization

Usage

  1. Add the dependency to your project
  2. Configure Redis connection in your application.properties or application.yml
  3. Create a RedisMessageListenerContainer bean in your configuration
  4. Use the @RedisListener annotation on your methods

Example Configuration

@Configuration
public class RedisConfig {
    
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(
            RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}

Example Usage

@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);
    }
}

Method Signatures

The @RedisListener annotation supports the following method signatures:

  1. No parameters:

    @RedisListener(channels = "channel")
    public void handleMessage() {
        // Just notified that a message was received
    }
  2. 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
    }
  3. 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
    }

License

Apache License 2.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages