Help with singleton!!!
Hi,
I did read the singleton topic. But it stays confuse to me.
How to garantee that only one instance of Class per VM ?
I am building a component to stay running forever doing some tasks... From command line this can be started and stopped..
I am trying to implement a class, a singleton. Some controls like isInShutdown() and isWorking() are definied according the runtime events
I am starting from one prompt, and trying to stop in other prompt. The stop does not work.
Can anyone help me, showing the basic stpes to do this . If possible with some pieces of code?
thanks
[623 byte] By [
Galadriela] at [2007-10-2 22:48:41]

> I am building a component to stay running forever
> doing some tasks... From command line this can be
> started and stopped..
>
> I am trying to implement a class, a singleton. Some
> controls like isInShutdown() and isWorking() are
> definied according the runtime events
>
> I am starting from one prompt, and trying to stop in
> other prompt. The stop does not work.
>
>
> Can anyone help me, showing the basic stpes to do
> this . If possible with some pieces of code?
How about usnig java.net API?
For example server can listen to incoming clients, handle their requests
and send out its responses.
Hi raindrop,
Thanks for your response.
The major doubt is about how to guarantee only one instance of a class inside JVM.
So, the service can not be initialized 2 times in the same VM.
If another call for the getInstance() of the class is placed, we can do nothing or display a message like "the service is already running". Independent of from which terminal/command line the getInstance is called.
regards
Try here: http://www-128.ibm.com/developerworks/java/library/j-dcl.html?loc=jAnd here: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
> The major doubt is about how to guarantee only one
> instance of a class inside JVM.
>
> So, the service can not be initialized 2 times in the
> same VM.
>
The simplest Singleton is:public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
// Singleton initialization code
}
public static Singleton getInstance(){
return INSTANCE;
}
}
Nobody can create new instance of this Singleton because it
has private constructor.
> If another call for the getInstance() of the class is
> placed, we can do nothing or display a message like
> "the service is already running". Independent of
> from which terminal/command line the getInstance is
> called.
Which way do you want to connect from terminal to
the desired VM?
> > The major doubt is about how to guarantee only one
> > instance of a class inside JVM.
> >
> > So, the service can not be initialized 2 times in
> the
> > same VM.
> >
>
> The simplest Singleton is
>
> ...
>
> Nobody can create new instance of this Singleton
> because it
> has private constructor.
This will work in many situations but be aware that it doesn't guarantee there will only be one instance per VM. It only ensures one instance per classloader. If another ClassLoader loads the the class and that class is used, you will have another instance.
raindrop ,
It is started from command line(cmd other ssh session)... will perform ciclic operations and send information to other applications
The shutdown will be fired from other command line in the same computer ... i think this is the critical point.:
Based on dubwai comment:
"This will work in many situations but be aware that it doesn't guarantee there will only be one instance per VM. It only ensures one instance per classloader. If another ClassLoader loads the the class and that class is used, you will have another instance."
I have an question: Using mutiple command lines will invoke diferente classloaders in the same machine ?
regards
Hi,
Additional info. Just to clarify:
Command line 1 - java -jar MyApp.jar -start
Commando line 2 - java -jar MyApp.jar -stop
In main method the getInstance() is called and according the command line arg ( start or stop).. the private method lauch() or the private method shutdown() is called.
thanks a lot
> I have an question: Using mutiple command lines will> invoke diferente classloaders in the same machine ?Generally it will invoke muliple VMs.
> Hi,
>
> Additional info. Just to clarify:
>
> Command line 1 - java -jar MyApp.jar -start
>
> Commando line 2 - java -jar MyApp.jar -stop
>
> In main method the getInstance() is
> called and according the command line arg ( start or
> stop).. the private method lauch() or the
> private method shutdown() is called.
This starts two separate JVMs. Have you written anything to make these talk to each other?
> This will work in many situations but be aware that
> it doesn't guarantee there will only be one instance
> per VM. It only ensures one instance per
> classloader. If another ClassLoader loads the the
> class and that class is used, you will have another
> instance.
How about setting up appropriate SecurityManager?
It helps to avoid using undesirable ClassLoaders.
> > This will work in many situations but be aware
> that
> > it doesn't guarantee there will only be one
> instance
> > per VM. It only ensures one instance per
> > classloader. If another ClassLoader loads the the
> > class and that class is used, you will have
> another
> > instance.
>
> How about setting up appropriate SecurityManager?
> It helps to avoid using undesirable ClassLoaders.
I suppose that could be done. The only thing is that multiple classloaders are not always questionable. For example, in an app server.
dubwai,
> This starts two separate JVMs. Have you written anything to make these talk to each other?
No!
I think an option is to use some Inter-Process Communication (IPC)techniques
I can codify something to stay looking into a file for the shutdown "message"/flag. And some flag to tells to "file" readers if the service is started or not.
The ideal approach is something in memory.
Any suggestions about how to do this ?
regards
Try to read Interprocess communications in Java article: http://www.javaworld.com/javaworld/javaqa/2000-03/03-qa-0324-ipc.html
>
> Any suggestions about how to do this ?
I would probably use JMX. Run the appliction through the JMX Manager, then when you want to shutdown connect to the JMX socket and send the shutdown message. You can do this through Jconsole so you dont even have to write a shutdown application.