如何在Java中创建跨进程的Singleton类 [英] How to create a cross-process Singleton class in Java

查看:94
本文介绍了如何在Java中创建跨进程的Singleton类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个通用的Singleton类,在任何给定的时间,只有一个实例在多个Java进程之间共享?

Is is possible to create a universal Singleton class, which is, at any given time, only one instance is shared across multiple Java processes?

推荐答案

多个Java进程不共享同一个虚拟机。

Multiple Java processes don't share the same virtual machine.

所以你最终会得到一个托管单例的JVM实例,然后是一个JVM实例使用远程方法调用访问单例的每个进程为@Little Bobby Tables建议。

So you would end up with one JVM instance hosting the singleton, then one JVM instance per process which access the singleton using Remote Method Invocation as @Little Bobby Tables suggested.

无论如何要考虑什么时候是Singleton而不是Singleton


两个或多个虚拟机中的多个单例

当Singleton类的副本在多个VM中运行时,将为每台计算机创建一个实例。每个VM都可以拥有自己的Singleton似乎很明显,但是在分布式系统中,例如那些使用EJB,Jini和RMI的系统,并不是那么简单。由于中间层可以隐藏分布式技术,因此可能难以确定对象实际实例化的位置。

When copies of the Singleton class run in multiple VMs, an instance is created for each machine. That each VM can hold its own Singleton might seem obvious but, in distributed systems such as those using EJBs, Jini, and RMI, it's not so simple. Since intermediate layers can hide the distributed technologies, to tell where an object is really instantiated may be difficult.

例如,只有EJB容器决定如何以及何时创建EJB对象或回收现有对象。 EJB可能存在于与调用它的代码不同的VM中。此外,可以在多个VM中同时实例化单个EJB。对于无状态会话bean,对您的代码显示为一个实例的多次调用实际上可能是对不同VM上的不同实例的调用。甚至可以通过调用之间的持久性机制来保存实体EJB,这样您就不知道哪个实例会回答您的方法调用。 (作为实体bean规范的一部分的主键是正确的,因为引用标识在标识bean时没有用。)

For example, only the EJB container decides how and when to create EJB objects or to recycle existing ones. The EJB may exist in a different VM from the code that calls it. Moreover, a single EJB can be instantiated simultaneously in several VMs. For a stateless session bean, multiple calls to what appears, to your code, to be one instance could actually be calls to different instances on different VMs. Even an entity EJB can be saved through a persistence mechanism between calls, so that you have no idea what instance answers your method calls. (The primary key that is part of the entity bean spec is needed precisely because referential identity is of no use in identifying the bean.)

EJB容器的传播能力如果您尝试在EJB的上下文中编写Singleton,则跨多个VM的单个EJB实例的标识会导致混淆。 Singleton的实例字段不是全局唯一的。因为看起来是同一个对象涉及多个VM,所以可能会出现几个Singleton对象。

The EJB containers' ability to spread the identity of a single EJB instance across multiple VMs causes confusion if you try to write a Singleton in the context of an EJB. The instance fields of the Singleton will not be globally unique. Because several VMs are involved for what appears to be the same object, several Singleton objects might be brought into existence.

基于分布式技术的系统,如EJB,RMI和吉尼应该避免拥有国家的单身人士。不拥有状态但只是控制对资源的访问的单例也不适合EJB,因为资源管理是EJB容器的角色。但是,在其他分布式系统中,可以使用控制资源的Singleton对象,因为它们在分布式系统中并不是唯一的,只是在特定的VM中。

Systems based on distributed technologies such as EJB, RMI, and Jini should avoid Singletons that hold state. Singletons that do not hold state but simply control access to resources are also not appropriate for EJBs, since resource management is the role of the EJB container. However, in other distributed systems, Singleton objects that control resources may be used on the understanding that they are not unique in the distributed system, just in the particular VM.

这篇关于如何在Java中创建跨进程的Singleton类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆