@@ -95,7 +95,7 @@ public OpcUaProtocolAdapter(
9595 }
9696
9797 @ Override
98- public void start (
98+ public synchronized void start (
9999 final @ NotNull ProtocolAdapterStartInput input ,
100100 final @ NotNull ProtocolAdapterStartOutput output ) {
101101 log .info ("Starting OPC UA protocol adapter {}" , adapterId );
@@ -112,9 +112,15 @@ public void start(
112112 return ;
113113 }
114114
115+ if (opcUaClientConnection != null ) {
116+ log .warn ("Tried starting already started OPC UA protocol adapter {}" , adapterId );
117+ output .startedSuccessfully ();
118+ return ;
119+ }
120+
115121 protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .DISCONNECTED );
116122
117- final var newOpcUaClientConnection = new OpcUaClientConnection (
123+ opcUaClientConnection = new OpcUaClientConnection (
118124 adapterId ,
119125 tagList ,
120126 protocolAdapterState ,
@@ -126,37 +132,32 @@ public void start(
126132 lastSubscriptionId );
127133
128134 CompletableFuture
129- .supplyAsync (() -> newOpcUaClientConnection .start (parsedConfig ))
135+ .supplyAsync (() -> opcUaClientConnection .start (parsedConfig ))
130136 .whenComplete ((success , throwable ) -> {
131- if (throwable != null ) {
137+ if (!success || throwable != null ) {
138+ this .opcUaClientConnection = null ;
132139 protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .ERROR );
133140 log .error ("Failed to start OPC UA client" , throwable );
134141 return ;
135142 }
136- if (success ) {
137- this .opcUaClientConnection = newOpcUaClientConnection ;
138- } else {
139- protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .ERROR );
140- log .error ("Failed to start OPC UA client" , throwable );
141- }
142143 });
143144
144145 log .info ("Successfully started OPC UA protocol adapter {}" , adapterId );
145146 output .startedSuccessfully ();
146147 }
147148
148149 @ Override
149- public void stop (final @ NotNull ProtocolAdapterStopInput input , final @ NotNull ProtocolAdapterStopOutput output ) {
150+ public synchronized void stop (final @ NotNull ProtocolAdapterStopInput input , final @ NotNull ProtocolAdapterStopOutput output ) {
150151 log .info ("Stopping OPC UA protocol adapter {}" , adapterId );
151152 final var tempOpcUaClientConnection = opcUaClientConnection ;
152153 if (tempOpcUaClientConnection != null ) {
154+ opcUaClientConnection = null ;
153155 tempOpcUaClientConnection .stop ();
154156 output .stoppedSuccessfully ();
155157 } else {
156158 log .info ("Tried stopping stopped OPC UA protocol adapter {}" , adapterId );
157159 output .stoppedSuccessfully ();
158160 }
159-
160161 }
161162
162163 @ Override
0 commit comments