Skip to content

Intercept JMS messages #22999

Open
Open
@jnizet

Description

@jnizet

I'm working on a Spring Boot app which communicates with other apps, or with itself, using JMS. In addition to the functional message payload being exchanged, some contextual information is added to the messages, as properties. For example: the identity of the user (or process) who triggered the sending of the message.

Instead of having to add this contextual information each time I send a message, and to extract this contextual information each time I receive one, I would like to do that once, in a single place (it's then stored it in a thread-local variable, and/or stored in the slf4J MDC).

The way I currently do it is by overriding methods of JmsTemplate and of DefaultMessageListenerContainer. This, however is not as elegant as I would like it: I need to provide my own configuration to provide a custom JmsTemplate, and a custom DefaultMessageListenerContainerFactory, which itself creates a custom DefaultMessageListenerContainer, instead of simply using the ones auto-configured by Spring Boot, leading to code that is more verbose than necessary, and which duplicates what Spring Boot does already (properties-based customization, etc.)

I also thought about using AOP to intercept the calls to the JmsListener-annotated methods. But then that prevents me from simply using the type of the payload for the method argument: every method must take a Message as argument, just to allow extracting the message properties inside the aspect.

Unless there is already a better way to achieve what I want, I would find it nice if I could simply add interceptors to the JmsTemplate and to the DefaultMessageListenerContainerFactory.

  • the JmsTemplate interceptors would allow to preprocess the message before sending it
  • the DefaultMessageListenerContainerFactory interceptors would be set on each of the created DefaultMessageListenerContainers (as the other properties are), and would allow preprocessing a message before invoking the listener, and postprocessing it after the listener is invoked, in a way similar to an MVC interceptor.

Metadata

Metadata

Assignees

Labels

in: messagingIssues in messaging modules (jms, messaging)status: blockedAn issue that's blocked on an external project changetype: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions