RSS Feed for This PostCurrent Article

Java: A Simple RMI Client Proxy Class

Download Source Code

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.


Trackback URL


Sorry, comments for this entry are closed at this time.