Java: A Simple RMI Client Proxy Class
By admin on Mar 9, 2008 in Java, Programming
Here is a simple RMI client proxy class that will help you to connect to a RMI server, cache the connection, call the method.
Some of the code snippet
// FactoryMethod
public RmiClientProxy(Class serviceInterface, String serviceUrl) {
this.serviceInterface = serviceInterface;
this.serviceUrl = serviceUrl;
serviceProxy = createProxy();
prepare();
}
This is a constructor that receives a service interface and the RMI connection string.
rivate Object createProxy(){
return Proxy.newProxyInstance(beanClassLoader,
new Class[]{serviceInterface}, new InvocationHandler() {
public Object invoke(Object proxy,
Method method, Object[] args) throws Throwable {
Remote stub = getStub();
try {
return doInvoke(method, args, stub);
} catch (RemoteConnectFailureException ex) {
return handleRemoteConnectFailure(method, args, ex);
} catch (RemoteException ex) {
if (RmiUtils.isConnectFailure(ex)) {
return
handleRemoteConnectFailure(method, args, ex);
} else {
throw ex;
}
}
}
});
}
It uses reflection to create the class and invoke the method.
