@@ -121,46 +121,48 @@ public ProtocolAdapterWrapper(
121121 initStartAttempt ();
122122 final var output = new ProtocolAdapterStartOutputImpl ();
123123 final var input = new ProtocolAdapterStartInputImpl (moduleServices );
124- final var startFuture = CompletableFuture .supplyAsync (() -> {
125- try {
126- adapter .start (input , output );
127- } catch (final Throwable throwable ) {
128- output .getStartFuture ().completeExceptionally (throwable );
129- }
130- return output .getStartFuture ();
131- }).thenCompose (Function .identity ()).handle ((ignored , error ) -> {
132- if (error != null ) {
133- log .error ("Error starting adapter" , error );
134- stopAfterFailedStart ();
135- protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STOPPED );
136- //we still return the initial error since that's the most significant information
137- return CompletableFuture .failedFuture (error );
138- } else {
139- protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STARTED );
140- return attemptStartingConsumers (writingEnabled ,
141- moduleServices .eventService ()).handle ((success , startException ) -> {
142- if (startException == null ) {
143- if (success ) {
144- log .debug ("Successfully started adapter with id {}" , adapter .getId ());
145- } else {
146- log .debug ("Partially started adapter with id {}" , adapter .getId ());
147- }
148- } else {
149- log .error ("Failed to start adapter with id {}" , adapter .getId (), startException );
124+ final var startFuture = CompletableFuture
125+ .supplyAsync (() -> {
126+ try {
127+ adapter .start (input , output );
128+ } catch (final Throwable throwable ) {
129+ output .getStartFuture ().completeExceptionally (throwable );
130+ }
131+ return output .getStartFuture ();
132+ })
133+ .thenCompose (Function .identity ()).handle ((ignored , error ) -> {
134+ if (error != null ) {
135+ log .error ("Error starting adapter" , error );
150136 stopAfterFailedStart ();
151- //we still return the initial error since that's the most significant information
152137 protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STOPPED );
153- throw new RuntimeException ("Failed to start adapter with id " + adapter .getId (),
154- startException );
138+ //we still return the initial error since that's the most significant information
139+ return CompletableFuture .failedFuture (error );
140+ } else {
141+ protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STARTED );
142+ return attemptStartingConsumers (writingEnabled ,
143+ moduleServices .eventService ()).handle ((success , startException ) -> {
144+ if (startException == null ) {
145+ if (success ) {
146+ log .debug ("Successfully started adapter with id {}" , adapter .getId ());
147+ } else {
148+ log .debug ("Partially started adapter with id {}" , adapter .getId ());
149+ }
150+ } else {
151+ log .error ("Failed to start adapter with id {}" , adapter .getId (), startException );
152+ stopAfterFailedStart ();
153+ //we still return the initial error since that's the most significant information
154+ protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STOPPED );
155+ throw new RuntimeException ("Failed to start adapter with id " + adapter .getId (),
156+ startException );
157+ }
158+ return success ;
159+ });
155160 }
156- return success ;
161+ }).thenApply (ignored -> (Void ) null ).whenComplete ((result , throwable ) -> {
162+ //always clean up state
163+ startFutureRef .set (null );
164+ operationState .set (OperationState .IDLE );
157165 });
158- }
159- }).thenApply (ignored -> (Void ) null ).whenComplete ((result , throwable ) -> {
160- //always clean up state
161- startFutureRef .set (null );
162- operationState .set (OperationState .IDLE );
163- });
164166
165167 startFutureRef .set (startFuture );
166168 return startFuture ;
@@ -302,33 +304,38 @@ private void cleanUpScheduler() {
302304
303305 final var stopFuture = new CompletableFuture <Void >();
304306 stopFutureRef .set (stopFuture );
305- CompletableFuture .supplyAsync (() -> {
306- stopPolling (protocolAdapterPollingService );
307- stopWriting (protocolAdapterWritingService );
308- try {
309- adapter .stop (input , output );
310- } catch (final Throwable throwable ) {
311- output .getOutputFuture ().completeExceptionally (throwable );
312- }
313- return output .getOutputFuture ();
314- }).thenCompose (Function .identity ())
315- .whenComplete ((result , throwable ) -> {
316- if (destroy ) {
317- log .info ("Destroying adapter with id '{}'" , getId ());
318- adapter .destroy ();
319- }
320- protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STOPPED );
321- operationState .set (OperationState .IDLE );
322- stopFutureRef .set (null );
323- if (throwable == null ) {
324- log .info ("Stopped adapter with id {}" , adapter .getId ());
325- stopFuture .complete (null );
326- } else {
327- log .error ("Error stopping adapter with id {}" , adapter .getId (), throwable );
328- stopFuture .completeExceptionally (throwable );
329- }
330- });
331307
308+ final var actualFuture = CompletableFuture
309+ .supplyAsync (() -> {
310+ stopPolling (protocolAdapterPollingService );
311+ stopWriting (protocolAdapterWritingService );
312+ try {
313+ adapter .stop (input , output );
314+ } catch (final Throwable throwable ) {
315+ output .getOutputFuture ().completeExceptionally (throwable );
316+ }
317+ return output .getOutputFuture ();
318+ })
319+ .thenApply (v -> {
320+ stopFutureRef .set (null );
321+ protocolAdapterState .setRuntimeStatus (ProtocolAdapterState .RuntimeStatus .STOPPED );
322+ operationState .set (OperationState .IDLE );
323+ if (destroy ) {
324+ log .info ("Destroying adapter with id '{}'" , getId ());
325+ adapter .destroy ();
326+ }
327+ return v ;
328+ })
329+ .thenCompose (Function .identity ())
330+ .whenComplete ((result , throwable ) -> {
331+ if (throwable == null ) {
332+ log .info ("Stopped adapter with id {}" , adapter .getId ());
333+ } else {
334+ log .error ("Error stopping adapter with id {}" , adapter .getId (), throwable );
335+ }
336+ });
337+
338+ stopFuture .thenCompose (v -> actualFuture );
332339 return stopFuture ;
333340 }
334341
0 commit comments