Class SchedulingInterceptor
- All Implemented Interfaces:
DispatchInterceptor, HandlerDecorator, HandlerInterceptor
This interceptor enables powerful scheduling features such as:
- Initial scheduling of @Periodic types: When a handler method or its parameter type is annotated
with
Periodic, this interceptor will automatically initialize the schedule at startup ifautoStart=true. - Rescheduling on success or failure: Upon successful or failed invocation of a scheduled handler, this interceptor can reschedule the next invocation based on the result or according to cron/delay rules.
- Metadata injection: Adds the
scheduleIdmetadata to outgoing scheduled messages to support correlation and rescheduling. - Dynamic behavior based on handler return value: Supports dynamic rescheduling based on return
types like
TemporalAmount,TemporalAccessor,Schedule, or even a replacement payload object.
Additionally, if the scheduled handler throws a CancelPeriodic exception,
the schedule is permanently cancelled without logging an error.
This interceptor is registered automatically with Fluxzero.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface HandlerDecorator
HandlerDecorator.MergedDecoratorNested classes/interfaces inherited from interface HandlerInterceptor
HandlerInterceptor.InterceptedHandler, HandlerInterceptor.PreparedHandlerFunction, HandlerInterceptor.PreparedHandlerInputFunction, HandlerInterceptor.PreparedHandlerInputInterceptor, HandlerInterceptor.PreparedHandlerInterceptor, HandlerInterceptor.PreparedInterceptedHandler -
Field Summary
Fields inherited from interface DispatchInterceptor
defaultInterceptors, noOpFields inherited from interface HandlerDecorator
noOpFields inherited from interface HandlerInterceptor
defaultInterceptors -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected InstantfirstDeadline(Periodic periodic, Instant now) protected ObjecthandleExceptionalResult(Throwable result, DeserializingMessage schedule, Instant now, Periodic periodic) protected ObjecthandleResult(Object result, DeserializingMessage schedule, Instant now, Periodic periodic) protected voidinitializePeriodicSchedule(MessageScheduler messageScheduler, Schedule schedule, Periodic periodic) protected voidinitializePeriodicSchedule(Class<?> payloadType, Periodic periodic) protected voidinitializePeriodicSchedule(Class<?> payloadType, Periodic periodic, String namespace) voidinitializePeriodicSchedules(Object target, String namespace) Initializes auto-start schedules for one handler in the namespace of the consumer it was assigned to.interceptDispatch(Message message, MessageType messageType, String topic) Intercepts the dispatch of a message before it is serialized and published or locally handled.interceptHandling(Function<DeserializingMessage, Object> function, HandlerInvoker invoker) Intercepts the message handling logic.protected InstantnextDeadline(Periodic periodic, Instant now) prepare(HandlerDescriptor handler) Prepares this interceptor for a specific handler method.protected booleanshouldReplacePeriodicSchedule(Schedule currentSchedule, Periodic periodic) booleanIndicates whether this interceptor opts into the prepared, mergeable handler wrapper.wrap(Handler<DeserializingMessage> handler) Wraps aHandlerwith this interceptor, producing an intercepted handler.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DispatchInterceptor
andThen, interceptDispatch, modifySerializedMessage, monitorDispatch, prepareLocalDispatch, withNamespaceMethods inherited from interface HandlerDecorator
andThenMethods inherited from interface HandlerInterceptor
prepareInput, prepareInput
-
Constructor Details
-
SchedulingInterceptor
public SchedulingInterceptor()
-
-
Method Details
-
wrap
Description copied from interface:HandlerInterceptorWraps aHandlerwith this interceptor, producing an intercepted handler.- Specified by:
wrapin interfaceHandlerDecorator- Specified by:
wrapin interfaceHandlerInterceptor- Parameters:
handler- the original handler to wrap- Returns:
- an intercepted handler that applies this interceptor to all handled messages
-
initializePeriodicSchedules
-
initializePeriodicSchedule
-
initializePeriodicSchedule
-
initializePeriodicSchedule
protected void initializePeriodicSchedule(MessageScheduler messageScheduler, Schedule schedule, Periodic periodic) -
shouldReplacePeriodicSchedule
-
firstDeadline
-
nextDeadline
-
interceptDispatch
Description copied from interface:DispatchInterceptorIntercepts the dispatch of a message before it is serialized and published or locally handled.You may modify the message or return
nullto block dispatching. Throwing an exception also prevents dispatching.- Specified by:
interceptDispatchin interfaceDispatchInterceptor- Parameters:
message- the message to be dispatchedmessageType- the type of the message (e.g., COMMAND, EVENT, etc.)topic- the target topic or null if not applicable- Returns:
- the modified message, the same message, or
nullto prevent dispatch
-
interceptHandling
public Function<DeserializingMessage, Object> interceptHandling(Function<DeserializingMessage, Object> function, HandlerInvoker invoker) Description copied from interface:HandlerInterceptorIntercepts the message handling logic.The
functionparameter represents the next step in the handling chain— typically the actual message handler. Theinvokerprovides metadata and invocation logic for the underlying handler method.Within this method, an interceptor may:
- Modify the
DeserializingMessagebefore passing it to the handler - Bypass the handler entirely and return a value directly
- Wrap the result after the handler is invoked
Note: Interceptors may return a different
DeserializingMessage, but it must be compatible with a handler method in the same target class. If no suitable handler is found, an exception will be thrown.- Specified by:
interceptHandlingin interfaceHandlerInterceptor- Parameters:
function- the next step in the handler chain (typically the handler itself)invoker- the metadata and execution strategy for the actual handler method- Returns:
- a decorated function that wraps handling behavior
- Modify the
-
prepare
Description copied from interface:HandlerInterceptorPrepares this interceptor for a specific handler method.The returned interceptor is cached and reused concurrently for invocations with the same stable handler metadata. Implementations may use this hook to resolve annotation- or signature-based policy once while keeping message-dependent decisions inside
HandlerInterceptor.PreparedHandlerInterceptor.interceptHandling(DeserializingMessage, HandlerDescriptor, BiFunction, HandlerInterceptor.PreparedHandlerFunction).The default adapter preserves the existing
HandlerInterceptor.interceptHandling(Function, HandlerInvoker)contract and deliberately disables the reusableHandlerMethodpath. Custom interceptors therefore retain their current behavior unless they explicitly opt into preparation.- Specified by:
preparein interfaceHandlerInterceptor- Parameters:
handler- stable metadata for the handler method- Returns:
- a thread-safe prepared interceptor
-
supportsPreparation
public boolean supportsPreparation()Description copied from interface:HandlerInterceptorIndicates whether this interceptor opts into the prepared, mergeable handler wrapper.The default is
false, preserving the exact wrapper behavior of existing custom interceptors. An interceptor should returntrueonly whenHandlerInterceptor.prepare(HandlerDescriptor)returns a thread-safe plan that fully represents its handling behavior. If a subclass overrides only the legacyHandlerInterceptor.interceptHandling(Function, HandlerInvoker)method, the legacy wrapper is retained automatically.- Specified by:
supportsPreparationin interfaceHandlerInterceptor- Returns:
truewhen this interceptor may be merged into a prepared wrapper
-
handleResult
protected Object handleResult(Object result, DeserializingMessage schedule, Instant now, Periodic periodic) -
handleExceptionalResult
protected Object handleExceptionalResult(Throwable result, DeserializingMessage schedule, Instant now, Periodic periodic)
-