Skip to content

Documentation inaccuracy for Redis Pub/Sub section #2524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sergey-morenets opened this issue Mar 5, 2023 · 3 comments
Closed

Documentation inaccuracy for Redis Pub/Sub section #2524

sergey-morenets opened this issue Mar 5, 2023 · 3 comments
Assignees
Labels
type: documentation A documentation update

Comments

@sergey-morenets
Copy link

Hi

Here's the code from the reference documentation to attach custom message handler:

  @Bean
  RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory, DefaultMessageDelegate listener) {

    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(new MessageListenerAdapter(listener, "handleMessage"), ChannelTopic.of("chatroom"));
    return container;
  }

However it would fail with the error message:


java.lang.NullPointerException: Cannot invoke "org.springframework.data.redis.listener.adapter.MessageListenerAdapter$MethodInvoker.getMethodName()" because "this.invoker" is null
	at org.springframework.data.redis.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:307)
	at org.springframework.data.redis.listener.RedisMessageListenerContainer.processMessage(RedisMessageListenerContainer.java:845)
	at org.springframework.data.redis.listener.RedisMessageListenerContainer.lambda$dispatchMessage$7(RedisMessageListenerContainer.java:993)
	at java.base/java.lang.Thread.run(Thread.java:1589)

The issue that this code doesn't invoke afterPropertiesSet as clearly mentioned in JavaDocs of MessageListenerAdapter:

Make sure to call {@link #afterPropertiesSet()} after setting all the parameters on the adapter.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 5, 2023
@mp911de
Copy link
Member

mp911de commented Mar 6, 2023

afterPropertiesSet is a lifecycle callback method invoked by the bean container. If you define RedisMessageListenerContainer in a @Bean method, then Spring Framework is going to call that method for you.

So in that regard, our documentation is accurate. If you want to spin up beans on your own without using the Spring bean container, then you need to take care of all lifecycle callbacks (afterPropertiesSet, destroy, start, stop and a few more).

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Mar 6, 2023
@sergey-morenets
Copy link
Author

Hi @mp911de

Thank you for the quick response.
I talked about this piece of code from your documentation:

@Bean
  RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory, DefaultMessageDelegate listener) {

    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(new MessageListenerAdapter(listener, "handleMessage"), ChannelTopic.of("chatroom"));
    return container;
  }

so this code just doesn't work because afterPropertiesSet is not invoked. I had to deep dive into source code to understand why. So I suggest to change this piece of code and add invocation of afterPropertiesSet().

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 6, 2023
@mp911de
Copy link
Member

mp911de commented Mar 6, 2023

Apologies, I mixed up RedisMessageListenerContainer and MessageListenerAdapter. After realizing what we're talking about, you're totally right, that we rather should declare MessageListenerAdapter as bean instead of requiring afterPropertiesSet. Also, calling the constructor with both required arguments (delegate, defaultListenerMethod), we are able to create a method invoker instance.

@mp911de mp911de added type: documentation A documentation update type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided type: bug A general bug labels Mar 6, 2023
@mp911de mp911de self-assigned this Mar 6, 2023
@mp911de mp911de added this to the 3.0.4 (2022.0.4) milestone Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants