How to send a kill signal?

If somoene closes down the MSDOS window or simply exits the program by clicking the "x", is it possible to send a "kill signal"? If it is possible, then how? I'm trying to create a online application users list, so could anyone help?

[241 byte] By [Petsa] at [2007-11-27 10:56:10]
# 1

> If somoene closes down the MSDOS window or simply

> exits the program by clicking the "x", is it possible

> to send a "kill signal"? If it is possible, then how?

> I'm trying to create a online application users list,

> so could anyone help?

Are you looking for Runtime's shutdown hooks? I mean, a kill signal as I understand it would be a signal to a process to get killed, not a message *from* the process.

CeciNEstPasUnProgrammeura at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 2

> Are you looking for Runtime's shutdown hooks? I mean,

> a kill signal as I understand it would be a signal to

> a process to get killed, not a message *from* the

> process.

No, exactly the opposite. I need to know when a user closes the proccess so I can send one last string of data to a webserver or perform one last action (e.g. send a string to the webserver that the user has logged off from the application).

Petsa at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 3

> No, exactly the opposite. I need to know when a user

> closes the proccess so I can send one last string of

> data to a webserver or perform one last action (e.g.

> send a string to the webserver that the user has

> logged off from the application).

Did I mention shutdown hooks?

CeciNEstPasUnProgrammeura at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 4

public void addShutdownHook(Thread hook)

I searched it when you mentioned it, but I can't make out how to use it.

Petsa at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 5

Although it sounds to me like you could just as well implement a normal session handling, invalidating a session after a timeout or on log-off. To keep the sessions alive, you could send a small "heartbeat" signal every 20 secs or so, depending on what exactly your requirements are.

CeciNEstPasUnProgrammeura at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 6

You're looking for Runtime's shutdown hooks... also useful for last chance cleanup (in a pinch).

corlettka at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 7

> I searched it when you mentioned it, but I can't make

> out how to use it.

When you shutdown the JVM, the thread you provide there will be executed. So it could e.g. send the message to your server.

CeciNEstPasUnProgrammeura at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 8

public void addShutdownHook(Thread hook) {}

public class hook extends Thread

{

public void run()

{

//code here

}

hook()

{

start();

}

}

So like that?

Petsa at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 9

> > public void addShutdownHook(Thread hook) {}

>

> public class hook extends Thread

> {

> public void run()

> {

> //code here

> }

> hook()

> {

> start();

> }

>

>

> So like that?

That does not work :/ Any ideas on what I'm doing wrong?

Petsa at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 10

Added 10 dukes, anyone help now :) ?

Petsa at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 11

Forget the dukes and add a problem description instead. And don't call start() in the c'tor.

CeciNEstPasUnProgrammeura at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 12

> Forget the dukes and add a problem description

> instead. And don't call start() in the c'tor.

I tested the code with System.out.println("HI"); and when I X'ed out of the Java application it did not print HI in the MSDOS window. I'm assuming this is because the code is faulty, yet I have no idea what I'm doing wrong.

I tested it without start() also.

Petsa at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...
# 13

Works for me!

import javax.swing.*;

class SampleShutdownHook implements Runnable {

public void run() {

System.out.println("Hello from SampleShutdownHook");

}

}

public class ShutdownHookExample implements Runnable {

public void run() {

//create gui

JFrame f = new JFrame();

f.setSize(200, 100);

f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

public static void main(String[] args) {

Runtime.getRuntime().addShutdownHook(new Thread(new SampleShutdownHook()));

SwingUtilities.invokeLater(new ShutdownHookExample());

}

}

Hippolytea at 2007-7-29 12:01:22 > top of Java-index,Java Essentials,Java Programming...