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 code must either be defined (1) as a Runnable
and
given as constructor argument, or (2) by re-defining code()
in a derived class.
Then run()
can be called to start its 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.The payload code to executeprotected boolean
Whether currently running.Fields inherited from class eu.bandm.tools.message.SingleSender
drain
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor which sets neither the message drain nor the payload code.Constructor which sets only the message receiver.MessageCatcher
(Runnable c, @Opt MessageReceiver<Message> rec) Constructor which sets the payload code and the message receiver. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
code()
Override this method to define the payload code, or set the value ofrunnable
via constructor call.Returns the last exception caught, if any.boolean
Returns whether currently running.void
run()
Start the execution of the given payload code.Methods inherited from class eu.bandm.tools.message.SingleSender
getDrain, getReceiver, send, setDrain, setReceiver
-
Field Details
-
caught
local cache for the caught exception. -
runnable
The payload code to execute -
running
protected boolean runningWhether currently running.
-
-
Constructor Details
-
MessageCatcher
Constructor which sets the payload code and the message receiver.- Parameters:
c
- the code to executerec
- the message drain. When null, it must be overwritten before using this instance.- See Also:
-
MessageCatcher
Constructor which sets only the message receiver. When using this constructor, you have to re-define the methodcode()
. Otherwise trying torun()
this instance will crash intentionally.- Parameters:
rec
- the drain for all caught messages.- See Also:
-
MessageCatcher
public MessageCatcher()Constructor which sets neither the message drain nor the payload code. When using this constructor, you have to re-define the methodcode()
. Otherwise trying torun()
this instance will crash intentionally.- See Also:
-
-
Method Details
-
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.
-
code
protected void code() -
run
public void run()Start the execution of the given payload code.- Specified by:
run
in interfaceRunnable
- Throws:
IllegalStateException
- if called when currently running.
-