是否可以中断Java RMI调用? [英] Is it possible to interrupt a Java RMI call?

查看:279
本文介绍了是否可以中断Java RMI调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在做一些原型设计工作,我们想知道是否有可能中断执行RMI调用的线程。如果我们在这个线程上调用interrupt(),它会抛出一个InterruptedException吗? (或应该?)我们的测试目前显示它没有。只是想知道它应该如何。

We're doing some prototyping work and we're wondering if it is possible to interrupt a thread that performed an RMI call. If we call interrupt() on this thread, would it throw an InterruptedException? (or should it?) Our testing currently shows it doesn't. Just wondering how it should be.

推荐答案

可中断的RMI库提供了一种中断RMI调用的机制。通常,当线程调用RMI方法时,线程会阻塞,直到RMI方法返回。如果方法调用花费的时间太长(例如,如果RMI服务器忙,或挂起,或者用户想要取消RMI操作),则没有简单的方法来中断阻塞RMI调用并将控制权返回给RMI线程。可中断的RMI库提供此功能。

The Interruptible RMI library provides a mechanism to interrupt RMI calls. Typically when a thread invokes an RMI method, the thread blocks until the RMI method returns. If the method call is taking too long (e.g. if the RMI server is busy, or hangs, or if the user wants to cancel the RMI operation), there is no easy way to interrupt the blocking RMI call and return control to the RMI thread. The Interruptible RMI library provides this functionality.

该库由两个关键组件组成:RMISocketFactory和ThreadFactory。使用来自RMISocketFactory的套接字从提供的ThreadFactory到RMI接口的线程上的RMI调用可以通过在客户端线程上调用Thread#interrupt()来中断。所以,它真的很容易使用。但请注意,在服务器端,您可能希望添加对方便实用程序方法的调用,以确保当前线程不是已被客户端中断的僵尸或孤立线程。

The library consists of two key components: an RMISocketFactory and a ThreadFactory. RMI calls made on a thread from the provided ThreadFactory to an RMI interface using sockets from the RMISocketFactory can be interrupted by calling Thread#interrupt() on the client thread. So, it's really easy to use. But note also on the server side that you may wish to add a call to a handy utility method to ensure that the current thread isn't a "zombie" or orphaned thread that has already been "interrupted" by the client.

以编程方式停止RMI服务器:

RMI服务器启动永不结束的线程。这样服务器在手动关闭之前是持久的。但是,您可以为远程服务器提供远程方法 shutDown()。此方法启动关闭线程。关闭线程仍在等待 notify()。当服务器完成所有清理处理时,它会唤醒关闭线程。

RMI Servers start a thread that never ends. This is so that the Server is persistent until manually shut down. However, you can provide a remote method, shutDown(), for the Remote Server. This method starts a shutdown thread. The shutdown thread remains waiting for a notify(). When the Server finishes all clean-up processing it wakes up the shutdown thread.

关闭线程在两(2)秒延迟后调用 System.exit(0)来结束Java虚拟机。延迟是这样的,从服务器到发起客户端的消息完成了旅程。

The shutdown thread, after a two (2) second delay, calls System.exit(0) to end the Java Virtual Machine. The delay is so that messages from the Server to the initiating Client complete the journey.

这篇关于是否可以中断Java RMI调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆