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

断了今生、忘了曾经 提交于 2019-12-04 21:20:20

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!