public class InvocationDispenser extends Object
public synchronized void replytoA(Object elem1, elem2) {
//do something
}
public void sendCommandA(Object someData) {
String token = this.myInvocationDispenser.register( new Invoker() {
public void invoke(Object... args) {
//check args ?
replytoA(args[0], args[1]) ;
}
} ) ;
ACommand toBeSent = buildCommand(token, someData) ;
sendCommand(toBeSent)
}
The listening Thread :
public void run() {
while(doItAgain) {
// don't forget exception handling!
CommandData receivedData = readReply() ;
String correlationString = receivedData.getToken() ;
Object[] responseData = receivedData.getData() ;
myInvocationDispenser.callBack(correlationString, responseData) ;
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
InvocationDispenser.TokenLostException |
| Constructor and Description |
|---|
InvocationDispenser() |
| Modifier and Type | Method and Description |
|---|---|
Object |
call(String token,
Object... args)
calls the registered invoker which corresponds to the token.
|
Object |
callBack(String token,
Object... args)
calls the registered invoker which corresponds to the token.
|
void |
dumpTask(String token)
Removes a response task
|
String |
generateToken()
generates a unique token (for this ClassLoader context!)
|
boolean |
isTokenUsed(String token)
tells if the token is known.
|
String |
register(Invoker invoker)
registers an Invoker and returns a token to be used to run the invocation code
|
String |
register(String token,
Invoker invoker)
registers an invoker and the token is given by the calling code.
|
Future |
registerFuture(String token,
Invoker invoker)
Gets a Future object from this task registration.
|
public String generateToken()
public String register(Invoker invoker)
invoker - should not be null.public String register(String token, Invoker invoker)
token - invoker - public Future registerFuture(String token, Invoker invoker)
Exemple of call may be :
public String pleaseCall(String arg) {
String token = myInvocationDispenser.generateToken() ;
Future future = registerFuture(token, new Invoker() {
public Object invoke(Object... args) {
return args[0] ;
}
}
ACommand toBeSent = buildCommand(token, arg) ;
sendCommand(toBeSent)
return (String) future.get() ;
}
token - invoker - public void dumpTask(String token)
token - public boolean isTokenUsed(String token)
token - public Object callBack(String token, Object... args) throws Exception
token - args - Exceptionpublic Object call(String token, Object... args) throws Exception
token - args - ExceptionCopyright © 2014 LSST. All rights reserved.