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

2

1

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?

Vestibule answered 29/9, 2009 at 20:7 Comment(1)
How can I make it possible- that there should be only one instance.Vestibule
A
2

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)

Almost answered 29/9, 2009 at 20:10 Comment(3)
Thanks for reply. FYI its an Axis2 service. Lets assume both these services are running on same machine. How can I make it possible- that there should be only one instance.Vestibule
As I said, using some sort of java remoting, coded as what would be java equivilent of .Net MarshallByRef Singleton. (One service's code must host the singleton, and the other one must "use" it even though it "lives in the the first one.Almost
'java equivilent of .Net MarshallByRef Singleton' - that i will have to find out.Vestibule
L
0

I'm not familiar with the details of how Java Web services are run, but if they are both running in the same VM then I think the classes would be shared across all applications in the VM and static fields would therefore be shared. Since the Singleton pattern is usually accomplished by attaching a single instance to a static member, the Singleton would be shared.

This based on: The Clean Code Talks - "Global State and Singletons"

You should be able to test it out by writing two simple Web services. One that does something to the singleton, such as set a flag, and another that checks for it.

Lydia answered 29/9, 2009 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.