Search code examples
web-servicesclasssingletonsoajava-ws

what is a singleton class? Can it help me running single instance of a class for two related services?


This might sound complex but i will ask anyway:

I am running a service A which uses class X. I want to start another service B which uses classes A besides new classes.

Service A is already running. I do a hot deployment of Service B.

Here is the real question - Will Service B use the same instance of class X or a separate instance. How can singleton class help me here?


Solution

  • Each Service will run in it's own operating System (OS) Process space, and each process space has it's own class instances. A "singleton" class is normally coded using static fields in a class, which would be local to the process space the code was running in, so no, they will not share singletons. Each will get it's own instance.

    You can do what you are trying to do, however, using some external shared synchronization process, (for example, exposing a singleton over whatever the java equivilent is to .Net Remoting (or WCF) - a network exposed endpoint which is coded to use a Singleton, and have both your services "connect to" and use that remotely accessible Singleton)