Skip to content

Implement ContextHolderRequestHandlerAdvice #3869

@artembilan

Description

@artembilan

Sometimes we may have a context during message handling. See a DelegatingSessionFactory as an example. So, we store some key into the thread local, then SftpMessageHandler obtain the session and and performs respective logic.
All this is done in the SftpMessageHandler transparently, but in case of DelegatingSessionFactory we just chose the target (S)FTP connection according previously mentioned key stored in the thread local.

See more info in SO question and in docs.

So, current solution for the context holder is a bit involved, especially when we use one-way channel adapter for (S)FTP.

The proposed ContextHolderRequestHandlerAdvice could be a single way to inject into such an outbound endpoint.
Its logic is pretty simple:

  1. Take a key from the request message
  2. Store the value into the context holder
  3. Invoke the MessageHandler
  4. Clean the value from the context holder in the finally

I feel like this advice can have these options:

  1. keyProvider - the Function<Message<?>, Object> to obtain the key from the message, or whatever source for the key is available. Could be as simple as setKeyProvider((m) -> m.getHeaders().get("myKey"));
  2. contextSetHook - a Consumer<Object> - just delegates to the method on the target context holder. For example in case of the mentioned DelegatingSessionFactory it could be like this setContextSetHook(myDelegatingFactory::setThreadKey);
  3. contextClearHook - a Consumer<Object> - just delegates to the method on the target context holder.For example in case of the mentioned DelegatingSessionFactory it could be like this setContextClearHook((key) -> myDelegatingFactory.clearThreadKey());

Probably internal logic could be implemented as a try-with-resource.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions