Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any way to close all existing file handlers when the java application ends?

 
cle tan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As sometimes when the application ends, some of the logs will be xxx.log.1 , xxx.log.2
seems like the logs are still being locked.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would usually close files immediately after reading or writing, but I see that log files would stay open until the JVM exits.
Can you list all open files?
What about a shutdown hook which starts a new thread to close all those files?
 
cle tan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming java application program ended, can it guarantee that a file won't be locked when it exits?

how to implement a shutdown hook?
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any decent operating system will free the resources held by a program upon its termination.
Exactly what kind of lock is it? Does it persist across the life span of a program?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shutdown hooks are really easy to use. If you search, you find lots of links, of which I am showing two: 1 2. I think no 1 is better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic