MrJazsohanisharma

Redis Message Listener in Spring Boot

Blog ads

Creating a Redis Message Listener in Spring Boot with RedisMessageListenerContainer

Real-time messaging is essential for modern distributed systems. Redis Pub/Sub, combined with Spring Boot, offers a simple and efficient way to implement message listeners that react instantly to published events. In this guide, you'll learn how to create a message listener for Redis Pub/Sub using Spring's RedisMessageListenerContainer and MessageListenerAdapter.

Why Use Redis Message Listeners?

  • Real-time Event Handling: Instantly react to published messages across microservices or components.
  • Loose Coupling: Decouple publishers and subscribers for scalable architectures.
  • Simplicity: Spring Data Redis makes listener registration and message handling straightforward.

Step-by-Step Example: Redis Pub/Sub Message Listener

1. Add Dependencies

Add the following to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. Configure Redis Connection

In application.properties:

spring.redis.host=localhost
spring.redis.port=6379

3. Create the Message Receiver

Define a simple POJO to handle incoming messages:

public class Receiver {
    public void receiveMessage(String message) {
        System.out.println("Received &lt;" + message + "&gt;");
    }
}

4. Register the Listener with the Listener Container

Configure the message listener container and adapter in your configuration class:

@Configuration
public class RedisConfig {

    @Bean
    public Receiver receiver() {
        return new Receiver();
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(Receiver receiver) {
        // "receiveMessage" is the method to invoke when a message arrives
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    public RedisMessageListenerContainer container(
            RedisConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
        return container;
    }
}

5. Publish Messages Using RedisTemplate

@RestController
public class MessagePublisher {

    @Autowired
    private StringRedisTemplate template;

    @GetMapping("/publish")
    public String publish(@RequestParam String message) {
        template.convertAndSend("chat", message);
        return "Message published: " + message;
    }
}

How It Works

  • The Receiver class defines a method to process incoming messages.
  • MessageListenerAdapter wraps the receiver and maps Redis messages to the receiveMessage method.
  • RedisMessageListenerContainer subscribes to the chat topic and dispatches messages to the adapter.
  • When you access /publish?message=Hello+Redis, the message is sent to the chat topic and instantly received by the listener.

Conclusion

Spring Boot and Redis Pub/Sub make it easy to implement real-time message listeners. By registering a POJO with RedisMessageListenerContainer, you can react to events as soon as they happen, enabling powerful event-driven architectures in your applications.

References:
- Spring.io Guide: Messaging with Redis
- Baeldung: Pub/Sub Messaging with Spring Data Redis

ads

Previous Post Next Post