Skip to content

Proposal: Type-safe Pub/Sub message listener API #3258

@vinsguru

Description

@vinsguru

Problem

MessageListenerAdapter relies on reflection (methodName, Object arguments) and lacks compile-time type safety. For modern Java (records, generics), this design seems to be outdated and error-prone. 😬

Proposed API

Introduce a type-safe, generic alternative for message handling:

public interface MessageHandler<T> {
    void onMessage(T message, String channel);
}

Example Usage:

public record NewsEvent(String title, String content) {
}

var listener = JsonMessageListener.of(
        NewsEvent.class,
        (event, channel) -> log.info("Received {} from {}", event, channel)
);

messageListenerContainer.addMessageListener(listener, ChannelTopic.of("news-channel"));

The existing MessageListener and MessageListenerAdapter APIs are low-level and reflection-based. They expose raw byte payloads and rely on method name resolution for dispatching, which feels outdated in modern Java applications.

Most Redis Pub/Sub use cases today exchange structured JSON data, yet developers must manually deserialize messages or use untyped adapters. A type-safe alternative such as MessageHandler<T> would provide a clearer, compile-time-safe contract for message consumption while remaining fully compatible with RedisMessageListenerContainer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions