Solved

How to pause applet closure in order to save data

Posted on 2001-08-09
10
755 Views
Last Modified: 2008-02-01
I am running a java applet in netscape 4.x. This applet is not embedded in a webpage. It opens in its own window.
If user select file -> exit in applet, I can programatically save data to database before closing the applet.

If the user either:

A:  clicks the 'X' in the upper right hand of the window
B: hits alt-f4 on the key or right clicks
C:  right-clicks on the window's button on the task bar and selects close

I cannot save application data before the applet closes.
The code necessary to save this data (to a database)is to extensive to execute completely in the stop() or destroy() methods.

Is there anyway to pause the applet shutdown in order to save data?

If a user tries to close ms word by any method other than force quitting w/o saving changes, word pauses the shutdown process to prompt user to save changes. I would like to do the same thing - except prompting the user - using a java applet in a netscape browser.
0
Comment
Question by:apatia
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6370837
Your code will have to run in stop() or destroy(), I don't know of any other option.
0
 
LVL 2

Expert Comment

by:Prasanna_Hebbar
ID: 6370980
I think it should be possible to do that using applet to javascript communication. (Using livewire). Write a method in the applet which returns the status whether the data has been saved. (Something like isDataSaved()).

You can write a javascript which will be called when the window is closed. This method will talk to the applet and checks if the data is saved. If the data is not saved, then does not close the window.

This is a vague solution. I remember reading about applet communication using livewire. I am not really confident about this solution. So you might want to study more about that before continuing.
0
 
LVL 4

Expert Comment

by:omry_y
ID: 6371379
as objects says, your code will have to execute in the destroy method (stop is a bad choice here, because netscape got a nasty habbit of stopping and starting a java applet when netscape is resized)
also, remember that you can`t do long tasks in the destroy method.
the JVM will just close on you.
also, know that explorer and netscape have different policies about how to handle applets that do long tasks on destroy, but I dont remember who is nicer.
I once tested it in the following way :
in the applet destroy, put a for loop that iterates 50 times, each time sleep 500 ms and do a Toolkit.beep().
now just see how many beeps your applet emits when you close the browser.
0
 
LVL 1

Accepted Solution

by:
dviji earned 300 total points
ID: 6373096
Hi apatia,
         If you call  your save routine in destroy that will work. But instead of driectly calling that if you create one thread and write your save routine it will be best. Very important is you have to do 'join()'..

i.e
public void init()
{
   ///
   MyThread thread = new MyThread();
}

public void destroy()
{
 thread.start();
 try
 {
   thr.join();
 }
 catch(Exception e)
 {
   e.printStackTrace();
 }

}
...
public class MyThread extends Thread
{
public void run()

}
[Need for join()
i.e once if the detroy method is called then first all the threads created for that window will die first.. if you do join() before that the user thread will run..
]

 I was tried to print simple text in console.. without thread it was printed like 150 times.. but with the thread it was printed more than 10000 times. And I also tried with sockets (juse created one small server) and it was works fine (in serverside if I write it into the file 13,550 times it was printed and with the console 7850 times). !!!!.

 I hope this is best way.. If you wants to store very small amount of data then it will be fine otherwise ... ;-(((. But these are all not 100% safe.
0
 
LVL 1

Expert Comment

by:dviji
ID: 6373105
Hi apatia,
         If you call  your save routine in destroy that will work. But instead of driectly calling that if you create one thread and write your save routine it will be best. Very important is you have to do 'join()'..

i.e
public class MyApplet extends Applet
{
public void init()
{
   ///
   MyThread thread = new MyThread();
}

public void destroy()
{
 thread.start();
 try
 {
   thread.join();
 }
 catch(Exception e)
 {
   e.printStackTrace();
 }

}
...
class MyThread extends Thread
{
  public void run()
  {
    save();
  }

}
}
[Need for join()
i.e once if the detroy method is called then first all the threads created for that window will die first.. if you do join() before that the user thread will run..
]

 I was tried to print simple text in console.. without thread it was printed like 150 times.. but with the thread it was printed more than 10000 times. And I also tried with sockets (juse created one small server) and it was works fine (in serverside if I write it into the file 13,550 times it was printed and with the console 7850 times). !!!!.

 I hope this is best way.. If you wants to store very small amount of data then it will be fine otherwise ... ;-(((. But these are all not 100% safe.

Best of luck... ;-)))

....dviji
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:apatia
ID: 6374062
Thanks everyone for your comments. I'll try all the suggestions and let you know how it goes.

As for putting the save code in the destroy() method, it just doesn't work. There's not enough time.
0
 
LVL 4

Expert Comment

by:omry_y
ID: 6374691
there is nothing you can do.
you may not do any time consuming operations when the JVM is beeing closed.
just do it earlier.
0
 
LVL 4

Expert Comment

by:omry_y
ID: 6381872
I fail to understand how the code in the accepted answer helps preventing the JVM from exiting.
0
 
LVL 2

Expert Comment

by:Prasanna_Hebbar
ID: 6384067
Even I didn't understand why another thread is created at the end? It is as good as doing it in the main thread. dviji, would you like to give explanation for that?

omry_y, I don't think jvm will be closed when the browser is closed. My understanding is that all the instances of browser share the same jvm. So closing one window should not shut down the JVM.

Thanks
0
 
LVL 4

Expert Comment

by:omry_y
ID: 6384124
On explorer, each explorer process have its own JVM.
an explorer process is created when you click the explorer icon.
if you create a new window through the explorer UI (open location, new window etc), a new process is NOT created, and thus the two windows indeed share the JVM.
the JVM is closed when the explorer process is closed, and this happens when the last explorer window of that process is closed.
but in this scenario, you must assume than when the user will close the browser, the JVM will be closed too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now