tags:

views:

19

answers:

2

We recently changed our system to keep the latest data in the memory instead of writing to the database. (it will be written to the database when the I/O is relatively free)

However, now we are stuck if in the event of Shutdown is required, there might be some data that have not written to the Database yet.

Is there anyway in Tomcat to trigger a call either a servlet method or some sort to perform action when a "shutdown" is issue to tomcat? We are using the following setup:

OS: Windows Server 2008

Tomcat: 5.5.30

Any idea how to perform the above?

Note: at the moment, we shutdown the tomcat from the windows services.

+1  A: 

Like everyone else has pointed out, looks like Servlet.destroy() may be what you want.

public void destroy()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

Does this meet your requirements? You can also implement a context listener:

Handling Servlet Life-Cycle Events

You can monitor and react to events in a servlet's life cycle by defining listener objects whose methods get invoked when life cycle events occur. To use these listener objects, you must define the listener class and specify the listener class.

There's examples there too.

The Alchemist