@@ -589,46 +589,55 @@ public void checkpointRestore() {
589589 */
590590 private class CracResourceAdapter implements org .crac .Resource {
591591
592- private @ Nullable CyclicBarrier barrier ;
592+ private CyclicBarrier stepToRestore = new CyclicBarrier (2 );
593+ private CyclicBarrier finishRestore = new CyclicBarrier (2 );
594+
595+ private void preventShutdown () {
596+ waitBarrier (this .stepToRestore );
597+ // Checkpoint happens here
598+ waitBarrier (this .finishRestore );
599+ }
593600
594601 @ Override
595602 public void beforeCheckpoint (org .crac .Context <? extends org .crac .Resource > context ) {
596- // A non-daemon thread for preventing an accidental JVM shutdown before the checkpoint
597- this .barrier = new CyclicBarrier (2 );
598-
599- Thread thread = new Thread (() -> {
600- awaitPreventShutdownBarrier ();
601- // Checkpoint happens here
602- awaitPreventShutdownBarrier ();
603- }, "prevent-shutdown" );
604-
603+ Thread thread = new Thread (this ::preventShutdown , "prevent-shutdown" );
605604 thread .setDaemon (false );
606605 thread .start ();
607- awaitPreventShutdownBarrier ();
608606
609607 logger .debug ("Stopping Spring-managed lifecycle beans before JVM checkpoint" );
610608 stopForRestart ();
611609 }
612610
613611 @ Override
614612 public void afterRestore (org .crac .Context <? extends org .crac .Resource > context ) {
613+ // Unlock barrier for beforeCheckpoint
614+ try {
615+ this .stepToRestore .await ();
616+ }
617+ catch (Exception ex ) {
618+ logger .trace ("Exception from stepToRestore barrier" , ex );
619+ }
620+
615621 logger .info ("Restarting Spring-managed lifecycle beans after JVM restore" );
616622 restartAfterStop ();
617623
618- // Barrier for prevent-shutdown thread not needed anymore
619- this .barrier = null ;
624+ // Unlock barrier for afterRestore to shutdown "prevent-shutdown" thread
625+ try {
626+ this .finishRestore .await ();
627+ }
628+ catch (Exception ex ) {
629+ logger .trace ("Exception from stepToRestore barrier" , ex );
630+ }
620631
621632 if (!checkpointOnRefresh ) {
622633 logger .info ("Spring-managed lifecycle restart completed (restored JVM running for " +
623634 CRaCMXBean .getCRaCMXBean ().getUptimeSinceRestore () + " ms)" );
624635 }
625636 }
626637
627- private void awaitPreventShutdownBarrier ( ) {
638+ private void waitBarrier ( CyclicBarrier barrier ) {
628639 try {
629- if (this .barrier != null ) {
630- this .barrier .await ();
631- }
640+ barrier .await ();
632641 }
633642 catch (Exception ex ) {
634643 logger .trace ("Exception from prevent-shutdown barrier" , ex );
0 commit comments