Skip to content

Commit facb33c

Browse files
committed
Verifying the execution sequence for a multi-instance system.
1 parent 000455b commit facb33c

File tree

19 files changed

+3834
-385
lines changed

19 files changed

+3834
-385
lines changed

plc4j/integrations/apache-hop/plc4x-hop-actions/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,10 @@
142142
<scope>provided</scope>
143143
</dependency>
144144
-->
145+
<dependency>
146+
<groupId>org.netbeans.api</groupId>
147+
<artifactId>org-openide-util-lookup</artifactId>
148+
<version>RELEASE170</version>
149+
</dependency>
145150
</dependencies>
146151
</project>

plc4j/integrations/apache-hop/plc4x-hop-actions/src/main/java/org/apache/plc4x/hop/actions/ActionSampleMetaData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@HopMetadata(
2828
key = "Xplc4xaction",
2929
name = "PLC4x Action",
30-
description = "A shared PLC4x connection to a PLC",
30+
description = "A shared PLC4x connection to a PLC_Borrar",
3131
image = "plc4x_toddy.svg",
3232
documentationUrl = "/metadata-types/neo4j/neo4j-connection.html")
3333
public class ActionSampleMetaData extends HopMetadataBase implements IHopMetadata {

plc4j/integrations/apache-hop/plc4x-hop-actions/src/main/java/org/apache/plc4x/hop/actions/Plc4xCheckConnections.java

+88-62
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@
3131

3232

3333
import java.util.List;
34-
import java.util.logging.Level;
35-
import java.util.logging.Logger;
3634
import org.apache.hop.core.Const;
3735
import org.apache.hop.core.exception.HopException;
3836
import org.apache.plc4x.hop.metadata.Plc4xConnection;
37+
import org.apache.plc4x.hop.metadata.util.Plc4xLookup;
38+
import org.apache.plc4x.hop.metadata.util.Plc4xWrapperConnection;
3939
import org.apache.plc4x.java.DefaultPlcDriverManager;
4040
import org.apache.plc4x.java.api.PlcConnection;
41-
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
41+
import org.openide.util.Lookup;
4242
import org.w3c.dom.Node;
4343

44-
44+
/*
45+
* The purpose of this "Action" is firstly to verify the connection to
46+
* the PLC and secondly to create a connection that will be shared by
47+
* the Hop environment.
48+
*/
4549
@Action(
4650
id = "CHECK_PLC4X_CONNECTIONS",
4751
name = "i18n::Plc4xActionConnections.Name",
4852
description = "i18n::Plc4xActionConnections.Description",
49-
image = "plc4x_toddy.svg",
53+
image = "plc4x_toddy_play.svg",
5054
categoryDescription = "i18n:org.apache.hop.workflow:ActionCategory.Category.Conditions",
5155
keywords = "i18n::Plc4xActionConnections.keyword",
5256
documentationUrl = "/workflow/actions/plc4x.html")
@@ -56,7 +60,13 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
5660

5761
private Plc4xConnection[] connections;
5862
private boolean connected = false;
63+
private Plc4xWrapperConnection connwrapper = null;
5964
private PlcConnection plcconn = null;
65+
private ActionBase actionbase = null;
66+
67+
private Plc4xLookup lookup = Plc4xLookup.getDefault();
68+
private Lookup.Template template = null;
69+
private Lookup.Result<Plc4xWrapperConnection> lkresult = null;
6070

6171
protected static final String[] unitTimeDesc =
6272
new String[] {
@@ -79,7 +89,7 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
7989
private long timeStart;
8090
private long now;
8191

82-
public Plc4xCheckConnections( String name) {
92+
public Plc4xCheckConnections(String name) {
8393
super(name, "");
8494
//connections = null;
8595
waitfors = null;
@@ -170,8 +180,6 @@ private static int getWaitTimeByCode(String tt) {
170180
return 0;
171181
}
172182

173-
174-
175183
/**
176184
*
177185
* Save values to XML
@@ -247,63 +255,84 @@ public void loadXml( Node entrynode, IHopMetadataProvider metadataProvider, IVar
247255
}
248256
}
249257

250-
/**
251-
* Execute this action and return the result.
252-
* In this case it means, just set the result boolean in the Result
253-
* class.
254-
* Check all conections metadata from the dialog.
255-
* @param prevResult The result of the previous execution
256-
* @return The Result of the execution.
257-
*/
258-
@Override
259-
public Result execute( Result prevResult, int nr ) {
260-
Result result = prevResult;
261-
result.setNrErrors(0);
262-
connected = true;
263-
for (Plc4xConnection connmeta:connections) {
264-
try {
265-
plcconn = new DefaultPlcDriverManager().getConnection(connmeta.getUrl()); //(01)
266-
if (!plcconn.isConnected()) {
267-
logBasic("Cant connect to: " + connmeta.getUrl());
268-
connected = false;
269-
plcconn = null;
270-
//break;
258+
/**
259+
* Execute this action and return the result.
260+
* In this case it means, just set the result boolean in the Result class.
261+
*
262+
* Check all conections metadata from the dialog (really only one).
263+
* @param prevResult The result of the previous execution
264+
* @return The Result of the execution.
265+
*/
266+
@Override
267+
public Result execute( Result prevResult, int nr ) {
268+
269+
Result result = prevResult;
270+
result.setNrErrors(0);
271+
connected = true;
272+
273+
actionbase = null;
274+
275+
for (Plc4xConnection connmeta:connections) {
276+
277+
if (null == connwrapper) { //(01)
278+
template = new Lookup.Template<>(Plc4xWrapperConnection.class, connmeta.getName(), null);
279+
lkresult = lookup.lookup(template);
280+
if (!lkresult.allItems().isEmpty()) {
281+
connwrapper = (Plc4xWrapperConnection) lkresult.allInstances().toArray()[0]; //(02)
282+
if (connwrapper != null) connwrapper.retain(); //(03)
283+
}
284+
};
285+
286+
if (null == connwrapper) { //(04)
287+
try {
288+
PlcConnection conn = new DefaultPlcDriverManager().getConnection(connmeta.getUrl()); //(05)
289+
if (conn.isConnected()) {
290+
connwrapper = new Plc4xWrapperConnection(conn, connmeta.getName());
291+
lookup.add(connwrapper); //(06)
292+
} else {
293+
connected = false;
294+
plcconn = null;
295+
}
296+
} catch (Exception ex) {
297+
connected = false;
298+
plcconn = null;
299+
}
300+
301+
} else {
302+
if (!connwrapper.getConnection().isConnected()) { //(07)
303+
connected = false;
304+
plcconn = null;
305+
}
306+
}
307+
308+
if (null == connwrapper) { //(08)
309+
try {
310+
311+
PlcConnection conn = new DefaultPlcDriverManager().getConnection(connmeta.getUrl()); //(09)
312+
313+
if (conn.isConnected()) {
314+
conn.close(); //(10)
315+
plcconn = null;
316+
} else {
317+
connected = false;
318+
plcconn = null;
319+
}
320+
} catch (Exception ex){
321+
connected = false;
322+
plcconn = null;
323+
}
324+
271325
}
272-
plcconn.close();
273-
plcconn = null;
274-
} catch (Exception ex) {
275-
Logger.getLogger(Plc4xCheckConnections.class.getName()).log(Level.SEVERE, null, ex);
276-
connected = false;
277-
plcconn = null;
278-
//break;
279-
} finally {
280-
281326
}
282-
283-
}
284327

285328
result.setResult(connected);
286329
return result;
287330
}
288331

289-
/**
290-
*
291-
* Add checks to report warnings
292-
*
293-
* @param remarks
294-
* @param workflowMeta
295-
* @param variables
296-
* @param metadataProvider
297-
*/
298-
@Override
299-
public void check( List<ICheckResult> remarks, WorkflowMeta workflowMeta, IVariables variables,
300-
IHopMetadataProvider metadataProvider ) {
301-
}
302-
303-
@Override
304-
public boolean resetErrorsBeforeExecution() {
305-
return false;
306-
}
332+
@Override
333+
public boolean resetErrorsBeforeExecution() {
334+
return false;
335+
}
307336

308337
@Override
309338
public boolean isEvaluation() {
@@ -313,9 +342,6 @@ public boolean isEvaluation() {
313342
@Override
314343
public boolean isUnconditional() {
315344
return false;
316-
}
317-
318-
319-
345+
}
320346

321347
}

plc4j/integrations/apache-hop/plc4x-hop-actions/src/main/java/org/apache/plc4x/hop/actions/Plc4xCheckDisConnections.java

+49-28
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,45 @@
3333
import java.util.List;
3434
import org.apache.hop.core.Const;
3535
import org.apache.hop.core.exception.HopException;
36+
import org.apache.hop.pipeline.PipelineMeta;
37+
import org.apache.hop.pipeline.engine.IPipelineEngine;
3638
import org.apache.plc4x.hop.metadata.Plc4xConnection;
39+
import org.apache.plc4x.hop.metadata.util.Plc4xLookup;
40+
import org.apache.plc4x.hop.metadata.util.Plc4xWrapperConnection;
41+
import org.openide.util.Lookup;
3742
import org.w3c.dom.Node;
3843

39-
44+
/*
45+
* The purpose of this "Action" is to release the driver resource
46+
* within a "workflow", in case the number of accounts reaches zero,
47+
* the wrapper will close the connection.
48+
* The work pattern must be within the "workflow":
49+
*
50+
* Start -> Create connection -> Run pipeline -> Close connection -> Finish.
51+
*
52+
* It should always be taken into account that the connections to
53+
* the PLCs are limited resources.
54+
*/
4055
@Action(
4156
id = "CHECK_PLC4X_DISCONNECTIONS",
4257
name = "i18n::Plc4xActionDisConnections.Name",
4358
description = "i18n::Plc4xActionDisConnections.Description",
44-
image = "plc4x_toddy.svg",
59+
image = "plc4x_toddy_stop.svg",
4560
categoryDescription = "i18n:org.apache.hop.workflow:ActionCategory.Category.Conditions",
4661
keywords = "i18n::Plc4xActionDisConnections.keyword",
4762
documentationUrl = "/workflow/actions/checkdbconnection.html")
4863
public class Plc4xCheckDisConnections extends ActionBase implements Cloneable, IAction {
49-
private static final Class<?> PKG = Plc4xCheckDisConnections.class; // Needed by Translator
64+
65+
private static final Class<?> PKG = Plc4xCheckDisConnections.class; // Needed by Translator
5066

5167

52-
private Plc4xConnection[] connections;
68+
private Plc4xConnection[] connections;
69+
private Plc4xWrapperConnection connwrapper = null;
70+
private ActionBase actionbase = null;
71+
72+
private Plc4xLookup lookup = Plc4xLookup.getDefault();
73+
private Lookup.Template template = null;
74+
private Lookup.Result<Plc4xWrapperConnection> lkresult = null;
5375

5476
protected static final String[] unitTimeDesc =
5577
new String[] {
@@ -240,32 +262,31 @@ public void loadXml( Node entrynode, IHopMetadataProvider metadataProvider, IVar
240262
}
241263
}
242264

243-
/**
244-
* Execute this action and return the result. In this case it means, just set the result boolean in the Result
245-
* class.
246-
*
247-
* @param result The result of the previous execution
248-
* @return The Result of the execution.
249-
*/
250-
@Override
251-
public Result execute( Result result, int nr ) {
252-
result.setResult(true);
253-
System.out.println("NR: " + nr);
265+
/**
266+
* Execute this action and return the result. In this case it means,
267+
* just set the result boolean in the Result class.
268+
*
269+
* @param result The result of the previous execution
270+
* @return The Result of the execution.
271+
*/
272+
@Override
273+
public Result execute( Result result, int nr ) {
274+
result.setResult(true);
275+
276+
for (Plc4xConnection connmeta:connections) {
277+
template = new Lookup.Template<>(Plc4xWrapperConnection.class, connmeta.getName(), null);
278+
lkresult = lookup.lookup(template);
279+
if (!lkresult.allItems().isEmpty()) {
280+
connwrapper = (Plc4xWrapperConnection) lkresult.allInstances().toArray()[0];
281+
if (connwrapper != null) {
282+
connwrapper.release();
283+
if (connwrapper.refCnt() <= 0)
284+
lookup.remove(connwrapper);
285+
}
286+
}
287+
};
254288

255289
return result;
256290
}
257291

258-
/**
259-
*
260-
* Add checks to report warnings
261-
*
262-
* @param remarks
263-
* @param workflowMeta
264-
* @param variables
265-
* @param metadataProvider
266-
*/
267-
@Override
268-
public void check( List<ICheckResult> remarks, WorkflowMeta workflowMeta, IVariables variables,
269-
IHopMetadataProvider metadataProvider ) {
270-
}
271292
}

0 commit comments

Comments
 (0)