如何使数组列表可远程访问

发布于 2024-10-18 20:29:48 字数 996 浏览 5 评论 0原文

我有一个项目,其中使用 Java RMI 使其他对象可以远程访问对象。我需要将以下类设为远程:

public interface MarketBB extends Remote
{
   public ArrayList<CloudEntry> getMarketBB() throws RemoteException; 
   public void moveAMP(int fromCloud, int toCloud) throws RemoteException;
}

我遇到的问题是,由于 ArrayList 保存着 CloudEntry 对象,因此当从另一个对象调用 getMarketBB 方法时,不会返回任何内容。

有没有办法使 CloudEntry 类的 ArrayList 可以远程访问?

以下是 CloudEntry 类的代码:

public class CloudEntryImpl implements CloudEntry {

    int cloudId;
    String cloudName;
    double speedGHz;
    double costPerGhzH;
    double commsCost;
    double commsTime;
    int noAMPs;

    //constructor, get and set methods for fields

}

和 CloudEntry 接口:

public interface CloudEntry extends Remote {

    public void setNoAmps(int noAmps) throws RemoteException;

    public String getCloudName() throws RemoteException;

    public String getCloudDetails() throws RemoteException;

}

I have a project where I am using Java RMI to make objects remotely accessible to other objects. I need to make the following class remote:

public interface MarketBB extends Remote
{
   public ArrayList<CloudEntry> getMarketBB() throws RemoteException; 
   public void moveAMP(int fromCloud, int toCloud) throws RemoteException;
}

The problem that I have is that because the ArrayList is holding CloudEntry objects, when the getMarketBB method is called from another object, nothing is returned.

Is there a way of making the ArrayList of CloudEntry classes remotely accessible?

Here is the code for the CloudEntry class:

public class CloudEntryImpl implements CloudEntry {

    int cloudId;
    String cloudName;
    double speedGHz;
    double costPerGhzH;
    double commsCost;
    double commsTime;
    int noAMPs;

    //constructor, get and set methods for fields

}

And the CloudEntry interface:

public interface CloudEntry extends Remote {

    public void setNoAmps(int noAmps) throws RemoteException;

    public String getCloudName() throws RemoteException;

    public String getCloudDetails() throws RemoteException;

}

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

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

发布评论

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

评论(2

山田美奈子 2024-10-25 20:29:48

您的 CloudEntryImpl 不可序列化。尝试将其更改为:

public class CloudEntryImpl extends UnicastRemoteObject implements CloudEntry {
    //...
}

Your CloudEntryImpl is not serializable. Try changing it to:

public class CloudEntryImpl extends UnicastRemoteObject implements CloudEntry {
    //...
}
最单纯的乌龟 2024-10-25 20:29:48

getMarketBB() 返回 ArrayList 的副本。您不能让它返回列表的“实时”视图。

相反,我建议您提供与您尝试对列表执行的操作相匹配的方法。恕我直言,无论有没有 RMI,这都是最佳实践。

public void addCloudEntry(CloudEntry ce);
public CloudEntry getCloudEntry(int i);

getMarketBB() returns a copy of the ArrayList. You cannot have it return a "live" view of the list.

Instead I would suggest you provide methods which match what you are trying to do with the list. IMHO This is best practice with or without RMI.

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