how to learn Java RMI quickly [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing thi开发者_Python百科s post.
Closed 3 years ago.
Improve this questionI have a Java application I've been working on for a year or two now. I would like to create a very simple set (with potential to add complexity later) of interfaces that I can use to control my Java app from another JVM (e.g. MATLAB).
I am assuming RMI is the best way to do this, but I'm not sure, as I know next to nothing about it.
What is the best way to learn RMI quickly?
Let's say I want to use an interface like this:
interface Application {
public void setLoggingEnabled(boolean enable);
public boolean isLoggingEnabled();
}
How could I implement a bridge between the two JVMs with this interface using RMI? What do I have to know about blocking / threading / synchronization to make this work?
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:
RmiServiceExporter
to make an object remotely accessible.RmiProxyFactoryBean
to access a remote object.
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
You can start with the official RMI tutorial.
Resources :
- Introduction to Java RMI
- Learn Java RMI
On the same topic :
- Java RMI Resources
Good links to start with :
RMI Guide :
http://download.oracle.com/javase/6/docs/technotes/guides/rmi/index.html
http://download.oracle.com/javase/1.4.2/docs/guide/rmi/
RMI Tutorial:
http://download.oracle.com/javase/tutorial/rmi/index.html
As Yok and Colin said Take a look to the RMI tutorial supported by Oracle (Sun) and by the time you are reading try to code the example codes and test them in an example project.
References
- RMI Tutorial : http://download.oracle.com/javase/tutorial/rmi/index.html
精彩评论