@@ -142,26 +142,33 @@ public void initialize() {
142142 errorMsg = initializeBridgeIp (getConfigAs (PowermaxIpConfiguration .class ), threadName );
143143 }
144144
145+ // Normally globalJob should always be null when initialize() is called
146+ // because job is cancelled and globalJob is set to null in dispose().
147+ // By security, cancel the job in case it is not null.
148+ ScheduledFuture <?> job = globalJob ;
149+ if (job != null ) {
150+ logger .warn ("initialize() for {}: encountering not null globalJob" , getThing ().getUID ());
151+ job .cancel (true );
152+ globalJob = null ;
153+ }
154+
145155 if (errorMsg == null ) {
146- ScheduledFuture <?> job = globalJob ;
147- if (job == null || job .isCancelled ()) {
148- // Delay the startup in case the handler is restarted immediately
149- globalJob = scheduler .scheduleWithFixedDelay (() -> {
150- try {
151- logger .trace ("Powermax job..." );
152- updateMotionSensorState ();
153- updateRingingState ();
154- if (isConnected ()) {
155- checkKeepAlive ();
156- retryDownloadSetup ();
157- } else {
158- tryReconnect ();
159- }
160- } catch (Exception e ) {
161- logger .warn ("Exception in scheduled job: {}" , e .getMessage (), e );
156+ // Delay the startup in case the handler is restarted immediately
157+ globalJob = scheduler .scheduleWithFixedDelay (() -> {
158+ try {
159+ logger .trace ("Powermax job..." );
160+ updateMotionSensorState ();
161+ updateRingingState ();
162+ if (isConnected ()) {
163+ checkKeepAlive ();
164+ retryDownloadSetup ();
165+ } else {
166+ tryReconnect ();
162167 }
163- }, 10 , JOB_REPEAT , TimeUnit .SECONDS );
164- }
168+ } catch (Exception e ) {
169+ logger .warn ("Exception in scheduled job: {}" , e .getMessage (), e );
170+ }
171+ }, 10 , JOB_REPEAT , TimeUnit .SECONDS );
165172 } else {
166173 updateStatus (ThingStatus .OFFLINE , ThingStatusDetail .CONFIGURATION_ERROR , errorMsg );
167174 }
@@ -225,7 +232,7 @@ public void initialize() {
225232 public void dispose () {
226233 logger .debug ("Handler disposed for thing {}" , getThing ().getUID ());
227234 ScheduledFuture <?> job = globalJob ;
228- if (job != null && ! job . isCancelled () ) {
235+ if (job != null ) {
229236 job .cancel (true );
230237 globalJob = null ;
231238 }
0 commit comments