One quick way to do this is to use Spring. This does not mean (required) using a lot of XML configuration: Spring RMI support classes can be used programmatically.
Two key classes:
The advantage of this method is that you only need to write an implementation of your interface, and then you can make it available using RmiServiceExporter . Meanwhile, on the client side, using RmiProxyFactoryBean gives you a proxy object that implements the interface. As for the client-side code, it works with a βrealβ interface implementation, but the proxy server makes RMI calls for you. Using RMI is transparent.
As an example of how fast this can be, I just wrote a server and client using your interface.
My interface implementation:
public class ApplicationImpl implements Application { private boolean enable; @Override public void setLoggingEnabled(boolean enable) { this.enable = enable; } @Override public boolean isLoggingEnabled() { return enable; } }
Server Side Code:
RmiServiceExporter exporter = new RmiServiceExporter(); exporter.setService(new ApplicationImpl()); exporter.setServiceInterface(Application.class); exporter.setServiceName("application"); exporter.afterPropertiesSet();
Client Code:
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 is expected to produce:
false true
Richard Fearn
source share