简体   繁体   English

Java RMI:如何传递POJO

[英]Java RMI: How to pass a POJO

I successfully created an RMI service and client. 我成功创建了一个RMI服务和客户端。 I can call methods and so on. 我可以调用方法等。 But now I wanted to try the following: I wanted a standard Java object like a LinkedList to be hosted on the service. 但现在我想尝试以下内容:我希望在服务上托管像LinkedList这样的标准Java对象。 Also, I wanted to 'pretend' I already had existing code that uses a LinkedList. 此外,我想“假装”我已经拥有使用LinkedList的现有代码。 What I want is to get a LinkedList that is actually managed by the service, but that I can access locally just like it was a normal LinkedList. 我想要的是获得一个实际由服务管理的LinkedList,但我可以在本地访问,就像它是一个普通的LinkedList一样。 On top of that I want to do some minimal logging, like if .add() is called it writes on the server: "Add called". 最重要的是,我想做一些最小的日志记录,就像调用.add()一样,它在服务器上写入:“Add called”。

This is not meant for production, just to help me understand how it works! 这不是为了生产,只是为了帮助我理解它是如何工作的!

So far I've tried a lot of things. 到目前为止,我已经尝试了很多东西。 The most promising is that I have created a class that extends LinkedList and implements Remote. 最有希望的是我创建了一个扩展LinkedList并实现Remote的类。 This class tries to register itself with the Registry in the constructor like this: 这个类试图在构造函数中使用Registry注册自己,如下所示:

try {

UnicastRemoteObject.exportObject((Remote)this); Naming.rebind("theList", (Remote)this); } catch (Exception e) { System.out.println("fail"); System.out.println(e.getMessage()); }

I have to do this because I need to extend LinkedList, thus I cannot extend UnicastRemoteObject. 我必须这样做,因为我需要扩展LinkedList,因此我无法扩展UnicastRemoteObject。

The output I get when I try to run this, on the server side: 我在服务器端尝试运行时获得的输出:

fail
Connection refused to host: 192.168.178.27; nested exception is: 
 java.net.ConnectException: Connection refused

And on the client side: 在客户端:

java.lang.ClassCastException: MyList_Stub cannot be cast to java.util.LinkedList
 at $Proxy0.createList(Unknown Source)
 at RemoteProgram.main(RemoteProgram.java:27)

Thanks in advance! 提前致谢!

What you are trying to do is very inefficient and not a very good idea. 你要做的是非常低效,而不是一个好主意。 Basically you can send anything in a method invocation that you can serialize. 基本上,您可以在可以序列化的方法调用中发送任何内容。 If you want good performance, I would suggest that you only have one remote object that represents your service and acts as a facade for all the services that you need (each remote object results in separate file descriptor, so having lots of remote objects is typically not a good idea). 如果你想要良好的性能,我建议你只有一个代表你的服务的远程对象,并作为你所需的所有服务的外观(每个远程对象产生单独的文件描述符,因此通常有很多远程对象)不是一个好主意)。 Additionally, if you are frequently adding and removing objects, then sending a message every time you add or remove an element is not really sensible. 此外,如果您经常添加和删除对象,则每次添加或删除元素时发送消息都不是很明智。 I would suggest having a single remote object with the following two very simple methods: 我建议使用以下两个非常简单的方法来拥有一个远程对象:

LinkedList retrieveLinkedListByName(String);
boolean commitNewVersionOfLinkedListByName(String,LinkedList);

On application startup, you can download the linked list, and then at regular intervals and at application exit, you can send back the linked list. 在应用程序启动时,您可以下载链接列表,然后定期和在应用程序退出时,您可以发回链接列表。 That should be more efficient then using the network every time you add or remove an element to your linked list. 每次在链接列表中添加或删除元素时,这应该比使用网络更有效。 As long as the elements of your LinkedList are serializable, you don't need to do any magic (like extending remote) for it be sent. 只要LinkedList的元素是可序列化的,您就不需要为它发送任何魔法(如扩展远程)。

java.lang.ClassCastException: MyList_Stub cannot be cast to java.util.LinkedList
at $Proxy0.createList(Unknown Source)
at RemoteProgram.main(RemoteProgram.java:27)

LinkedList is a concrete class, RMI works with interfaces so you should be casting to the List interface on the client side. LinkedList是一个具体的类,RMI与接口一起工作,因此您应该转换到客户端的List接口。

Agree with the other posters, this is a very inefficient design. 同意其他海报,这是一个非常低效的设计。

As for your exceptions, I don't see know how you can get a 'connection refused' in the server and still be able to run the client. 至于您的例外,我不知道如何在服务器中获得“拒绝连接”并仍然能够运行客户端。 You would need to post the stack trace. 您需要发布堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM