You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by ld...@apache.org on 2022/05/15 19:59:06 UTC

[plc4x] branch develop updated: fix(plc4j/opcua): More rebust MonitoredItem handling within subscription handle.

This is an automated email from the ASF dual-hosted git repository.

ldywicki pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 066c3954af fix(plc4j/opcua): More rebust MonitoredItem handling within subscription handle.
066c3954af is described below

commit 066c3954afa0ae3b534432802a61c3f9d448d958
Author: Łukasz Dywicki <lu...@code-house.org>
AuthorDate: Sun May 15 21:56:20 2022 +0200

    fix(plc4j/opcua): More rebust MonitoredItem handling within subscription handle.
    
    Fix for PLC4X-342. Minor adjustments of logging so stack traces go through slf4j.
    
    Signed-off-by: Łukasz Dywicki <lu...@code-house.org>
---
 .../opcua/protocol/OpcuaSubscriptionHandle.java    | 52 +++++++++-------------
 1 file changed, 20 insertions(+), 32 deletions(-)

diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java
index b9d9cedaab..65572e94a7 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java
@@ -77,8 +77,7 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
         try {
             onSubscribeCreateMonitoredItemsRequest().get();
         } catch (Exception e) {
-            LOGGER.info("Unable to serialize the Create Monitored Item Subscription Message");
-            e.printStackTrace();
+            LOGGER.info("Unable to serialize the Create Monitored Item Subscription Message", e);
             plcSubscriber.onDisconnect(context);
         }
         startSubscriber();
@@ -141,7 +140,7 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
         CreateMonitoredItemsRequest createMonitoredItemsRequest = new CreateMonitoredItemsRequest(
             requestHeader,
             subscriptionId,
-            TimestampsToReturn.timestampsToReturnNeither,
+            TimestampsToReturn.timestampsToReturnBoth,
             requestList.size(),
             requestList
         );
@@ -175,37 +174,35 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                         plcSubscriber.onDisconnect(context);
                     }
                 } catch (ParseException e) {
-                    LOGGER.error("Unable to parse the returned Subscription response");
-                    e.printStackTrace();
+                    LOGGER.error("Unable to parse the returned Subscription response", e);
                     plcSubscriber.onDisconnect(context);
                 }
-                for (MonitoredItemCreateResult result : responseMessage.getResults().toArray(new MonitoredItemCreateResult[0])) {
+                MonitoredItemCreateResult[] array = responseMessage.getResults().toArray(new MonitoredItemCreateResult[0]);
+                for (int index = 0, arrayLength = array.length; index < arrayLength; index++) {
+                    MonitoredItemCreateResult result = array[index];
                     if (OpcuaStatusCode.enumForValue(result.getStatusCode().getStatusCode()) != OpcuaStatusCode.Good) {
-                        LOGGER.error("Invalid Field {}, subscription created without this field", fieldNames.get((int) result.getMonitoredItemId()));
+                        LOGGER.error("Invalid Field {}, subscription created without this field", fieldNames.get(index));
                     } else {
-                        LOGGER.debug("Field {} was added to the subscription", fieldNames.get((int) result.getMonitoredItemId() - 1));
+                        LOGGER.debug("Field {} was added to the subscription", fieldNames.get(index));
                     }
                 }
                 future.complete(responseMessage);
             };
 
             Consumer<TimeoutException> timeout = e -> {
-                LOGGER.info("Timeout while sending the Create Monitored Item Subscription Message");
-                e.printStackTrace();
+                LOGGER.info("Timeout while sending the Create Monitored Item Subscription Message", e);
                 plcSubscriber.onDisconnect(context);
             };
 
             BiConsumer<OpcuaAPU, Throwable> error = (message, e) -> {
-                LOGGER.info("Error while sending the Create Monitored Item Subscription Message");
-                e.printStackTrace();
+                LOGGER.info("Error while sending the Create Monitored Item Subscription Message", e);
                 plcSubscriber.onDisconnect(context);
             };
 
             channel.submit(context, timeout, error, consumer, buffer);
 
         } catch (SerializationException e) {
-            LOGGER.info("Unable to serialize the Create Monitored Item Subscription Message");
-            e.printStackTrace();
+            LOGGER.info("Unable to serialize the Create Monitored Item Subscription Message", e);
             plcSubscriber.onDisconnect(context);
         }
         return future;
@@ -287,8 +284,7 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                                         //plcSubscriber.onDisconnect(context);
                                     }
                                 } catch (ParseException e) {
-                                    LOGGER.error("Unable to parse the returned Subscription response");
-                                    e.printStackTrace();
+                                    LOGGER.error("Unable to parse the returned Subscription response", e);
                                     plcSubscriber.onDisconnect(context);
                                 }
                                 if (serviceFault == null) {
@@ -312,14 +308,12 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                             };
 
                             Consumer<TimeoutException> timeout = e -> {
-                                LOGGER.error("Timeout while waiting for subscription response");
-                                e.printStackTrace();
+                                LOGGER.error("Timeout while waiting for subscription response", e);
                                 plcSubscriber.onDisconnect(context);
                             };
 
                             BiConsumer<OpcuaAPU, Throwable> error = (message, e) -> {
-                                LOGGER.error("Error while waiting for subscription response");
-                                e.printStackTrace();
+                                LOGGER.error("Error while waiting for subscription response", e);
                                 plcSubscriber.onDisconnect(context);
                             };
 
@@ -327,8 +321,7 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                             channel.submit(context, timeout, error, consumer, buffer);
 
                         } catch (SerializationException e) {
-                            LOGGER.warn("Unable to serialize subscription request");
-                            e.printStackTrace();
+                            LOGGER.warn("Unable to serialize subscription request", e);
                         }
                     }
                     /* Put the subscriber loop to sleep for the rest of the cycle. */
@@ -338,8 +331,7 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                 //sleep(this.revisedCycleTime * 10);
                 complete = true;
             } catch (Exception e) {
-                LOGGER.error("Failed :(");
-                e.printStackTrace();
+                LOGGER.error("Failed to start subscription", e);
             }
             return null;
         });
@@ -400,27 +392,23 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                         LOGGER.error("Fault when deleteing Subscription ServiceFault return from server with error code,  '{}'", header.getServiceResult().toString());
                     }
                 } catch (ParseException e) {
-                    LOGGER.error("Unable to parse the returned Delete Subscriptions Response");
-                    e.printStackTrace();
+                    LOGGER.error("Unable to parse the returned Delete Subscriptions Response", e);
                 }
             };
 
             Consumer<TimeoutException> timeout = e -> {
-                LOGGER.error("Timeout while waiting for delete subscription response");
-                e.printStackTrace();
+                LOGGER.error("Timeout while waiting for delete subscription response", e);
                 plcSubscriber.onDisconnect(context);
             };
 
             BiConsumer<OpcuaAPU, Throwable> error = (message, e) -> {
-                LOGGER.error("Error while waiting for delete subscription response");
-                e.printStackTrace();
+                LOGGER.error("Error while waiting for delete subscription response", e);
                 plcSubscriber.onDisconnect(context);
             };
 
             channel.submit(context, timeout, error, consumer, buffer);
         } catch (SerializationException e) {
-            LOGGER.warn("Unable to serialize subscription request");
-            e.printStackTrace();
+            LOGGER.warn("Unable to serialize subscription request", e);
         }
 
         sleep(500);