0

I'm developing a project which has complete java code & SQL, where the application kicks-off from the shell script. I want to execute some cleanup task (closing transactions n logging abrupt message) when a user press Ctrl^C.

I tried with adding shut down task by Runtime.getRuntime().addShutdownHook(Thread)

But it'll execute every time my application is exits the VM & i never know is that command killed / executed successfully.

I'm looking for some pointers to implement this.

  • Thanks in Advance..!!!
anji_rajesh
  • 369
  • 2
  • 11
  • http://stackoverflow.com/questions/1486679/determine-exit-status-within-the-java-shutdown-hook-thread may help. – M A Apr 27 '15 at 19:57

1 Answers1

0

It would work if you override the SecurityManager checkExit(int status) method. But this is not a recommended way of handling this sort of event. If my code experience ctrl+c, I usually like to do clean up when I see something inconsistent next time. This is usually not a bad idea.

BufBills
  • 8,005
  • 12
  • 48
  • 90
  • The application db can be used in some other places, where i'll get the stale data (In my application perspective). I'm trying to avoid that & log some messages against those objects. So on exit only i should do these tasks. – anji_rajesh Apr 27 '15 at 20:10
  • @anji_rajesh sounds like you have to make sure your DB operation is more atomic. Rather than multiple steps which can cause stale data. – BufBills Apr 27 '15 at 20:12