Package eu.bandm.tools.message
Class MessageCatcher
- All Implemented Interfaces:
Runnable
Executes payload code and sends the first exception caught to its given
message drain.
The execution ends with the programmed end of the payload code, or when a exception is caught. Then execution can be restarted.
The payload code can be defined
- by explicitly defining
payload()in a derived class, or - by using the factory method
tryCatch(java.lang.Runnable, eu.bandm.tools.message.MessageReceiver<eu.bandm.tools.message.Message>)with aRunnable
In either case, run() starts the controlled execution.
A caught MessageException is unwrapped and sent directly to the
given MessageReceiver. All other
exceptions are wrapped into a SimpleMessage of kind
failure.
Errors are not caught, only Exceptions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionlocal cache for the caught exception.protected booleanWhether currently running.Fields inherited from class eu.bandm.tools.message.SingleSender
drain -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor which sets neither the message drain nor the payload code.protectedConstructor which sets only the message receiver. -
Method Summary
Modifier and TypeMethodDescriptionReturns the last exception caught, if any.booleanReturns whether currently running.protected abstract voidpayload()Override this method to define the payload code.voidrun()Start the execution of the given payload code.static MessageCatchertryCatch(Runnable payload, @Opt MessageReceiver<Message> rec) Creates a new instance with the given payload code and message receiver.Methods inherited from class eu.bandm.tools.message.SingleSender
getDrain, send, setDrain
-
Field Details
-
caught
local cache for the caught exception. -
running
protected boolean runningWhether currently running.
-
-
Constructor Details
-
MessageCatcher
Constructor which sets only the message receiver. When using this constructor, subclasses must define the methodpayload().- Parameters:
rec- the drain for all caught messages.- See Also:
-
MessageCatcher
protected MessageCatcher()Constructor which sets neither the message drain nor the payload code. When using this constructor, subclasses must define the methodpayload().- See Also:
-
-
Method Details
-
tryCatch
Creates a new instance with the given payload code and message receiver.- Parameters:
payload- the code to executerec- the message drain. When null, it must be overwritten before using this instance.- Returns:
- an executable object that runs
payload, and sends a caught message exception torec - See Also:
-
isRunning
public boolean isRunning()Returns whether currently running.- Returns:
- whether currently running.
-
getCaught
Returns the last exception caught, if any.- Returns:
- the last exception caught, if any.
-
payload
protected abstract void payload()Override this method to define the payload code. -
run
public void run()Start the execution of the given payload code.- Specified by:
runin interfaceRunnable- Throws:
IllegalStateException- if called when currently running.
-