How to create a cross-process Singleton class in Java

前端 未结 2 1580
囚心锁ツ
囚心锁ツ 2020-12-10 16:21

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

相关标签:
2条回答
  • 2020-12-10 16:46

    Yes, but not without external facilities. The simplest way is to use RMI. Other options include CORBA or Web Services - Just google it up.

    0 讨论(0)
  • 2020-12-10 16:58

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

    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.

    Anyway consider When is a Singleton not a Singleton:

    Multiple Singletons in Two or More Virtual Machines

    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.

    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.)

    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.

    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.

    0 讨论(0)
提交回复
热议问题