You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2020/11/19 10:08:48 UTC

[incubator-streampipes-extensions] branch dev updated: Use try-with-resources syntac in plc4x s7 adapter

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

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git


The following commit(s) were added to refs/heads/dev by this push:
     new 56e1506  Use try-with-resources syntac in plc4x s7 adapter
56e1506 is described below

commit 56e15064d6bf1898c335eaa83908693960007f33
Author: Philipp Zehnder <ze...@fzi.de>
AuthorDate: Thu Nov 19 11:08:19 2020 +0100

    Use try-with-resources syntac in plc4x s7 adapter
---
 .../connect/adapters/plc4x/s7/Plc4xS7Adapter.java            | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java
index e2ad3b0..13672bd 100644
--- a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java
+++ b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java
@@ -193,15 +193,15 @@ public class Plc4xS7Adapter extends PullAdapter {
         getConfigurations(adapterDescription);
 
         this.driverManager = new PooledPlcDriverManager();
-        try {
-            PlcConnection plcConnection =  this.driverManager.getConnection("s7://" + this.ip);
+        try (PlcConnection plcConnection = this.driverManager.getConnection("s7://" + this.ip)) {
 
             if (!plcConnection.getMetadata().canRead()) {
                 throw new AdapterException("The S7 on IP: " + this.ip + " does not support reading data");
             }
-
         } catch (PlcConnectionException e) {
             throw new AdapterException("Could not establish connection to S7 with ip " + this.ip, e);
+        } catch (Exception e) {
+            throw new AdapterException("Could not close connection to S7 with ip " + this.ip, e);
         }
     }
 
@@ -213,8 +213,7 @@ public class Plc4xS7Adapter extends PullAdapter {
     protected void pullData() {
 
         // Create PLC read request
-        try  {
-            PlcConnection plcConnection = this.driverManager.getConnection("s7://" + this.ip);
+        try (PlcConnection plcConnection = this.driverManager.getConnection("s7://" + this.ip)) {
             PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
             for (Map<String, String> node : this.nodes) {
                 builder.addItem(node.get(PLC_NODE_NAME), node.get(PLC_NODE_NAME) + ":" + node.get(PLC_NODE_TYPE).toUpperCase());
@@ -240,8 +239,7 @@ public class Plc4xS7Adapter extends PullAdapter {
 
                 // publish the final event
                 adapterPipeline.process(event);
-                plcConnection.close();
-            } catch (InterruptedException | ExecutionException e) {
+        } catch (InterruptedException | ExecutionException e) {
                 LOG.error(e.getMessage());
                 e.printStackTrace();
             } catch (Exception e) {