One quick way to do this is to use Spring. This doesn't (necessarily) mean using lots of XML configuration: Spring's RMI support classes can be used programmatically.
The two key classes are:
An advantage of doing it this way is that you only need to write an implementation of your interface, and that can then be made available using RmiServiceExporter
. Meanwhile, on the client side, using RmiProxyFactoryBean
gives you a proxy object that implements the interface. As far as client-side code is concerned, it's working with a 'real' implementation of the interface, but the proxy does the RMI invocations for you. The use of RMI is transparent.
As an example of how quick this can be, I've just written a server and client using your interface.
My implementation of the interface is:
public class ApplicationImpl implements Application {
private boolean enable;
@Override
public void setLoggingEnabled(boolean enable) {
this.enable = enable;
}
@Override
public boolean isLoggingEnabled() {
return enable;
}
}
The server-side code is:
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setService(new ApplicationImpl());
exporter.setServiceInterface(Application.class);
exporter.setServiceName("application");
exporter.afterPropertiesSet();
The client-side code is:
RmiProxyFactoryBean pfb = new RmiProxyFactoryBean();
pfb.setServiceInterface(Application.class);
pfb.setServiceUrl("rmi://localhost/application");
pfb.afterPropertiesSet();
Application app = (Application) pfb.getObject();
System.out.println(app.isLoggingEnabled());
app.setLoggingEnabled(true);
System.out.println(app.isLoggingEnabled());
which as expected outputs:
false
true