-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Intercept JMS messages #22999
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
Comments
Hi!
I have exactly the same use case, and I am trying different solutions. Thanks! |
Here's basically what I use for the listening part. For the JmsTemplate part, we plan to use inheritance, but we currently wrap the JmsTemplate into our own class for now, so I don't have any example.
|
I think you can use |
@danieljohngomez I have tried this approach but I have not been able to get it working :_( @jnizet thanks!!!! For the listening part I think I will use aspect approach. For the JmsTemplate part, how are you wrapping the jmsTemplate? I would like to override this method
modifying the message with my properties. Thanks! |
@destebanm I just send all the messages using my own bean which itself delegates to JmsTemplate to send the message and set the appropriate message properties. |
@destebanm Check out AbstractMessageBrokerConfiguration.brokerChannel() for a reference. |
Thanks @danieljohngomez, I will take a look! |
Did you get it to work? Can't find any place where to plug in the ChannelInterceptor |
@bound2 In my case, I have overridden these methods on org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration:
With the ChannelRegistration, you can then do interceptors(ChannelInterceptor... interceptors). If you want to do it on the broker channel, override AbstractMessageBrokerConfiguration.brokerChannel() then do super.brokerChannel().addInterceptor(); |
Hi there! First, if you are still interested by this feature, can you add a reaction to this comment and maybe explain the use case you're trying to implement in your application? We are asking this because this issue predates the Observability support in JMS. If your use case is now covered by the Observability support, we might not need this after all. If we get enough valid use cases for this, we can consider the following implementation with this contract: /**
* Intercept a {@code Message} during a JMS operation:
* <ul>
* <li>for send operations, interceptors can mutate the message before it is sent, or ignore it
* and prevent it from being sent altogether.
* <li>for receive operations, interceptors can mutate the message before it is consumed
* by the application, or ignore it and prevent its processing altogether.
* </ul>
*
* @author Brian Clozel
* @since 6.2.0
*/
@FunctionalInterface
public interface MessageInterceptor {
/**
* Intercept the given message during a JMS operation.
* @param destination the JMS destination where the message is going to be sent, or where it was received from
* @param message the message being intercepted
* @return {@code true} if the message should be further processed, or {@code false} if it should be dropped
* @throws JMSException throws by {@link Message} methods
*/
boolean intercept(Destination destination, Message message) throws JMSException;
} You would be able to configure "send interceptors" and "receive interceptors" on If we're getting enough feedback on this, we can consider it for 6.2.0-M1 and get this prototype into your hands so you can give it a try. Thanks! |
Although I'm not an author of the issue - I can add an usecase. I needed some interceptor in order to get some data from headers/properties and construct Security Context, because my legacy code heavy relied on Spring Security. From my prospective your interface is good for it, (having in mind that Message has some getters for headers/properties) |
@nkonev Thanks for your feedback. Indeed, |
Sorry, I don't have access to the source code of the application where I had this usecase anymore, so I'm sorry I won't be able to tell if that would completely suit the needs I had back then. But I fully trust your judgement. |
We would really appreciate these interceptors for JMS. We would like to use them for filling and extracting security context as was already mentioned. So it would be nice to catch the connection exception and retry a submission again in interceptors. |
Delaying this until #32501 is implemented. |
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 text was updated successfully, but these errors were encountered: