Announcement Announcement Module
Collapse
No announcement yet.
Is it ok to have multiple listeners in one container? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it ok to have multiple listeners in one container?

    One MessageListenerContainer (extending AbstractMessageListenerContainer at least) can have only one message listener. However, in the general case, clients want to listen to many different queues with different listeners.

    Are there any drawbacks to having that one listener delegate to multiple other (custom) listeners depending on the type of the message received / on the queue. For example:

    Code:
         public void onMessage(Message message, Channel channel) throws Exception {
     		Object messageObject = extractMessage(message);
    		// Get all listeners that are declared to handle the message
    		Collection<RabbitMessageListener<?>> currentListeners = listeners.get(messageObject.getClass());
    		for (RabbitMessageListener listener : currentListeners) {
    			try {
    				listener.onMessage(messageObject);
    			} catch (Exception e) {
    				throw new ListenerExecutionFailedException("Failed to execute listener "
    						+ listener.getClass().getName(), e);
    			}
    		}
    	}

  • #2
    I don't see anything wrong with that code particularly. It should work OK for simple requirements. Maybe a more declarative approach with Spring Integration exposing the EI patterns would be easier to understand and maintain if the logic, in particular the listeners.get() method that you sketch, got more complicated.

    Comment

    Working...
    X