how to learn Java RMI quickly

帅比萌擦擦* 提交于 2019-12-12 07:58:58

问题


I 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?


回答1:


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




回答2:


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



回答3:


You can start with the official RMI tutorial.


Resources :

  • Introduction to Java RMI
  • Learn Java RMI

On the same topic :

  • Java RMI Resources



回答4:


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


来源:https://stackoverflow.com/questions/3638024/how-to-learn-java-rmi-quickly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!