The Artima Developer Community
Sponsored Link

Java Answers Forum
Help on Java Event !!

8 replies on 1 page. Most recent reply: Dec 7, 2004 10:08 PM by Gunasekhar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Gunasekhar

Posts: 7
Nickname: dguna
Registered: Nov, 2004

Help on Java Event !! Posted: Dec 1, 2004 10:53 PM
Reply to this message Reply
Advertisement
Hi all...............

I have developed a standalone java application.
I started my application and its running. Now when i terminate the application say from linux command prompt I will call kill command and terminate my application, i want to perform few tasks before the application is killed.

Can any one tell me, what java event is raised when a application is terminated like above stated?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help on Java Event !! Posted: Dec 2, 2004 1:00 AM
Reply to this message Reply
Don't use the kill command.

kill terminates any process immediatly, it does not "close" a program.

Gunasekhar

Posts: 7
Nickname: dguna
Registered: Nov, 2004

Re: Help on Java Event !! Posted: Dec 2, 2004 1:19 AM
Reply to this message Reply
Thanks for your response..
Let me be more detail on why i wanted this.
It is a Client/Server application. Server is in a loop, keeps accepting client requests and waits for another client.
After client connected and work of client is over, client has to tell server that it is closing the socket. This i can achieve on close of window event.

Now due to other reasons also application may get terminated. In any case i wanted to close the client socket. So that accordingly server will be notified about the same.

If i could somehow find this system level event, and whether it is possible to trap that event in java???

I appreciate for your response

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help on Java Event !! Posted: Dec 4, 2004 1:47 AM
Reply to this message Reply
The classical approach for this problem uses a different system.

First try using a "Heartbeat". The server checks on a regular basis if the client still responds.
If it doesn't respond it will be removed from the list.



You say that you send the term-signal to the server when the window get's closed.
You should not do it this way. Instead call a thingsToDoWhenIWantToCloseTheProgram() - Method.
Call the same method on your other close-buttons.

I don't think there exists a event wich gets fired when the application get's closed.

I wouldn't know such a event for other program languages as well.
All events I know refer to the program window.
In all other cases a thingstoDo ... is used.

varanasi R eswar

Posts: 3
Nickname: eswar
Registered: Nov, 2003

Re: Help on Java Event !! Posted: Dec 6, 2004 4:56 AM
Reply to this message Reply
Hi ,
this is eshwar..., i can help u by sugesting to add exceptions try...catch...final..., where there is an opportuinty of getting thigs..added at the end of any operation my be at the end of the prog...


or use the runtime property ... shutdownhook() function...
if u want i can get u the sample prog...

Runtime r = new Runtime();

r.addShutDownHook(thread..);

ok try this...

best
V.R.Eshwar,

Gunasekhar

Posts: 7
Nickname: dguna
Registered: Nov, 2004

Re: Help on Java Event !! Posted: Dec 6, 2004 6:13 AM
Reply to this message Reply
Hi Eshwar

Thanks for your response...

Could you be more in brief technically on how it can be implemented ( may be a full sample program)
or atleast steps to do it.

I would appreciate for your response

regards
Gunasekhar

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Help on Java Event !! Posted: Dec 6, 2004 8:39 PM
Reply to this message Reply
Hi,

this code will help you to understand it better. This code print the value of counter after every 5 seconds. Press Ctrl+C while program is running. This terminate the program and the shutdownhook will be called.
Read the java documentation for more detail on addShutDownHook().
public class ShutDownHookEx
{
    public ShutDownHookEx()
    {
        // add a shutdown hook. this will be called when the
        // JVM will terminate conditionally or unconditionally.
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            public void run()
            {
                System.out.println("from shutdown hook");
            }
        });
    }
 
    public void startLoop()
    {
        // a counter thread, waits for 5 seconds and prints value of n 
        Thread t = new Thread()
        {
            public void run()
            {
                int n = 0;
                while (n < 1000)
                {
                    try
                    {
                        sleep(5000);
                        System.out.println("n : " + n);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    n++;
                }
            }
        };
        t.start();
    }
 
    public static void main(String[] args)
    {
        ShutDownHookEx sdhe = new ShutDownHookEx();
        sdhe.startLoop();
    }
}

Regards,
Amol Brid.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Help on Java Event !! Posted: Dec 6, 2004 8:44 PM
Reply to this message Reply
Hi varanasi,

you cannot create instance of Runtime class. getInstance() method of Runtime class returns the only instance of this class.

Regards,
Amol Brid.

Gunasekhar

Posts: 7
Nickname: dguna
Registered: Nov, 2004

Re: Help on Java Event !! Posted: Dec 7, 2004 10:08 PM
Reply to this message Reply
Hi Amol

I tried running your sample program code, it runs without any problem, displays counter value after every 5 secs but on terminating the program it doesnt print "from shutdown hook", its not working.

I run this program from linux. Does your code have any os dependent issues


regards
Gunasekhar

Flat View: This topic has 8 replies on 1 page
Topic: iterator infinite loop Previous Topic   Next Topic Topic: Sending outputs to JTextArea

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use