如何快速学习Java RMI

发布于 2024-09-17 23:25:08 字数 1436 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱殇璃 2024-09-24 23:25:08

实现此目的的一种快速方法是使用 Spring。这并不(必然)意味着使用大量 XML 配置:Spring 的 RMI 支持类可以通过编程方式使用。

两个关键类是:

这样做的一个优点是您只需要编写接口的实现,然后就可以使用 RmiServiceExporter 来实现。同时,在客户端,使用 RmiProxyFactoryBean 为您提供一个实现该接口的代理对象。就客户端代码而言,它正在使用接口的“真实”实现,但代理会为您执行 RMI 调用。 RMI 的使用是透明的。

作为一个示例,说明这有多快,我刚刚使用您的界面编写了服务器和客户端。

我的接口实现是:

public class ApplicationImpl implements Application {

    private boolean enable;

    @Override
    public void setLoggingEnabled(boolean enable) {
        this.enable = enable;
    }

    @Override
    public boolean isLoggingEnabled() {
        return enable;
    }

}

服务器端代码是:

RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setService(new ApplicationImpl());
exporter.setServiceInterface(Application.class);
exporter.setServiceName("application");
exporter.afterPropertiesSet();

客户端代码是:

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());

预期输出:

false
true

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
中二柚 2024-09-24 23:25:08

您可以从官方 RMI 教程开始。


资源:

同一主题:

You can start with the official RMI tutorial.


Resources :

On the same topic :

戏舞 2024-09-24 23:25:08

正如 Yok 和 Colin 所说,请查看 Oracle (Sun) 支持的 RMI 教程,在阅读时尝试编写示例代码并在示例项目中测试它们。

参考

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文