@@ -714,46 +714,55 @@ public void checkpointRestore() {
714714 */
715715 private class CracResourceAdapter implements org .crac .Resource {
716716
717- private @ Nullable CyclicBarrier barrier ;
717+ private CyclicBarrier stepToRestore = new CyclicBarrier (2 );
718+ private CyclicBarrier finishRestore = new CyclicBarrier (2 );
719+
720+ private void preventShutdown () {
721+ waitBarrier (this .stepToRestore );
722+ // Checkpoint happens here
723+ waitBarrier (this .finishRestore );
724+ }
718725
719726 @ Override
720727 public void beforeCheckpoint (org .crac .Context <? extends org .crac .Resource > context ) {
721- // A non-daemon thread for preventing an accidental JVM shutdown before the checkpoint
722- this .barrier = new CyclicBarrier (2 );
723-
724- Thread thread = new Thread (() -> {
725- awaitPreventShutdownBarrier ();
726- // Checkpoint happens here
727- awaitPreventShutdownBarrier ();
728- }, "prevent-shutdown" );
729-
728+ Thread thread = new Thread (this ::preventShutdown , "prevent-shutdown" );
730729 thread .setDaemon (false );
731730 thread .start ();
732- awaitPreventShutdownBarrier ();
733731
734732 logger .debug ("Stopping Spring-managed lifecycle beans before JVM checkpoint" );
735733 stopForRestart ();
736734 }
737735
738736 @ Override
739737 public void afterRestore (org .crac .Context <? extends org .crac .Resource > context ) {
738+ // Unlock barrier for beforeCheckpoint
739+ try {
740+ this .stepToRestore .await ();
741+ }
742+ catch (Exception ex ) {
743+ logger .trace ("Exception from stepToRestore barrier" , ex );
744+ }
745+
740746 logger .info ("Restarting Spring-managed lifecycle beans after JVM restore" );
741747 restartAfterStop ();
742748
743- // Barrier for prevent-shutdown thread not needed anymore
744- this .barrier = null ;
749+ // Unlock barrier for afterRestore to shutdown "prevent-shutdown" thread
750+ try {
751+ this .finishRestore .await ();
752+ }
753+ catch (Exception ex ) {
754+ logger .trace ("Exception from stepToRestore barrier" , ex );
755+ }
745756
746757 if (!checkpointOnRefresh ) {
747758 logger .info ("Spring-managed lifecycle restart completed (restored JVM running for " +
748759 CRaCMXBean .getCRaCMXBean ().getUptimeSinceRestore () + " ms)" );
749760 }
750761 }
751762
752- private void awaitPreventShutdownBarrier ( ) {
763+ private void waitBarrier ( CyclicBarrier barrier ) {
753764 try {
754- if (this .barrier != null ) {
755- this .barrier .await ();
756- }
765+ barrier .await ();
757766 }
758767 catch (Exception ex ) {
759768 logger .trace ("Exception from prevent-shutdown barrier" , ex );
0 commit comments