简体   繁体   English

是java RMI远程对象(服务器)单例吗?

[英]Are java RMI remote objects (server) singleton?

I have been using java RMI for a while now but I couldn't figure out if the RMI Remote Stubs (on the server side) are singleton? 我一直在使用java RMI一段时间但我无法弄清楚RMI Remote Stubs(在服务器端)是否是单例? The reason I ask is: 我问的原因是:

lets assume that one of the RMI implementation methods lower down in the chain of calls have a synchronized method. 假设调用链中较低的RMI实现方法之一具有synchronized方法。 If for some reason the logic in the Synchronized Method is messed up (or hangs), the future RMI calls (from the client) will hang too while trying to get access to that synchronized method. 如果出于某种原因,同步方法中的逻辑混乱(或挂起),则在尝试访问该同步方法时,未来的RMI调用(来自客户端)也会挂起。 This will hold true only if the RMI stubs are going to be singleton. 只有当RMI存根将成为单例时,这才会成立。 If a new object is created on the server side at every remote call from the client, this won't be a problem because than the methods are being called from a different object and synchronized method won't be an issue anymore. 如果在客户端的每次远程调用时在服务器端创建一个新对象,这将不会成为问题,因为从另一个对象调用方法并且同步方法将不再是问题。

Long story short. 长话短说。 I am trying to understand how JVM internally maintains rmi remote objects on the server side and if they are singleton. 我试图了解JVM如何在服务器端内部维护rmi远程对象以及它们是否是单例。 I tried many different javadocs but they don't explicitly mention this anywhere. 我尝试了许多不同的javadoc,但他们没有在任何地方明确提到它。

Any and all help is appreciated ! 任何和所有的帮助表示赞赏!

EDIT Based on some questions and comments, I am refining the question: my real question is, does RMI on the server side happen to keep some kind of an object pool based on what one object you export and register ? 编辑基于一些问题和评论,我正在提炼这个问题:我真正的问题是,服务器端的RMI是否会根据您导出和注册的对象保留某种对象池? Can you bind more than one object of the same type with the same name (somewhat simulating an object pool where RMI can give me any of the objects that I registered) or in order to have multiple instances of the same object, I will have to register them with different names 你能绑定多个具有相同名称的同一类型的对象(有些模拟一个对象池,其中RMI可以给我任何我注册的对象),或者为了拥有同一个对象的多个实例,我将不得不用不同的名字注册它们

First of all, the "stub" is a client-side concept, there are no stubs on the server. 首先,“存根”是一个客户端概念,服务器上没有存根。

As for the remote objects themselves, the RMI system doesn't instantiate the objects for you, it's up to you to create instances and export them. 对于远程对象本身,RMI系统不会为您实例化对象,您可以创建实例并导出它们。 You create one instance of the object, export that object, and bind it in the registry under a particular name. 您创建对象的一个​​实例,导出该对象,并在特定名称下将其绑定在注册表中。 All calls on client stubs obtained from that same name in the registry will ultimately end up at the same object on the server. 在注册表中从同一名称获取的客户端存根上的所有调用最终都将终止于服务器上的同一对象。

Can you bind more than one object of the same type with the same name (somewhat simulating an object pool where RMI can give me any of the objects that I registered) 你能绑定多个具有相同名称的同一类型的对象(有点模拟一个对象池,其中RMI可以给我任何我注册的对象)

No, you can only bind one object in the registry under a given name. 不,您只能在给定名称下绑定注册表中的一个对象。 But the object you bind could itself be a proxy to your own object pool, for example using the Spring AOP CommonsPoolTargetSource mechanism. 但是绑定的对象本身可以是您自己的对象池的代理,例如使用Spring AOP CommonsPoolTargetSource机制。

Stubs are not singletons, but your question is really about the server-side objects. 存根不是单例,但你的问题实际上是服务器端对象。 They are not singletons either, unless you implement them that way yourself. 他们也不是单身人士,除非你自己以这种方式实施。 RMI doesn't do anything about that whatsoever. RMI对此无动于衷。

EDIT Based on some questions and comments, I am refining the question: my real question is, does RMI on the server side happen to keep some kind of an object pool based on what one object you export and register? 编辑基于一些问题和评论,我正在提炼这个问题:我真正的问题是,服务器端的RMI是否会根据您导出和注册的对象保留某种对象池?

No. 没有。

Can you bind more than one object of the same type with the same name 是否可以使用相同的名称绑定多个相同类型的对象

No. 没有。

I will have to register them with different names 我将不得不用不同的名字注册它们

You don't have to register them at all. 您根本不需要注册它们。 You need one singleton remote object bound into the Registry: consider that as a factory method for further remote objects, which are returned as results from its remote methods. 您需要将一个单独的远程对象绑定到注册表中:将其视为其他远程对象的工厂方法,这些远程对象作为远程方法的结果返回。 For example, a remote Login object is bound in the Registry and has a single login() method that returns a remote session object, a new one per login, with its own API. 例如,一个远程Login对象绑定在Registry中,并且有一个login()方法,该方法返回一个远程会话对象,每个登录一个新对象,并带有自己的API。

RMI its based on proxy design pattern. RMI基于代理设计模式。

See what says here 看看这里说的是什么

A RMI Server is an application that creates a number of remote objects. RMI服务器是一个创建许多远程对象的应用程序。 An RMI Server is responsible for: RMI服务器负责:

  1. Creating an instance of the remote object (eg CarImpl instance = new CarImpl()); 创建远程对象的实例(例如CarImpl instance = new CarImpl());
  2. Exporting the remote object; 导出远程对象;
  3. Binding the instance of the remote object to the RMI registry. 将远程对象的实例绑定到RMI注册表。

From the Java docs: 来自Java文档:

http://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch3.html http://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch3.html

A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. 由RMI运行时调度到远程对象实现的方法可能在或可能不在单独的线程中执行。 The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads. RMI运行时无法保证将远程对象调用映射到线程。 Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe. 由于同一远程对象上的远程方法调用可以并发执行,因此远程对象实现需要确保其实现是线程安全的。

Yes, the server side method is synchronized. 是的,服务器端方法是同步的。 The implementation is platform-specific. 该实现是特定于平台的。 You cannot assume anything else about threading. 你不能假设任何关于线程的东西。 And you certainly cannot assume whether or not the remote object is a singleton. 你当然不能假设远程对象是否是单身人士。

Also, it might be useful to look at Remote Object Activitation: 此外,查看远程对象激活可能很有用:

http://docstore.mik.ua/orelly/java-ent/jenut/ch03_06.htm http://docstore.mik.ua/orelly/java-ent/jenut/ch03_06.htm

http://docs.oracle.com/javase/7/docs/api/java/rmi/activation/package-summary.html http://docs.oracle.com/javase/7/docs/api/java/rmi/activation/package-summary.html

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

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