MessageListenerAdapter is a Spring Data Redis class that lets any plain Java object receive Redis Pub/Sub messages. You wrap your POJO and the name of one of its methods in the adapter, register the adapter with a RedisMessageListenerContainer, and the container calls that method for every message on the subscribed channels. Your code never touches the low-level Message or byte[] types.
The class is org.springframework.data.redis.listener.adapter.MessageListenerAdapter. It is the Redis counterpart of the JMS and AMQP adapters, and it is the approach shown in the official Spring Data Redis messaging guide.
Three pieces cooperate:
| Component | Role |
|---|---|
RedisMessageListenerContainer | Holds the connection, subscribes to topics and dispatches messages on a task executor |
MessageListenerAdapter | Converts the raw message and calls a method on a delegate object |
| Your delegate (POJO) | Contains the business logic, with no Redis types |
Redis Pub/Sub is fire-and-forget. A message goes only to subscribers connected at that moment and is not stored. If your application is down, the message is lost. Use Redis Streams when you need durability, replay or consumer groups.
The delegate is an ordinary Spring bean. The adapter calls handleMessage by default, but you can name any method:
The adapter supports a method taking the message body, or the body plus the channel name as a second String. There is no @MessageListener annotation for this; the method is chosen when you create the adapter.
Spring Boot auto-configures the RedisConnectionFactory and a StringRedisTemplate, but not the listener container, so you declare it:
ChannelTopic matches one channel exactly, and PatternTopic matches a glob pattern such as alerts.*. Declaring the adapter as a @Bean makes Spring call its initialization, which sets up the default serializers.
Publish with StringRedisTemplate:
You can test it without code from redis-cli:
The application log then shows:
By default the adapter decodes the payload with StringRedisSerializer, so the method argument is a UTF-8 String. To receive typed objects, send JSON and convert it yourself, or set a different serializer with adapter.setSerializer(...), for example GenericJackson2JsonRedisSerializer.
The container runs listeners on a TaskExecutor. A slow handler delays other messages on the same executor, so keep handlers short and hand heavy work to another executor. Handlers can run concurrently across messages, so they must be thread-safe. If a handler throws, the container logs the error and continues; there is no retry.
Also note that redis.convertAndSend and the subscribing application can use different serializers. When subscribers receive garbled text, check that both sides use the same one.
If you need the raw message and pattern, skip the adapter and implement org.springframework.data.redis.connection.MessageListener:
The adapter is simply a convenience that does this decoding and the method lookup for you.
handleMessage. Pass a method name to the constructor, as in new MessageListenerAdapter(receiver, "receiveMessage"), or call setDefaultListenerMethod.
No. Messages are delivered only to currently connected subscribers and are not persisted. Use Redis Streams if you cannot afford to lose messages.
Check that the RedisMessageListenerContainer bean exists, that the topic name matches the channel exactly, and that publisher and subscriber use the same Redis database and server. Pub/Sub is not scoped by database number, but they must reach the same instance.
Yes. Publish JSON and either deserialize it in the receiver or configure a JSON serializer on the adapter.
MessageListenerAdapter connects Redis Pub/Sub to a plain method on a Spring bean: choose the delegate and method, register the adapter on a RedisMessageListenerContainer with channel or pattern topics, and publish with StringRedisTemplate. Keep handlers fast and thread-safe, and remember that Pub/Sub does not store messages.