Package io.mats3.api.intercept
Interface MatsInitiateInterceptor
- All Known Subinterfaces:
MatsInitiateInterceptor.MatsInitiateInterceptOutgoingMessages
- All Known Implementing Classes:
LocalStatsMatsInterceptor
,MatsMetricsLoggingInterceptor
,MatsMicrometerInterceptor
public interface MatsInitiateInterceptor
Implement this interface to intercept Initiations, then register with
MatsInterceptable.addInitiationInterceptor(MatsInitiateInterceptor)
.
Meant for intercepting initiations with ability to modify the initiation, and to implement extra logging and metrics
gathering.
When more than one interceptor is installed, the ordering becomes interesting. If the following class is added twice,
first with 1 as the argument, and then secondly with 2 as the argument ..:
private static class MyMatsInitiateInterceptor implements MatsInitiateInterceptor { private final int _number; MyMatsInitiateInterceptor(int number) { _number = number; } @Override public void initiateStarted(InitiateStartedContext initiateStartedContext) { log.info("Started #" + _number); } @Override public void initiateIntercept(InitiateInterceptContext initiateInterceptContext, InitiateLambda initiateLambda, MatsInitiate matsInitiate) { log.info("Intercept pre #" + _number); // Wrap the MatsInitiate to catch "send(dto)", before invoking the lambda MatsInitiateWrapper wrappedMatsInitiate = new MatsInitiateWrapper(matsInitiate) { @Override public MessageReference send(Object messageDto) { log.info(".. send pre #" + _number); MessageReference send = super.send(messageDto); log.info(".. send post #" + _number); return send; } }; // Invoke the lambda, with the wrapped MatsInitiate initiateLambda.initiate(wrappedMatsInitiate); log.info("Intercept post #" + _number); } @Override public void initiateCompleted(InitiateCompletedContext initiateCompletedContext) { log.info("Completed #" + _number); } }.. then the following sequence of log lines and operations ensues:
- First the
initiateStarted(..)
are invoked in sequence: Started #1
Started #2
- Then the initiation goes into the transaction, and a "reverse stack" of lambdas are generated from the two
interceptors'
initiateIntercept(..)
methods, and then the resulting lambda is invoked: Intercept pre #1
Intercept pre #2
- .. the actual user provided lambda (the one provided by the endpoint) is invoked, performing a
send(..)
, which goes through the stack of MatsInitiateWrappers which theinitiateIntercept(..)
invocations generated (thus hitting the last wrapping by #2 first): .. send pre #2
.. send pre #1
- .. the actual Mats API send(..) method is invoked, before we traverse out of the MatsInitiateWrappers:
.. send post #1
.. send post #2
- .. and then the user lambda exits, traversing back up the
initiateIntercept(..)
stack Intercept post #2
Intercept post #1
- Finally, the {link
initiateComplete(..)
is invoked, in explicit reversed order: Completed #2
Completed #1
MatsInitiator
gotten with MatsFactory.getDefaultInitiator()
or
MatsFactory.getOrCreateInitiator(String)
- and notice the special semantics of getDefaultInitiator(),
whereby if such an seemingly "from the outside" initiation is invoked when code-flow-wise within a Stage, you will
actually be "elevated" to be initiated "within Mats" using the Stage initiator - and hence this interceptor will not
be invoked (it is no longer "from outside of Mats").
The concept of "outside" vs. "inside" perhaps seems subtle, but there is a distinct difference: No processing will
ever happen in a Mats fabric if no initiations happens "from the outside": It is always a "from the outside"
initiation that will set Mats flows in action. Such a process flow might then set several new Mats flows in action
(i.e. initiations "from the inside"), but those are dependent on the initial Mats flow that was set in motion "from
the outside", and would never have been initiated was it not for such initiation.
To catch initiations "from the inside", you will employ a MatsStageInterceptor
.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
static interface
static interface
static interface
static interface
static interface
While still within the initiation context, this interception enables modifying outgoing messages from the user lambda, setting trace properties, adding "sideloads", deleting a message, or initiating additional messages.static interface
Enables the intercepting of the invocation of the user lambda in an Initiation, with ability to wrap theMatsInitiator.MatsInitiate
(and thus modify any request, send or publishes) - or even take over the entire initiation. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
default void
Invoked right before user lambda is invoked.
-
Method Details
-
initiateStarted
Invoked right before user lambda is invoked. -
initiateCompleted
-