Spring RMI callback
One open issue of Spring remoting via RMI is that it hasn't supported RMI callback as discussed in here.The conventional RMI callback requires the interface implements Remote and the methods throw RemoteException. Hereby it is not very nice.
WlcfgRmiCallbackProxyFactory is implemented to provide such support by customizing Spring remoting code. It provides a remote proxy which intercepts method invocation on callback interface (non-RMI). It uses wlcfgRmiCallbackExporter which wraps a non-RMI service into remote object via RmiInvocationHandler and export it. Note that it doesn't bind to RMI registry.
public class WlcfgRmiCallbackProxyFactory extends RemoteInvocationBasedAccessor
implements MethodInterceptor, Serializable {
private static final long serialVersionUID = 5685339356048366732L;
private Class callbackInterface;
private RmiInvocationHandler invocationHandler;
public WlcfgRmiCallbackProxyFactory(Object callback, Class callbackInterface) {
this.callbackInterface = callbackInterface;
WlcfgRmiCallbackExporter exporter = new WlcfgRmiCallbackExporter();
this.invocationHandler = exporter.getRmiInvocationHandler(callback,
callbackInterface);
}
public Proxy getProxy() {
Proxy proxy = (Proxy) ProxyFactory.getProxy(callbackInterface, this);
return proxy;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
return invocationHandler.invoke(createRemoteInvocation(invocation));
} catch (RemoteException re) {
throw RmiClientInterceptorUtils.convertRmiAccessException(
invocation.getMethod(), re, RmiClientInterceptorUtils.isConnectFailure(re), ClassUtils.getShortName(callbackInterface));
} catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException();
RemoteInvocationUtils.fillInClientStackTraceIfPossible(targetEx);
throw targetEx;
}
}
}
public class WlcfgRmiCallbackExporter extends RmiBasedExporter {
private static Log log = LogFactory.getLog(WlcfgRmiCallbackExporter.class);
public RmiInvocationHandler getRmiInvocationHandler(Object callback, Class callbackInterface) {
this.setService(callback);
this.setServiceInterface(callbackInterface);
this.setRegisterTraceInterceptor(false);
// wrap non-RMI service into a remote object
Remote exportedObject = getObjectToExport();
try {
UnicastRemoteObject.exportObject(exportedObject, 0);
} catch(RemoteException e) {
log.error("Failed to export " + getServiceInterface().getName(), e);
}
log.info("Export object " + getServiceInterface().getName());
return (RmiInvocationHandler)exportedObject;
}
}
There are two types of proxy: JDK dynamic proxies or CGLIB. Spring can use both for creating proxies at runtime. One is based on interface, the other for concrete class as discussed in here. With the invocation of ProxyFactory.getProxy(), a Proxy class is generated at runtime which implements all of the supplied interfaces. It is fancy that the returned proxy is an instance of the interface and can be casted as:
WlcfgRmiCallbackProxyFactory proxyFactory = new WlcfgRmiCallbackProxyFactory(this, IHostSessionListener.class);
hostService.registerForNofitications((IHostSessionListener)proxyFactory.getProxy());
Two common usages of proxies are demonstrated above:
- Line 18, add interceptor so that a normal method call becomes remote call.
- Line 47, limit method access to target source object - only methods defined in serviceInterface are exposed. Otherwise, with reflection method call, all methods would be exposed.