In my standalone GUI application, I will install a Windows Closed handle on the application
formWindowClosed
within the closed handle, I will perform necessary clean up, and did a final System.exit(0) at the every last code.
This works well, until I receive a complain from customer that the program is not able to shut down properly. My suspect is that, there are still pending I/O operation, which cannot even be interrupted by System.exit(0).
I wish to use Process.destroy() too, to give a final HIT to the application.
However, I am not sure how I can obtain current running process.
You might want to check out this similar question on SO.
Related
I am running a Java program that acts as a server for my program using CreateProcess with no window (using the settings shown here). I have an object which runs the process in its constructor, saves the process handle, and uses it in the destructor in a call to TerminateProcess to kill it.
My problem is that when my window is closed with the X in the corner, this destructor is never called and the process keeps running in the background, the handle is lost, and I have no indication that it is still running or a way to kill it (other the Task Manager). How can I make sure it is killed.
Larger picture
Here is what I need to do. If you can suggest a better way altogether to accomplish this, that is fine too.
I need a new instance of this java server for every run of my program (sometimes even multiple times in the program). I would like to run it without any visible indication (window etc.) but this is not a requirement. I do need to be able to either make sure that no matter what happens the server I opened is killed when I am done with it, or a way to make sure there is no running instance of it and if so, kill it before I start a new one. (Or better yet, both.) Using the process name is no good, since it is just java, and I don't want to kill all Java instances on the computer. I can (if I knew how) check if a program is listening on the port this server uses, and if so close it.
EDIT
I forgot to mention (if it makes a difference) that the GUI is C# and I have no control over it or over the Java program itself. I am writing a library in c++ that get called from the C# GUI and needs to use the Java server.
Create a new background process called "watchdog". This process obtains a handle to your main GUI and also to the background Java server, and then calls WaitForSingleObject on your GUI's process handle. When this call returns, you terminate the Java server.
Modify your DllMain entrypoint to handle DLL_PROCESS_DETACH. Its response should be to perform whatever desired cleanup you have.
I have scenario where I am scheduling task at fixed duration repetitively.Fixed delay is generated by calling start method of another class that implements runnable interface using Thread.sleep(long ms ) method.
But when I test this application in my local pc it is working.But when I run this application in ibm blade server(64 bit) having OS(Windows server 2008 R2) it do not work as desired. It do not come out of sleep method.
Kindly suggest the solution?
Thank You in Advance.
There is not much information in your question to see what the problem is. Thread.sleep should either return or throw an exception. Maybe something different is happening. For example, an exception has occurred, caught and forgotten, or you have a deadlock somewhere. Anyway, different versions of Java sometimes have subtle differences causing bugs. You will have to investigate the problem yourself.
Try debugging the application. When it hangs, press Pause and examine all threads to find the hanging one.
If you can't install a debugger on the server, add System.out.println in every reasonable place of the code; reading the output in the console, you will probably be able to track down the issue.
If you can't launch the application with console, create a text file and write the messages to it. Don't forget to flush it every time.
I am building a system where there is a simple GUI which will trigger the system to execute. I implemented the system in a way that when an exception is thrown, System.exit(0) is called and so the application stops.
I would like the implementation to stop, as it is doing now, however I wish that the GUI would not be closed as well. I tried implementing the system in a separate thread, however when some exception was thrown, the application still closed down.
Is there a way to leave the UI open, but still stop the implementation?
You cannot use Java system.exit for that purpose because that function kills the instance of Java Virtual Machine that is running your code, consequently stopping all the threads of your application.
In order for System.exit to work the way you want it to right now, you would need to have two different processes, so that each of them would run in its own Java Virtual Machine. However, this will make it harder to link things together.
Ideally, you should add some sort of control in the implementation, so that your GUI thread could activate or deactivate a switch which would naturally stop the logic of the implementation. This really is the best way to go in my opinion.
Essentially, they will need to be separate applications. That is, make them separate processes, not separate threads.
This kind of loose-coupling between your UI and back-end will achieve the behaviour you want, and also yields a bunch of other benefits relating to separating the concerns of your UI and your backend.
No, there is no way to keep your UI open if your program is running on the same process. I suppose you could run your UI and your program as separate applications, but that seems tedious and error prone. If an exception is thrown, you should be notifying the user, not just exiting the JVM.
We have a Java App that connects via RMI to another Java app.
There are multiple instances of this app running at the same time, and after a few days an instance just stops processing... the CPU is in 0 and I have an extra thread listening to an specific port that helps to shutdown the App.
I can connect to the specific port but the app doesn't do anything.
We're using Log4j to keep a log and nothing is written, so there aren't any exceptions thrown.
We also use c3p0 for the DB connections.
Anyone have ideas?
Thanks,
I would suggest starting with a thread dump of the affected application.
You need to see what is going on on a thread by thread basis. It could be that you have a long running thread, or other process which is blocking other work from being done.
Since you are running linux, you can get your thread dump with the following command
kill -3 <pid>
If you need help reading the output, please post it in your original question.
If nothing is shown from the thread dump, other alternatives can be looked at.
Hum... I would suggest using JMetter to stress the Application and take note of anything weird that might be happening (such as Memory Leaks, Deadlocks and such). Also review the code for any Exceptions that might interrupt the program (or System.exit() calls). Finally, if other people have access to the computer, makes sense to check if the process wasn't killed manually somehow.
I have a Java Swing application that is being used as a cluster application. The problem is that every time the cluster tries to terminate the Java application, it just hangs and Windows displays the "End Now" dialog. The said application is a server type one so it spawns a thread for every attempt to connect to it is made.
I learned that the cluster sends the TERM signal using the program presented in this article. BUT when the console application is used as a cluster application, the cluster can just terminate the process after a few TERM signals.
I also tried the vanilla sample desktop application that's available when making a new project using NetBeans 6.8. It also won't terminate even after receiving the signal.
From the demonstrations done above, I think that it has something to do with Swing or with the threads. Can anyone help me with this? Thank you.
EDIT: It could be killed by using the task manager though I think it sends another signal.
When your Java application receives the TERM signal it will run any registered shut-down hooks before terminating. One possibility is that one of these shut-down hooks is blocking indefinitely or else taking a long time (>30 seconds) to run, causing the Windows "End Now" dialog to be displayed.
One thing you could try is to register a shut-down hook that simply prints to the console and verify that it is indeed being called. However, unfortunately there'll be no way to determine whether other shut-down hooks have run at this point as hooks are run in an arbitrary order.