2

The idea is to check for updates while the application is running. The update consists of one main jar and a couple of third party jars. If updates are available:

  • Download them
  • Put them in a temp directory
  • Add a shutdown hook that replaces all the actual jars with the jars from the temp directory

Is this a good idea?

Chadi
  • 737
  • 1
  • 6
  • 21

2 Answers2

1

No. From the Javadoc:

Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

Ergo you should not engage in long-running or blocking activities in a shutdown hook.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Is copying a couple of (small) files from a temporary directory to another considered a long-running activity? Note that this does not include the download time as the updates were already downloaded before the shutdown and put in this temp directory. – Chadi Oct 07 '15 at 10:04
  • In addition, we had issues with the update of the main JAR on Windows and Java 8 (file being in use). We finally opted for a safer, already done solution: [getdown](https://github.com/threerings/getdown/). – Chadi Jan 04 '16 at 15:09
0

If you want a well reliable system around shutDown hook then its not a good idea due to following guidelines :-
a) Shutdown Hooks may not be executed on certain scenarios.
b) Once started, the shutdown hooks can be forcibly stopped before completion.
c) If defined more than one shutdown hooks, their execution order is not guaranteed.
d) Can't register/unregister Shutdown Hooks with in Shutdown Hooks.

More on it can be found at Javadoc ref

Community
  • 1
  • 1
Avis
  • 2,197
  • 18
  • 28
  • It's OK if hooks do not execute occasionally. They will just execute next time. The execution order is not relevant since I only have one hook. I don't need to un-register hooks. – Chadi Oct 09 '15 at 13:54