RMI绑定同一对象两次但呈现不同的接口

发布于 2024-08-17 16:12:07 字数 428 浏览 4 评论 0原文

嘿伙计们,我在这里给你们带来了一个有趣的!

我有一个名为 Server 的对象,它实现了两个 RMI 接口:CSCP 和 ISCP。 我需要我的客户端能够在 RMI CSCP 接口上进行通信,但对 ISCP 接口一无所知,并且我需要其他服务器在 ISCP 接口上与其进行通信,但对 CSCP 接口一无所知。到目前为止和我在一起吗?

基本上,目前我已将其设置为绑定两次,一次绑定到 rmiregistry 中的“ISCP”,一次绑定到“CSCP”。

然而,当客户端尝试绑定时(记住他们只知道 CSCP 接口),他们会得到一个异常,说他们找不到 ISCP 类 - 但他们应该不需要它。

那么,如何让一个对象(服务器)在两个不同的 rmi 绑定上呈现两个不同的 RMI 接口,并使它们保持分离?

如果你能为我解决这个问题,你就是一个天才:D 如果我不够清楚,请告诉我!

Hey guys, I have an interesting one for you here!

I have one object, called Server, that implements two RMI interfaces, CSCP and ISCP.
I need my Clients to be able to communicate on the RMI CSCP interface, but know nothing of the ISCP interface, and I need other servers to communicate with it on the ISCP interface but know nothing of the CSCP interface. With me so far?

Basically, at the moment I have it set up so that it binds twice, once to "ISCP" in the rmiregistry, and once to "CSCP".

However, when clients try to bind (bear in mind they only know the CSCP interface), they get an exception saying they cannot find the class ISCP - but they should have NO need for it.

So, how do have one object (Server) present two different RMI interfaces on two different rmibindings, keeping them separate?

You're a genius if you can solve this one for me :D If I wasn't clear enough let me know!

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

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

发布评论

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

评论(2

浅沫记忆 2024-08-24 16:12:07

我建议编写两个适配器类,一个实现 ICSP,一个实现 CSCP。这些适配器类中的每个方法都调用“真实”对象中的适当方法。

然后,您可以使用不同的名称将每个适配器类绑定到 RMI,而不是绑定原始对象。客户端可以根据他们拥有的接口检索他们想要的任何绑定对象。

即使只有一个接口,这也是一种很好的做法,因为将业务对象与它们通信的远程传输机制(本例中为 RMI)分离通常是一个好主意。必须导出两个远程接口使这种情况变得更加强大。

I suggest write two adapter classes, once which implements ICSP, and one which implements CSCP. Each method in these adapter classes calls the appropriate method in the "real" object.

You then bind each of these adapter classes to RMI, under a different name, instead of binding the original object. Clients can retrieve whichever bound object they want, according to which interface they have.

Even if there only one interface, this would be good practise anyway, since it's usually a good idea to decouple your business objects from the remote transport mechanism they communicate with (RMI in this case). Having to export two remote interfaces makes this case even stronger.

神回复 2024-08-24 16:12:07

尝试使用 Spring 远程处理使用不同的接口以不同的名称绑定同一个对象 - 它使用反射来绑定任何 Java 对象(即不需要实现Remote< /code>) 与查找和调用方法类似。

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="ICSP"/>
  <property name="service" ref="myService"/>
  <property name="serviceInterface" value="example.ICSP"/>
</bean>

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="CSCP"/>
  <property name="service" ref="myService"/>
  <property name="serviceInterface" value="example.CSCP"/>
</bean>

这些可以仅使用 Spring 库以编程方式使用:

RmiServiceExporter e = new RmiServiceExporter();
e.setServiceName("SCSP");
e.setService(myServiceObj);
e.setServiceInterface(example.SCSP.class);
e.prepare(); // read the doc; I'm not sure this is the exact method

Try using Spring remoting and bind the same object under different names using different interfaces - it uses reflection to bind any Java object (i.e. does not need to implement Remote) and similarly to lookup and invoke methods.

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="ICSP"/>
  <property name="service" ref="myService"/>
  <property name="serviceInterface" value="example.ICSP"/>
</bean>

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="CSCP"/>
  <property name="service" ref="myService"/>
  <property name="serviceInterface" value="example.CSCP"/>
</bean>

These can be used programmatically using just the Spring libraries:

RmiServiceExporter e = new RmiServiceExporter();
e.setServiceName("SCSP");
e.setService(myServiceObj);
e.setServiceInterface(example.SCSP.class);
e.prepare(); // read the doc; I'm not sure this is the exact method
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文