What happens for in-progress job when do restart java - java

When the java app in the middle of the service process restarts, waits for the process and after finishing that continued or does abort the process.

Depends on which process we are talking about, in-flight http requests are killed, but this behaviour can be configured, see https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.graceful-shutdown.

Related

Dropwizard: Need to shutdown the dropwizard application gracefully gracefully

When I send a stop signal(either with kill -SIGINT <pid> or System.exit(0) or environment.getApplicationContext().getServer().stop()) to the application, it waits for the shutdownGracePeriod (by default 30 sec or whatever I configure in .yml file) and also it does not accept new request. However, my requirement is to make the server wait for the ongoing request to complete before stopping. The ongoing request may take 30 sec or 30 minutes, it is unknown. Can somebody suggest me the way to achieve this?
Note: I've referred to the below links but could not achieve.
How to shutdown dropwizard application?
shutdownGracePeriod
We've used in-app healthcheck combined with some external load balancer service and prestop scripts. A healthcheck is turned off by the prestop script, then healthcheck says it is unhealthy so no new requests are sent by the load balancer (but existing ones are processed), only after draining period a stop signal is sent to the application.
Even this though has a specified time limit. I don't know how you would monitor requests that last an unknown amount of time.

Linux Shutdown Order

Gracefully shutting down a system ( using "shutdown" command ), terminates all the services registered under systemd in order and also send kill signal to all the running processes to give them a chance gracefully shut down.
Is there any specific order in which kill signal is sent to the processes which are not registered as service in systemd?
Is any any order between systemd services shut down and kill signal sent to other processes?
I've a java application process running on a VM and want that it's terminated only after a particular service registered under systemd has terminated. Is there any other way to achive this thing?
I would not count on non-service process order, because it might not exist or depend on OS flavors / versions.
How about creating our service which controls the process start/stop order and adding it to the system?

Tomcat8 kills my threads on shutdown

I created a webapplication that needs to do some cleanup on shutdown. This cleanup will take about a minute and its completely OK for it to do so.
When I deploy my webapp onto Tomcat 8 and then stop it, my ContextListener gets called and the cleanup begins. But it seems like Tomcat stops my thread the hard way and it won't complete anymore. At least on Tomcat 6 that wasn't an issue.
An ideas how to configure Tomcat 8 to stop from misbehaving?
Partial Answer:
I found out it has something to do with a performance optimization I did. I used startStopThreads="2" to start my applications in parallel, which works out well, but on shutdown this also seems to kill my threads.
If you have a task which is to be performed on shutdown, I would add this as shutdown hook. Most likely Tomcat 8 is called System.exit() which is a normal thing to do and this kills all user threads but start shutdown hooks.
A better solution is to never leave the system in a state where you really need this. i.e. you cannot assume an application will die gracefully.
if you are waiting for client to disconnect, I suggest you add a shutting down phase. During this phase you refuse new connections, move connections to another server or attempt to gracefully tell existing ones you are going away. After a short period or time out, you then shut down.

How to process servlet requests during long shutdown

We need to implement a graceful shutdown mechanism into our Servlet application.
EDIT: We want to make it simple as possible, which would be handling a kill signal sent via operating system's function. This would allow system admins to use built in shell utilities (kill or taskkill on Windows), otherwise they would have to install another utility just to "talk" with server.
This mechanism works in two phases:
upon shutdown request, deny certain critical activities
block until previously initiated critical actions are completed; these may take several hours
Phase #1 is implemented in our DAO layer.
Phase #2 is implemented in our ServletContextListener#contextDestroyed method
Our problem is that once contextDestroyed is called the Servlet container stops servicing further HTTP requests.
EDIT: contextDestroyed is called when someone is calling the operating system's kill function on server's process.
We would like to let the application alive during Phase #2, notifying the users that some activities are unavailable.
Use a filter to keep a list of all critical requests.
When the "prepare shutdown" request is received, the filter should start denying some requests.
Write a servlet that tells you how many critical jobs are still left in the queue.
In the shutdown tool, send the "prepare shutdown". The poll the servlet for the number of critical jobs. When this reaches 0, send the actual shutdown command.
To make this happen, create a service in the business layer which orchestrates this. Note that everything must happen before contextDestroyed() is being called! Your special application shutdown doesn't fit into the J2EE view of the world, so you have to manage it yourself.
The service should be able to tell interested parties when a shutdown is in progress, how many critical jobs are still running, etc. Servlets and filters can then use this service to deny requests or tell how many jobs are left.
When all jobs are done, deny all requests except access to the "shutdown info" servlet which should then tell that the app is now ready for death.
Write a tool which gives the administrators a nice UI to initiate shutdown of your app.
[EDIT] You may feel tempted to prevent the OS from shutting down your application. Don't do that.
What you should do is write a special tool to shut down your application using the two phase process that I described above. This should be the standard way to shutdown.
Yes, administrators will complain about it. On Unix, you can hide this tool by putting it into the init script, so no one will notice. There might be a similar solution on Windows.
Killing the server should always be possible to be able to stop it in case of (un)expected circumstances like: bugs in your shutdown code, emergency shutdown during power failure, bugs in your application code, or when Murphy happens.

How to do an action on process terminating?

I have written a program in Java which creates a socket connection for a simple online game. The server is multiclient and has a list of users logged. When I close the client it sends to the server a message to log out the user. I also want that when the client is terminated with the "terminate" button of Eclipse (or with Windows task manager) the client sends the same message. I tried with a ShootdownHook but it does not work. Any idea?
Thanks.
In order to have a robust system, you will at some point need to implement a heartbeat mechanism that allows the server to close connections. For example, if a client hasn't set a heartbeat in the last 30 seconds, then close the connection. Consider the case where the network between client and server goes down... or the machine the client is running on dies an ugly death. In those cases, you cannot rely on the client's logout message getting to the server.
Having said that, I suspect that shutdown hook is too late to do actual network IO. I have successfully used shutdown hooks to close connections. So you will need to have a more formal shutdown where the message is sent before other shutdown activity -- especially closing connections -- is initiated.
Nothing you can on the client side can act in the event of the red square in Eclipse. It terminates the jvm 'with extreme prejudice' -- no code runs. No hooks, no nothing.
You have, as #Dilum explained, to cope on the server side.

Categories