public class RPCInvoker<T extends RPCIf,E extends Endpoint> extends Object implements RPCIf
ChannelTransportを指定して、そのTransport上でRPCを行うためのクラスである。
RPCは、以下のようにして行う。
int n = rpcInvoker.getStub(AppIf.class,targetObjectId,targetEndpoint).appMethod(arg1,arg2);
ここで、AppIfは、対象のオブジェクトが実装しているインターフェースである。 AppIfは、RPCIfを継承していなければいけない。targetObjectIdは、対象のObjectIdである。 targetEndpointは、対象のオブジェクトが存在するピアを表すEndpointである。Endpointの実際の型は、 このクラスの型パラメータEが示す型でなければいけない。対象は、対象の存在するピアの対応する RPCInvokerに対して、registerRPCObjectを使用して登録されていなければならない。 appMethodは、対象が持つメソッドであり、arg1, arg2は、appMethodの引数であるものとする。 appMethodの返り値の型はintであるとする。 appMethodは、AppIfあるいは、 そのスーパー・インターフェースで宣言されていなければいけない。さらに、対象が呼び出し側と異なるピアに存在する場合は @RemoteCallableアノテーションがついていて、かつRPCExceptionをスローするように宣言されていなければいけない。 例えば、以下の通りである。
import org.piax.gtrans.RemoteCallable;
interface AppIf extends RPCIf {
@RemoteCallable
int appMethod(int a1, String a2) throws RPCException;
}
引数のコピー
RPCでは、引数は、一般にはコピーされるが、保証されない。呼び出されたメソッド内で引数のオブジェクトを変更した場合、
それが呼び出し側に反映されるかどうかは不明であるので注意が必要である。
呼び出された側では、変更しない方が無難である。
oneway RPC
呼び出したメソッドの終了を待つ通常の同期型RPCの他に、終了を待たないoneway RPCが存在する。 oneway RPCでは、呼び出したメソッドの返り値を受け取ることはできない。 @RemoteCallable アノテーションにType.ONEWAYの引数を指定すると
デフォルトではONEWAY RPCとして呼ばれる。
oneway RPCを指定しても実際に、非同期になるかどうかは、
使用しているトランスポートに依存するので、注意が必要である。
動的RPC
RPCは原則として対象のインターフェースを指定して行うが 例外的に対象のインターフェースが、実行時まで不明な場合がある。 例えば、デバッグなどで実行時に人間が、メソッドや引数を指定する場合である。 この場合は、人間はインターフェースを知っているが、呼び出し側のプログラムには インターフェースが存在しないことがある。 このような場合のために、動的な呼び出しを用意している。 以下のように使用する。
try {
Object r = rpcInvoker.rcall(targetObjectId,targetEndpoint,"appMethod",arg1,arg2);
} catch (Throwable t) {
// 例外処理
}
返り値は常にObject型となる。intなど基本型はボクシングが行われてInteger型などになる。
例外は、何が発生するか不明なので、Throwableを受けなければいけない。
その他
getStubには、いくつかバリエーションが存在する。 timeoutを指定できるもの、RPCModeを指定できるもの、 targetObjectId,targetEndpointの代わりにCallerrIdを指定するものが存在する。 timeoutが指定できないものでは、GTransConfigValues.rpcTimeoutが使用される。 RPCModeを指定できるものでは、指定によりoneway RPCかどうかの決定方法を 以下のように変更可能である。| 修飾子とタイプ | クラスと説明 |
|---|---|
static class |
RPCInvoker.MethodCall |
| 修飾子とタイプ | フィールドと説明 |
|---|---|
Map<E,org.piax.gtrans.RPCInvoker.ChannelPoolEntry> |
channelPool |
protected boolean |
isActive
RPCInvokerオブジェクトがアクティブな状態であることを示す。
|
protected ObjectId |
objId |
static boolean |
POOL_CHANNEL |
static int |
POOL_CHANNEL_SIZE |
protected ChannelTransport<E> |
trans |
protected TransportId |
transId |
static boolean |
USE_CHANNEL_FOR_ONEWAY |
| コンストラクタと説明 |
|---|
RPCInvoker(TransportId rpcId,
ChannelTransport<? super E> trans) |
| 修飾子とタイプ | メソッドと説明 |
|---|---|
void |
changeRPCTimeout(RPCIf stub,
int timeout) |
void |
changeTransport(ChannelTransport<?> trans) |
protected void |
checkActive()
RPCInvokerオブジェクトがアクティブな状態であるかどうかをチェックする。
|
void |
fin() |
E |
getEndpoint() |
RPCIf |
getRPCObject(ObjectId objId) |
PeerId |
getSrcPeerId()
RPCで呼ばれた際の呼び出し側を返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
CalleeId cid)
リモートピア上の、clz型のインターフェースをを持ち、
cidで指定される
オブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
CalleeId cid,
int timeout)
リモートピア上の、clz型のインターフェースを持ち、
cidで指定される
オブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
CalleeId cid,
int timeout,
RPCMode rpcMode)
リモートピア上の、clz型のインターフェースを実装し、
cidで指定されるオブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
CalleeId cid,
RPCMode rpcMode)
リモートピア上の、clz型のインターフェースを実装し、
cidで指定されるオブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
ObjectId targetId,
E remotePeer)
リモートピア上の、clz型のインターフェースを持ち、
targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
ObjectId targetId,
E remotePeer,
int timeout)
リモートピア上の、clz型のインターフェースを持ち、
targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
ObjectId targetId,
E remotePeer,
int timeout,
RPCMode rpcMode)
リモートピア上の、clz型のインターフェースを実装し、
targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
|
<S extends RPCIf> |
getStub(Class<S> clz,
ObjectId targetId,
E remotePeer,
RPCMode rpcMode)
リモートピア上の、clz型のインターフェースを実装し、
targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
|
T |
getStub(E remotePeer)
リモートピア上のこのオブジェクトに対応するRPCInvokerオブジェクトのメソッドを
呼び出すためのstubを返す。
|
T |
getStub(E remotePeer,
int timeout)
リモートピア上のこのオブジェクトに対応するRPCInvokerオブジェクトのメソッドを呼び出すためのstubを返す。
|
ChannelTransport<E> |
getTransport() |
protected Object |
invokeInReceive(boolean isOneway,
RPCIf obj,
RPCInvoker.MethodCall mc) |
boolean |
isOnline()
非推奨です。
|
protected RPCInvoker.MethodCall |
newMethodCall(ObjectId target,
E remotePeer,
boolean oneway,
String method,
Object... args) |
void |
offline()
非推奨です。
|
void |
online()
非推奨です。
|
Object |
rcall(CalleeId cid,
int timeout,
RPCMode rpcMode,
String method,
Object... args)
cidで指定されるオブジェクトのメソッドを動的に呼び出す。
|
Object |
rcall(CalleeId cid,
int timeout,
String method,
Object... args)
リモートピア上の、clz型のインターフェースを実装し、
cidで指定されるオブジェクトのメソッドを動的に呼び出す。
|
Object |
rcall(CalleeId cid,
RPCMode rpcMode,
String method,
Object... args)
cidで指定されるオブジェクトのメソッドを動的に呼び出す。
|
Object |
rcall(CalleeId cid,
String method,
Object... args)
Call a remote method on the object specified by cid.
|
Object |
rcall(ObjectId targetId,
E remotePeer,
int timeout,
RPCMode rpcMode,
String method,
Object... args)
targetIdを持つオブジェクトのメソッドを動的に呼び出す。
|
Object |
rcall(ObjectId targetId,
E remotePeer,
int timeout,
String method,
Object... args)
リモートピア上の、clz型のインターフェースを実装し、
targetIdを持つオブジェクトのメソッドを動的に呼び出す。
|
Object |
rcall(ObjectId targetId,
E remotePeer,
RPCMode rpcMode,
String method,
Object... args)
リモートピア上の、clz型のインターフェースを実装し、
targetIdを持つオブジェクトのメソッドを動的に呼び出す。
|
Object |
rcall(ObjectId targetId,
E remotePeer,
String method,
Object... args)
リモートピア上の、clz型のインターフェースを実装し、
targetIdを持つオブジェクトのメソッドを動的に呼び出す。
|
protected void |
receiveOneway(RPCInvoker.MethodCall mc) |
protected ReturnValue<?> |
receiveSync(RPCInvoker.MethodCall mc) |
void |
registerRPCObject(ObjectId objId,
RPCIf obj)
RPCの対象となるオブジェクトを登録する。
|
ReturnValue<?> |
sendInvoke(ObjectId target,
E remotePeer,
int timeout,
String method,
Object... args)
send an oneway RPC invocation message.
|
void |
sendOnewayInvoke(ObjectId target,
E remotePeer,
String method,
Object... args)
send an oneway RPC invocation message.
|
boolean |
unregisterRPCObject(ObjectId objId,
RPCIf obj)
RPC対象から抹消する。
|
public static boolean POOL_CHANNEL
public static int POOL_CHANNEL_SIZE
public static boolean USE_CHANNEL_FOR_ONEWAY
protected final TransportId transId
protected final ObjectId objId
protected volatile ChannelTransport<E extends Endpoint> trans
protected volatile boolean isActive
public RPCInvoker(TransportId rpcId, ChannelTransport<? super E> trans) throws IdConflictException, IOException
public PeerId getSrcPeerId()
public void fin()
protected void checkActive()
throws IllegalStateException
IllegalStateException - RPCInvokerオブジェクトがインアクティブな状態である場合public void changeTransport(ChannelTransport<?> trans)
public void registerRPCObject(ObjectId objId, RPCIf obj) throws IdConflictException
objId - 対象のObjectIdobj - 対象オブジェクトIdConflictException - thrown if the object ID is already used.public boolean unregisterRPCObject(ObjectId objId, RPCIf obj)
objId - 対象のObjectIdobj - 対象オブジェクト@Deprecated public void online()
@Deprecated public void offline()
@Deprecated public boolean isOnline()
public ChannelTransport<E> getTransport()
public E getEndpoint()
public T getStub(E remotePeer)
remotePeer - リモートピアを示すEndpointpublic T getStub(E remotePeer, int timeout)
remotePeer - リモートピアを示すEndpointtimeout - timeout値(msec)public <S extends RPCIf> S getStub(Class<S> clz, ObjectId targetId, E remotePeer, int timeout, RPCMode rpcMode)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェースtargetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointtimeout - timeout値(msec)rpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。public <S extends RPCIf> S getStub(Class<S> clz, ObjectId targetId, E remotePeer, RPCMode rpcMode)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェースtargetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointrpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。public <S extends RPCIf> S getStub(Class<S> clz, ObjectId targetId, E remotePeer, int timeout)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトの型targetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointtimeout - timeout値(msec)public <S extends RPCIf> S getStub(Class<S> clz, ObjectId targetId, E remotePeer)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトの型targetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointpublic <S extends RPCIf> S getStub(Class<S> clz, CalleeId cid, int timeout, RPCMode rpcMode)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェースcid - RPC呼び出しの対象となるオブジェクトのIdtimeout - timeout値(msec)rpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。public <S extends RPCIf> S getStub(Class<S> clz, CalleeId cid, RPCMode rpcMode)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェースcid - RPC呼び出しの対象となるオブジェクトのIdrpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。public <S extends RPCIf> S getStub(Class<S> clz, CalleeId cid, int timeout)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトの型cid - the id of the callee.timeout - timeout値(msec)public <S extends RPCIf> S getStub(Class<S> clz, CalleeId cid)
S - A type of RPCIfclz - RPC呼び出しの対象となるオブジェクトの型cid - the id of the callee.public Object rcall(ObjectId targetId, E remotePeer, int timeout, RPCMode rpcMode, String method, Object... args) throws Throwable
targetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointtimeout - timeout値(msec)rpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(ObjectId targetId, E remotePeer, RPCMode rpcMode, String method, Object... args) throws Throwable
targetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointrpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(ObjectId targetId, E remotePeer, int timeout, String method, Object... args) throws Throwable
targetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointtimeout - timeout値(msec)method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(ObjectId targetId, E remotePeer, String method, Object... args) throws Throwable
targetId - RPC呼び出しの対象となるオブジェクトのIdremotePeer - リモートピアを示すEndpointmethod - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(CalleeId cid, int timeout, RPCMode rpcMode, String method, Object... args) throws Throwable
cid - the id of the callee.timeout - timeout値(msec)rpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(CalleeId cid, RPCMode rpcMode, String method, Object... args) throws Throwable
cid - the id of the callee.rpcMode - Onewayかどうかを指定する。
AUTOならば、Annotationにより決定する。
SYNCならば、常に同期型である。
ONEWAYならば、常にOnewayである。method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(CalleeId cid, int timeout, String method, Object... args) throws Throwable
cid - the id of the callee.timeout - timeout値(msec)method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public Object rcall(CalleeId cid, String method, Object... args) throws Throwable
cid - the id of the callee.method - the name of the method.args - the arguments for the method.Throwable - exceptions occurred while RPC.public void changeRPCTimeout(RPCIf stub, int timeout) throws IllegalArgumentException
protected RPCInvoker.MethodCall newMethodCall(ObjectId target, E remotePeer, boolean oneway, String method, Object... args)
public void sendOnewayInvoke(ObjectId target, E remotePeer, String method, Object... args) throws RPCException
target - the target object ID.remotePeer - the endpoint of the remote peer.method - the name of the method.args - the arguments of the method.RPCException - an exception occurred while the RPC.public ReturnValue<?> sendInvoke(ObjectId target, E remotePeer, int timeout, String method, Object... args) throws RPCException
target - the target object ID.remotePeer - the endpoint of the remote peer.timeout - the timeout.method - the name of the method.args - the arguments of the method.RPCException - an exception occurred while the RPC.protected Object invokeInReceive(boolean isOneway, RPCIf obj, RPCInvoker.MethodCall mc) throws NoSuchMethodException, InvocationTargetException
protected void receiveOneway(RPCInvoker.MethodCall mc)
protected ReturnValue<?> receiveSync(RPCInvoker.MethodCall mc)
Copyright © 2017. All rights reserved.