public class InvokingReceiver extends MethodClassExecutor implements Receiver
MessageDispatcher
and the dispatcher
offers a message to this receiver, the message is accepted if there exists a method with
that message as an argument in the provided object. The method is then invoked with the message
as its parameter. However, the method is invoked from the message dispatcher thread context and
thus blocks the dispatcher's message receiving until the method finishes. See QueueInvokingReceiver
and ThreadInvokingReceiver
for non-blocking invokers.
For example, if there is an instance example
the following class:
class Example { public void receive(MySpecialMessage msg) { ... } public void receive(MyOtherMessage msg) { ... } public void receive(ExampleMessage msg) { ... } }where the
MySpecialMessage
, MyOtherMessage
and ExampleMessage
are descendants of Message
.
The following code registers a new invoking receiver with the message dispatcher:
messageDispather.registerReceiver(new InvokingReceiver(example, "receive"));Then, whenever an instance of message
MySpecialMessage
arrives at the dispatcher, it will be accepted by this
created InvokingReceiver
and the first method of Example
class will be invoked with the received message in
its parameter.
MessageDispatcher
MethodExecutor.ExecutableMethod
differentiateByArgNo, registeredMethods
executionObject
Constructor and Description |
---|
InvokingReceiver(java.lang.Object executionObject,
java.lang.String methodsName)
Creates a new instance of InvokingReceiver for message methods.
|
Modifier and Type | Method and Description |
---|---|
boolean |
acceptMessage(Message msg,
boolean allowSuperclass)
Accepts the message if a there is a method for the message's class.
|
protected void |
processMessage(Message msg,
java.lang.reflect.Method method)
Invoke the method associtated with the accepted message.
|
getClassMethods, getDifferentiatingClasses, getDifferentiatingClasses, getMethod, getMethod, getRegisteredMethods
backgroundExecute, backgroundExecute, backgroundExecute, execute, execute, printUsage, printUsage, printUsage
public InvokingReceiver(java.lang.Object executionObject, java.lang.String methodsName) throws java.lang.IllegalArgumentException
executionObject
that have the specified methodsName
and one message argument
(i.e. a class that is a descendant of Message
) are remebered and associated with
their message argument class.
Invoking then uses this fast association to invoke a method specific for the received message
(according to its class).executionObject
- the object on which the message methods are invokedmethodsName
- the name of the methods to inspect (if null, all methods are inspected)java.lang.IllegalArgumentException
- if the supplied execution object is nullpublic boolean acceptMessage(Message msg, boolean allowSuperclass)
acceptMessage
in interface Receiver
msg
- the message offered to acceptanceallowSuperclass
- First, the message is offered with allowSuperclass set to false.
If no receiver accepts it, another offering round is issued with allowSuperclass
set to true (so the receiver can relax its acceptance conditions).protected void processMessage(Message msg, java.lang.reflect.Method method)
msg
- the accepted message (it will be the parameter for the invoked method)method
- the method to invoke on the executionObject