Hi,
I am playing around with the pub-sub implementation of Spring Data Redis (version 1.0.1) and I cannot pass the channel correctly to my message listener. My problem is a bit similar than the one mentioned in this Jira issue: https://jira.springsource.org/browse/DATAREDIS-77.
The source code of my message listener is following:
The configuration of my message listener adapter and the message listener container is following:Code:public class ContactMessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(ContactMessageListener.class); public void handleMessage(Contact contact, String channel) { LOGGER.debug(channel + ":" + contact.toString()); } }
Then I send message to a channel that notifies about new contacts:Code:@Bean public MessageListenerAdapter messageListenerAdapter() { MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new ContactMessageListener()); messageListenerAdapter.setSerializer(contactSerializer()); return messageListenerAdapter; } @Bean public RedisMessageListenerContainer redisMessageListenerContainer() { RedisMessageListenerContainer listeners = new RedisMessageListenerContainer(); listeners.setConnectionFactory(redisConnectionFactory()); listeners.addMessageListener(messageListenerAdapter(), Arrays.asList( new ChannelTopic(RedisContactService.CHANNEL_NEW_CONTACTS), new ChannelTopic(RedisContactService.CHANNEL_UPDATED_CONTACTS), new ChannelTopic(RedisContactService.CHANNEL_REMOVED_CONTACTS) )); return listeners; }
However, in my log I get following message:Code:@Override public Contact add(Contact added) { LOGGER.debug("Adding contact with information: " + added); //Persist contact etc... redisTemplate.convertAndSend(CHANNEL_NEW_CONTACTS, added); return added; }
I ran this example on debugger and found out that the onMessage() method of MessageListenerAdapter class does not use channel information stored in a Message class. Instead it passes on the pattern that is always null. The source code of the onMessage() method is following (I added some questions in the comments by using CAPS):Code:DEBUG - ContactMessageListener - null:example.model.Contact@1eac9126[id=5,address=example.model.Address@536d6585[country=,streetAddress=,postCode=,postOffice=,state=],emailAddress=,firstName=Foo,lastName=Bar,phoneNumber=]
Am I missing something or should I just give up and implement the MessageListener interface?Code:public void onMessage(Message message, byte[] pattern) { try { // Check whether the delegate is a MessageListener impl itself. // In that case, the adapter will simply act as a pass-through. //THIS WORKS if (delegate != this) { if (delegate instanceof MessageListener) { ((MessageListener) delegate).onMessage(message, pattern); return; } } // Regular case: find a handler method reflectively. Object convertedMessage = extractMessage(message); //SHOULD THIS BE: String convertedChannel = stringSerializer.deSerialize(message.getChannel());? String convertedChannel = stringSerializer.deserialize(pattern); // Invoke the handler method with appropriate arguments. Object[] listenerArguments = new Object[] { convertedMessage, convertedChannel }; invokeListenerMethod(invoker.getMethodName(), listenerArguments); } catch (Throwable th) { handleListenerException(th); } }


Reply With Quote
