如何设计一个 API 来使用 java RMI 操作远程对象

发布于 2024-09-06 09:04:29 字数 1174 浏览 2 评论 0原文

我有一个通过 RMI 公开的远程对象,我用它来对该对象所拥有的资源执行一些操作。现在我对需要执行的每个操作都有方法了。

示例:

public interface IRobotController {
    public int walk(int meters);
    public void dance(int seconds);
}

public interface RMIRobotController implements IRobotController, java.rmi.Remote {}

public class RobotController implements RMIRobotController {
    private Robot robot;

    ...

    public int walk(int meters) {
        return robot.walk(meters);
    }

    public void dance(int seconds) {
        robot.dance(seconds);
    }
}

我想重构 API,以便只有一种方法,operate()。假设我不想通过 RMI 返回对机器人对象的实际引用,因为我有时需要执行多个操作,并且不希望往返。

我想出了这个,基于 Callable的概念:

public abstract class RobotCallable<T> {
    protected Robot robot;

    public void setRobot(Robot robot) {
        this.robot = robot;
    }

    public abstract T call()
}

public class RobotController implements RMIRobotController {
    private Robot robot;

    ...

    public T operate(RobotCallable<T> robotCallable) {
        robotCallable.setRobot(robot)
        return robotCallable.call();
    }
}

我这些东西通常是如何实现的? RMI 是否有某种机制可以自动执行此操作?

I have a remote object exposed through RMI, which I use to perform several things on a resource that the object holds. Right now I have method for each operation I need to perform.

Example:

public interface IRobotController {
    public int walk(int meters);
    public void dance(int seconds);
}

public interface RMIRobotController implements IRobotController, java.rmi.Remote {}

public class RobotController implements RMIRobotController {
    private Robot robot;

    ...

    public int walk(int meters) {
        return robot.walk(meters);
    }

    public void dance(int seconds) {
        robot.dance(seconds);
    }
}

I would like refactor the API so that I would only have one method, operate(). Assume I don't want to return an actual reference to the robot object through RMI, because I sometimes need to perform more than one operation, and don't want the round trips.

I came up with this, based on the concept of Callable<T>:

public abstract class RobotCallable<T> {
    protected Robot robot;

    public void setRobot(Robot robot) {
        this.robot = robot;
    }

    public abstract T call()
}

public class RobotController implements RMIRobotController {
    private Robot robot;

    ...

    public T operate(RobotCallable<T> robotCallable) {
        robotCallable.setRobot(robot)
        return robotCallable.call();
    }
}

I this how these things are usually implemented? Is there some mechanism for RMI that does this automatically?

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

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

发布评论

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

评论(2

等风也等你 2024-09-13 09:04:29

您尝试做的事情称为命令模式。
使用这种模式的目的是减少耦合。

话虽如此,这实际上不会发生,因为每次进行更改和编译时,序列化 ID 都不会匹配。

拨打电话时您会失去类型安全性。

您必须实现自己的消息格式,在其中放置有效负载和消息编组,然后在到达时对其进行解组(现在通常使用 XML 完成)。

基本上,通过将命令模式与 RMI 结合使用,您将失去 RMI 的所有优点,而仅将其用作传输。

如果这是您的目标,那么 Web 服务将是一个更好的选择。

What you are trying to do is called the Command Pattern.
The purpose to use this pattern is to reduce coupling.

That being said that is not really going to happen since your Serialization IDs will not match every time you make a change and compile.

You loose type safety when making your calls.

You have to implement your own message format, in which you put your payload and message marshall and then unmarshall it on arrival (This is typically done with XML these days).

Basically by using the Command Pattern with RMI, you loose all the benefits of RMI and just use it as a transport.

If this is your goal then WebServices would be a far better way to go.

残疾 2024-09-13 09:04:29

为什么要将接口精简为一个API?在接口中支持多个远程方法的成本确实很低:您只需声明它们、编写它们并调用它们。没有任何 RMI 开销。无论如何,您都必须编写代码来执行每个操作,加上从操作()发送到所需代码段的代码,而拥有单独的方法对您来说毫无意义。结果更加清晰。

Why do you want to reduce the interface to one API? There's really little cost in supporting multiple remote methods in an interface: you only have to declare them, write them, and call them. No RMI overheads whatsoever. And you have to write the code to do each operation anyway, plus the code to despatch from operation() to the desired piece of code, which having separate methods would have done for you for nothing. And the result is much clearer.

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