Package io.mats3
Class MatsFactory.MatsFactoryWrapper
java.lang.Object
io.mats3.MatsFactory.MatsFactoryWrapper
- All Implemented Interfaces:
MatsConfig.StartStoppable,MatsFactory,MatsFactory.MatsWrapper<MatsFactory>
- Enclosing interface:
- MatsFactory
public static class MatsFactory.MatsFactoryWrapper
extends Object
implements MatsFactory.MatsWrapper<MatsFactory>, MatsFactory
A base Wrapper for
MatsFactory, which simply implements MatsFactory, takes a MatsFactory instance and
forwards all calls to that. Use this if you need to wrap the MatsFactory, where most of the methods are
pass-through to the target, as any changes to the MatsFactory interface then won't break your wrapper.
Note: The hashCode() and equals(Object) are implemented to forward to the wrappee.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.mats3.MatsFactory
MatsFactory.ContextLocal, MatsFactory.FactoryConfig, MatsFactory.MatsFactoryWrapper, MatsFactory.MatsWrapper<T> -
Field Summary
Fields inherited from interface io.mats3.MatsFactory
NO_CONFIG -
Constructor Summary
ConstructorsConstructorDescriptionNo-args constructor, which implies that you either need to invokesetWrappee(MatsFactory)before publishing the instance (making it available for other threads), or overrideunwrap()to provide the desiredMatsFactoryinstance.MatsFactoryWrapper(MatsFactory targetMatsFactory) Standard constructor, taking the wrappedMatsFactoryinstance. -
Method Summary
Modifier and TypeMethodDescriptionbooleanGets or creates the default Initiator (whose name is 'default') from which to initiate new Mats processes, i.e.Optional<MatsEndpoint<?,?>> getEndpoint(String endpointId) List<MatsEndpoint<?,?>> getOrCreateInitiator(String name) Gets or creates a new Initiator from which to initiate new Mats processes, i.e.inthashCode()voidIf this method is invoked before any endpoint is created, the endpoints will not start even thoughMatsEndpoint.finishSetup()is invoked on them, but will wait tillMatsFactory.start()is invoked on the factory. This feature should be employed in most setups where the MATS endpoints might use other services or components whose order of creation and initialization are difficult to fully control, e.g.voidsetWrappee(MatsFactory targetMatsFactory) Sets the wrappedMatsFactory, e.g.<R,I> MatsEndpoint<R, Void> single(String endpointId, Class<R> replyClass, Class<I> incomingClass, MatsEndpoint.ProcessSingleLambda<R, I> processor) Sets up aMatsEndpointthat just contains one stage, useful for simple "request the full person data for this/these personId(s)" scenarios.<R,I> MatsEndpoint<R, Void> single(String endpointId, Class<R> replyClass, Class<I> incomingClass, Consumer<? super MatsEndpoint.EndpointConfig<R, Void>> endpointConfigLambda, Consumer<? super MatsStage.StageConfig<R, Void, I>> stageConfigLambda, MatsEndpoint.ProcessSingleLambda<R, I> processor) Variation ofMatsFactory.single(String, Class, Class, ProcessSingleLambda)that can be configured "on the fly".<R,S> MatsEndpoint<R, S> Sets up aMatsEndpointon which you will add stages.<R,S> MatsEndpoint<R, S> staged(String endpointId, Class<R> replyClass, Class<S> stateClass, Consumer<? super MatsEndpoint.EndpointConfig<R, S>> endpointConfigLambda) Variation ofMatsFactory.staged(String, Class, Class)that can be configured "on the fly".voidstart()Starts all endpoints that has been created by this factory, by invokingMatsEndpoint.start()on them.booleanstop(int gracefulShutdownMillis) Stops all endpoints and initiators, by invokingMatsEndpoint.stop(int)on all the endpoints, andMatsInitiator.close()on all initiators that has been created by this factory.<S,I> MatsEndpoint<Void, S> subscriptionTerminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Special kind of terminator that, in JMS-style terms, subscribes to a topic instead of listening to a queue (i.e.<S,I> MatsEndpoint<Void, S> subscriptionTerminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, Consumer<? super MatsEndpoint.EndpointConfig<Void, S>> endpointConfigLambda, Consumer<? super MatsStage.StageConfig<Void, S, I>> stageConfigLambda, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Variation ofMatsFactory.subscriptionTerminator(String, Class, Class, ProcessTerminatorLambda)that can be configured "on the fly", but notice that the concurrency of a SubscriptionTerminator is always 1.<S,I> MatsEndpoint<Void, S> terminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Sets up aMatsEndpointthat contains a single stage that typically will be the reply-to endpointId for arequest initiation, or that can be used to directly send a "fire-and-forget" styleinvocationto.<S,I> MatsEndpoint<Void, S> terminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, Consumer<? super MatsEndpoint.EndpointConfig<Void, S>> endpointConfigLambda, Consumer<? super MatsStage.StageConfig<Void, S, I>> stageConfigLambda, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Variation ofMatsFactory.terminator(String, Class, Class, ProcessTerminatorLambda)that can be configured "on the fly".toString()unwrap()In a situation where you might be given aMatsFactory.MatsFactoryWrapper, but need the actual implementation, this method allows you to just always callmatsFactory.unwrapFully()instead of first checking whether it is a proxy and only then cast and unwrap.<I> IResolve the "deadly diamond of death", by calling toMatsWrapper.super.unwrapTo(iface);booleanwaitForReceiving(int timeoutMillis) Waits until all endpoints have fully entered the receive-loops, i.e.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.mats3.MatsFactory
close
-
Constructor Details
-
MatsFactoryWrapper
Standard constructor, taking the wrappedMatsFactoryinstance.- Parameters:
targetMatsFactory- theMatsFactoryinstance whichunwrap()will return (and hence all forwarded methods will use).
-
MatsFactoryWrapper
public MatsFactoryWrapper()No-args constructor, which implies that you either need to invokesetWrappee(MatsFactory)before publishing the instance (making it available for other threads), or overrideunwrap()to provide the desiredMatsFactoryinstance. In these cases, make sure to honor memory visibility semantics - i.e. establish a happens-before edge between the setting of the instance and any other threads getting it.
-
-
Method Details
-
setWrappee
Sets the wrappedMatsFactory, e.g. in case you instantiated it with the no-args constructor. Do note that the field holding the wrapped instance is not volatile nor synchronized. This means that if you want to set it after it has been published to other threads, you will have to override both this method andunwrap()to provide for needed memory visibility semantics, i.e. establish a happens-before edge between the setting of the instance and any other threads getting it. Avolatilefield would work nice.- Specified by:
setWrappeein interfaceMatsFactory.MatsWrapper<MatsFactory>- Parameters:
targetMatsFactory- theMatsFactorywhich is returned byunwrap(), unless that is overridden.
-
unwrap
- Specified by:
unwrapin interfaceMatsFactory.MatsWrapper<MatsFactory>- Returns:
- the wrapped
MatsFactory. All forwarding methods invokes this method to get the wrappedMatsFactory, thus if you want to get creative wrt. how and when the MatsFactory is decided, you can override this method.
-
unwrapTo
Resolve the "deadly diamond of death", by calling toMatsWrapper.super.unwrapTo(iface);- Specified by:
unwrapToin interfaceMatsFactory- Specified by:
unwrapToin interfaceMatsFactory.MatsWrapper<MatsFactory>- Returns:
- default
thisififace.isAssignableFrom(thisClass), otherwise throwsIllegalArgumentException(overridden by wrappers).
-
unwrapFully
Description copied from interface:MatsFactoryIn a situation where you might be given aMatsFactory.MatsFactoryWrapper, but need the actual implementation, this method allows you to just always callmatsFactory.unwrapFully()instead of first checking whether it is a proxy and only then cast and unwrap.- Specified by:
unwrapFullyin interfaceMatsFactory- Specified by:
unwrapFullyin interfaceMatsFactory.MatsWrapper<MatsFactory>- Returns:
- the fully unwrapped instance: If the returned instance from
MatsFactory.MatsWrapper.unwrap()is itself aMatsWrapper, it will recurse down by invoking this method (unwrapFully()) again on the returned target.
-
getFactoryConfig
- Specified by:
getFactoryConfigin interfaceMatsFactory- Returns:
- the
MatsFactory.FactoryConfigon which to configure the factory, e.g. defaults for concurrency.
-
staged
Description copied from interface:MatsFactorySets up aMatsEndpointon which you will add stages. The first stage is the one that will receive the incoming (typically request) DTO, while any subsequent stage is invoked when the service that the previous stage sent a request to, replies. Unless the state object was sent along with therequestorsend, the first stage will get a newly constructed empty state instance, while the subsequent stages will get the state instance in the form it was left in the previous stage.- Specified by:
stagedin interfaceMatsFactory- Parameters:
endpointId- the identification of thisMatsEndpoint, which are the strings that should be provided to theMatsInitiator.MatsInitiate.to(String)orMatsInitiator.MatsInitiate.replyTo(String, Object)methods for this endpoint to get the message. Typical structure is"OrderService.placeOrder"for public endpoints, or"OrderService.private.validateOrder"for private (app-internal) endpoints.replyClass- the class that this endpoint shall return.stateClass- the class of the State DTO that will be sent along the stages.- Returns:
- the
MatsEndpointon which to add stages.
-
staged
public <R,S> MatsEndpoint<R,S> staged(String endpointId, Class<R> replyClass, Class<S> stateClass, Consumer<? super MatsEndpoint.EndpointConfig<R, S>> endpointConfigLambda) Description copied from interface:MatsFactoryVariation ofMatsFactory.staged(String, Class, Class)that can be configured "on the fly".- Specified by:
stagedin interfaceMatsFactory
-
single
public <R,I> MatsEndpoint<R,Void> single(String endpointId, Class<R> replyClass, Class<I> incomingClass, MatsEndpoint.ProcessSingleLambda<R, I> processor) Description copied from interface:MatsFactorySets up aMatsEndpointthat just contains one stage, useful for simple "request the full person data for this/these personId(s)" scenarios. This sole stage is supplied directly, using a specialization of the processor lambda which does not have state (as there is only one stage, there is no other stage to pass state to), but which can return the reply by simply returning it on exit from the lambda. Do note that this is just a convenience for the often-used scenario where for example a request will just be looked up in the backing data store, and replied directly, using only one stage, not needing any multi-stage processing.- Specified by:
singlein interfaceMatsFactory- Parameters:
endpointId- the identification of thisMatsEndpoint, which are the strings that should be provided to theMatsInitiator.MatsInitiate.to(String)orMatsInitiator.MatsInitiate.replyTo(String, Object)methods for this endpoint to get the message. Typical structure is"OrderService.placeOrder"for public endpoints, or"OrderService.private.validateOrder"for private (app-internal) endpoints.replyClass- the class that this endpoint shall return.incomingClass- the class of the incoming (typically request) DTO.processor- thestagethat will be invoked to process the incoming message.- Returns:
- the
MatsEndpoint, but you should not add any stages to it, as the sole stage is already added.
-
single
public <R,I> MatsEndpoint<R,Void> single(String endpointId, Class<R> replyClass, Class<I> incomingClass, Consumer<? super MatsEndpoint.EndpointConfig<R, Void>> endpointConfigLambda, Consumer<? super MatsStage.StageConfig<R, Void, I>> stageConfigLambda, MatsEndpoint.ProcessSingleLambda<R, I> processor) Description copied from interface:MatsFactoryVariation ofMatsFactory.single(String, Class, Class, ProcessSingleLambda)that can be configured "on the fly".- Specified by:
singlein interfaceMatsFactory
-
terminator
public <S,I> MatsEndpoint<Void,S> terminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Description copied from interface:MatsFactorySets up aMatsEndpointthat contains a single stage that typically will be the reply-to endpointId for arequest initiation, or that can be used to directly send a "fire-and-forget" styleinvocationto. The sole stage is supplied directly. This type of endpoint cannot reply, as it has no-one to reply to (hence "terminator"). Do note that this is just a convenience for the often-used scenario where an initiation requests out to some service, and then the reply needs to be handled - and with that the process is finished. That last endpoint which handles the reply is what is referred to as a terminator, in that it has nowhere to reply to. Note that there is nothing hindering you in setting the replyTo endpointId in a request initiation to point to a single-stage or multi-stage endpoint - however, any replies from those endpoints will just go void. It is possible toinitiatefrom within a terminator, and one interesting scenario here is to do apublishto asubscriptionTerminator. The idea is then that you do the actual processing via a request, and upon the reply processing in the terminator, you update the app database with the updated information (e.g. "order is processed"), and then you publish an "update caches" message to all the nodes of the app, so that they all have the new state of the order in their caches (or, in a push-based GUI logic, you might want to update all users' view of that order). Note that you (as in the processing node) will also get that published message on your instance of the SubscriptionTerminator. It is technically possiblereplyfrom within a terminator - but it hard to envision many wise usage scenarios for this, as the stack at a terminator would probably be empty.- Specified by:
terminatorin interfaceMatsFactory- Parameters:
endpointId- the identification of thisMatsEndpoint, which are the strings that should be provided to theMatsInitiator.MatsInitiate.to(String)orMatsInitiator.MatsInitiate.replyTo(String, Object)methods for this endpoint to get the message. Typical structure is"OrderService.placeOrder"for public endpoints (which then is of a "fire-and-forget" style, since a terminator is not meant to reply), or"OrderService.terminator.validateOrder"for private (app-internal) terminators that is targeted by thereplyTo(endpointId,..)invocation of an initiation.stateClass- the class of the State DTO that will may be provided by therequest initiation(or that was sent along with theinvocation).incomingClass- the class of the incoming (typically reply) DTO.processor- thestagethat will be invoked to process the incoming message.- Returns:
- the
MatsEndpoint, but you should not add any stages to it, as the sole stage is already added.
-
terminator
public <S,I> MatsEndpoint<Void,S> terminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, Consumer<? super MatsEndpoint.EndpointConfig<Void, S>> endpointConfigLambda, Consumer<? super MatsStage.StageConfig<Void, S, I>> stageConfigLambda, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Description copied from interface:MatsFactoryVariation ofMatsFactory.terminator(String, Class, Class, ProcessTerminatorLambda)that can be configured "on the fly".- Specified by:
terminatorin interfaceMatsFactory
-
subscriptionTerminator
public <S,I> MatsEndpoint<Void,S> subscriptionTerminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Description copied from interface:MatsFactorySpecial kind of terminator that, in JMS-style terms, subscribes to a topic instead of listening to a queue (i.e. it uses "pub-sub"-style messaging, instead of queue-based). You may only communicate with this type of endpoints by using theMatsInitiator.MatsInitiate.publish(Object)orMatsInitiator.MatsInitiate.replyToSubscription(String, Object)methods. Notice that the concurrency of a SubscriptionTerminator is always 1, as it makes no sense to have multiple processors for a subscription - all of the processors would just get an identical copy of each message. If you do need to handle massive amounts of messages, or your work handling is slow, you should instead of handling the work in the processor itself, rather accept the message as fast as possible and send the work out to be processed by some kind of thread pool. (The toolMatsFuturizerdoes this for its Future-completion handling).- Specified by:
subscriptionTerminatorin interfaceMatsFactory- Parameters:
endpointId- the identification of thisMatsEndpoint, which are the strings that should be provided to theMatsInitiator.MatsInitiate.to(String)orMatsInitiator.MatsInitiate.replyTo(String, Object)methods for this endpoint to get the message.stateClass- the class of the State DTO that will may be provided by therequest initiation(or that was sent along with theinvocation).incomingClass- the class of the incoming (typically reply) DTO.processor- thestagethat will be invoked to process the incoming message.- Returns:
- the
MatsEndpoint, but you should not add any stages to it, as the sole stage is already added.
-
subscriptionTerminator
public <S,I> MatsEndpoint<Void,S> subscriptionTerminator(String endpointId, Class<S> stateClass, Class<I> incomingClass, Consumer<? super MatsEndpoint.EndpointConfig<Void, S>> endpointConfigLambda, Consumer<? super MatsStage.StageConfig<Void, S, I>> stageConfigLambda, MatsEndpoint.ProcessTerminatorLambda<S, I> processor) Description copied from interface:MatsFactoryVariation ofMatsFactory.subscriptionTerminator(String, Class, Class, ProcessTerminatorLambda)that can be configured "on the fly", but notice that the concurrency of a SubscriptionTerminator is always 1.- Specified by:
subscriptionTerminatorin interfaceMatsFactory
-
getEndpoints
- Specified by:
getEndpointsin interfaceMatsFactory- Returns:
- all
MatsEndpoints created on thisMatsFactory.
-
getEndpoint
- Specified by:
getEndpointin interfaceMatsFactory- Parameters:
endpointId- whichMatsEndpointto return, if present.- Returns:
- the requested
MatsEndpointif present,Optional.empty()if not.
-
getDefaultInitiator
Description copied from interface:MatsFactoryGets or creates the default Initiator (whose name is 'default') from which to initiate new Mats processes, i.e. send a message from "outside of Mats" to a Mats endpoint - NOTICE: This is an active object that can carry backend resources, and it is Thread Safe: You are not supposed to create one instance per message you send! IMPORTANT NOTICE!! The MatsInitiator returned from this specific method is special when used within a Mats Stage's context (i.e. the Thread running a Mats Stage): Any initiations performed with it within a Mats Stage will have the same transactional demarcation as an initiation performed usingProcessContext.initiate(..). The idea here is that you thus can create methods that both can be used from the outside of a Mats Stage (thus resulting in an ordinary initiation), but if the same method is invoked within a Mats Stage, the initiation will partake in the same transactional demarcation as the rest of what happens within that Mats Stage. Note that you get a bit strange semantics wrt. the exceptions thatMatsInitiator.initiate(..)andMatsInitiator.initiateUnchecked(..)raises: Outside of a Mats Stage, they can throw in the given situations those exceptions describe. However, within a Mats Stage, they will never throw those exceptions, since the actual initiation is not performed until the Mats Stage exits. But, if you want to make such a dual mode method that can be employed both outside and within a Mats Stage, you should thus code for the "Outside" mode, handling those Exceptions as you would in an ordinary initiation. If you would really NOT want this - i.e. you for some reason want the initiation performed within the stage to execute even though the Mats Stage fails - you may useMatsFactory.getOrCreateInitiator(String), and you can even request the same underlying default initiator by just supplying that method with the argument "default". Please, however, make sure you understand the quite heavy consequence of this: If the Mats Stage throws, the retry-mechanism will kick in, running the Mats Stage one more time, and you will thus potentially send that message many times - one time per retry - since such an initiation with a NON-default MatsInitiator is specifically then not part of the Stage's transactional demarcation. Just to ensure that this point comes across: The returned MatsInitiator is Thread Safe, and meant for reuse: You are not supposed to create one instance ofMatsInitiatorper message you need to send, and then close it afterwards - rather either create one for the entire application, or e.g. for each component: TheMatsInitiatorcan have underlying backend resources attached to it - which also means that it needs to beclosedfor a clean application shutdown (Note that all MatsInitiators are closed whenMatsFactory.stop()is invoked).- Specified by:
getDefaultInitiatorin interfaceMatsFactory- Returns:
- the default
MatsInitiator, whose name is 'default', on which messages can beinitiated. - See Also:
-
getOrCreateInitiator
Description copied from interface:MatsFactoryGets or creates a new Initiator from which to initiate new Mats processes, i.e. send a message from "outside of Mats" to a Mats endpoint - NOTICE: This is an active object that can carry backend resources, and it is Thread Safe: You are not supposed to create one instance per message you send! A reason for wanting to make more than oneMatsInitiatorcould be that each initiator might have its own connection to the underlying message broker. You also might want to name the initiators based on what part of the application uses it; The name of the initiator shows up in monitors and tooling. IMPORTANT NOTICE!! Please read the JavaDoc ofMatsFactory.getDefaultInitiator()for important information wrt. transactional demarcation when employing a NON-default MatsInitiator within a Mats Stage. Just to ensure that this point comes across: The returned MatsInitiator is Thread Safe, and meant for reuse: You are not supposed to create one instance ofMatsInitiatorper message you need to send, and then close it afterwards - rather either create one for the entire application, or e.g. for each component: TheMatsInitiatorcan have underlying backend resources attached to it - which also means that it needs to beclosedfor a clean application shutdown (Note that all MatsInitiators are closed whenMatsFactory.stop()is invoked).- Specified by:
getOrCreateInitiatorin interfaceMatsFactory- Returns:
- a
MatsInitiator, on which messages can beinitiated.
-
getInitiators
- Specified by:
getInitiatorsin interfaceMatsFactory- Returns:
- all
MatsInitiators created on thisMatsFactory.
-
holdEndpointsUntilFactoryIsStarted
public void holdEndpointsUntilFactoryIsStarted()Description copied from interface:MatsFactoryIf this method is invoked before any endpoint is created, the endpoints will not start even thoughMatsEndpoint.finishSetup()is invoked on them, but will wait tillMatsFactory.start()is invoked on the factory. This feature should be employed in most setups where the MATS endpoints might use other services or components whose order of creation and initialization are difficult to fully control, e.g. typically in an IoC container like Spring. Set up the "internal machinery" of the system, including internal services, and only after all this is running, then fire up the endpoints. If this is not done, the endpoints might start consuming messages off of the MQ (there might already be messages waiting when the service boots), and thus invoke services/components that are not yet fully started. Note: To implement delayed start for a specific endpoint, simply hold off on invokingMatsEndpoint.finishSetup()until you are OK with it being started and hence starts consuming messages (e.g. when the needed cache service is finished populated); This semantics works both when holdEndpointsUntilFactoryIsStarted() has been invoked or not.- Specified by:
holdEndpointsUntilFactoryIsStartedin interfaceMatsFactory- See Also:
-
start
public void start()Description copied from interface:MatsFactoryStarts all endpoints that has been created by this factory, by invokingMatsEndpoint.start()on them. Subsequently clears theMatsFactory.holdEndpointsUntilFactoryIsStarted()-flag.- Specified by:
startin interfaceMatsConfig.StartStoppable- Specified by:
startin interfaceMatsFactory
-
waitForReceiving
public boolean waitForReceiving(int timeoutMillis) Description copied from interface:MatsFactoryWaits until all endpoints have fully entered the receive-loops, i.e. runsMatsEndpoint.waitForReceiving(int)on all the endpoints started from this factory. Note: If there are no Endpoints registered, this will immediately returntrue!- Specified by:
waitForReceivingin interfaceMatsConfig.StartStoppable- Specified by:
waitForReceivingin interfaceMatsFactory- Parameters:
timeoutMillis- number of milliseconds before giving up the wait, returningfalse. 0 is indefinite wait, negative values are not allowed.- Returns:
trueif the entity started within the timeout,falseif it did not start.
-
stop
public boolean stop(int gracefulShutdownMillis) Description copied from interface:MatsFactoryStops all endpoints and initiators, by invokingMatsEndpoint.stop(int)on all the endpoints, andMatsInitiator.close()on all initiators that has been created by this factory. They can be started again individually, or all at once by invokingMatsFactory.start()Should be invoked at application shutdown.- Specified by:
stopin interfaceMatsConfig.StartStoppable- Specified by:
stopin interfaceMatsFactory- Parameters:
gracefulShutdownMillis- number of milliseconds to let the stage processors wait after having asked for them to shut down, and interrupting them if they have not shut down yet.- Returns:
trueif all Endpoints (incl. e.g. threads) and resources (e.g. JMS Connections) closed successfully.
-
hashCode
public int hashCode() -
equals
-
toString
-