You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@plc4x.apache.org by "codekundan (via GitHub)" <gi...@apache.org> on 2024/11/30 15:41:50 UTC
[D] Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below in Main Thread, it works fine. Am I missing anything?
GitHub user codekundan created a discussion: Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below in Main Thread, it works fine. Am I missing anything?
`public class Plc4xConnect2 {
private static final Logger logger = LogManager.getLogger(Plc4xConnect.class.getName());
private static boolean closingFlag = false;
public static void main(String[] args) throws PlcConnectionException, Exception {
String connectionString = "opcua:tcp://LAPTOP-A1865M3M:4841?discovery=true&security-policy=Basic128Rsa15&message-security=SIGN_ENCRYPT&key-store-file=D://Plc4x//plc4xConnect//src//test-mxopc-keystore.p12&key-store-password=pass";
try (PlcConnection connection = new DefaultPlcDriverManager().getConnection(connectionString)) {
ExecutorService esVirtual = Executors.newFixedThreadPool(4);
esVirtual.submit(() -> {
Plc4xConnect2.readValues(connection);
});
}
}
static void readValues(PlcConnection connection) {
while (true) {
System.out.println("Thread : ..............................." + Thread.currentThread());
//Read tags in OPC-UA
PlcReadRequest readRequest = connection.readRequestBuilder().addTagAddress("readTag", "ns=4;s=Address Space.Dev00.Tag000;BOOL").build();
// readRequest.execute is thowing Exception
CompletableFuture<? extends PlcReadResponse> readFuture = readRequest.execute();
readFuture.whenComplete((response, error) -> {
try {
for (String tag : response.getTagNames()) {
if (response.getResponseCode(tag) == PlcResponseCode.OK) {
int numValues = response.getNumberOfValues(tag);
System.out.println("numValues = "+numValues);
if (numValues == 1) {
boolean needReceipe = (boolean) response.getObject(tag);
if (needReceipe) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_db", "root", "pass")) {
Statement stmt = con.createStatement();
String query = "select * from table";
ResultSet rs = stmt.executeQuery(query);
int sno = 0;
String first_name = "";
int flow = 0;
double outer_shaping = 0F;
while (rs.next()) {
sno = rs.getInt("sno");
first_name = rs.getString("first_name");
flow = rs.getInt("flow");
outer_shaping = rs.getDouble("outer_shaping");
System.out.println(sno + " " + first_name + " " + flow + " " + outer_shaping);
}
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(Plc4xConnect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
}
} else {
System.out.println("Something went wrong ............." + response.getResponseCode(tag));
}
}
} catch (Exception e) {
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(Plc4xConnect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
}
}`
GitHub link: https://github.com/apache/plc4x/discussions/1920
----
This is an automatically sent email for dev@plc4x.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@plc4x.apache.org
Re: [D] Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below readValues() method in Main Thread, it works fine. Am I missing anything?
Posted by "codekundan (via GitHub)" <gi...@apache.org>.
GitHub user codekundan added a comment to the discussion: Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below readValues() method in Main Thread, it works fine. Am I missing anything?
Hi Plc4x Team, I got the answer. I was making the **mistake of creating a Connection in Main Thread** and **running the tags reading task in Executor Service thread**. Now, reading tags is happening in Executor Service Thread but at the same time the Main Thread reaches End of Line and terminates the Connection. So, now i created the Connection and reading the tags in Executor Service Thread itself which is working fine.
GitHub link: https://github.com/apache/plc4x/discussions/1920#discussioncomment-11422927
----
This is an automatically sent email for dev@plc4x.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@plc4x.apache.org
Re: [D] Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below readValues() method in Main Thread, it works fine. Am I missing anything?
Posted by "codekundan (via GitHub)" <gi...@apache.org>.
GitHub user codekundan edited a discussion: Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below readValues() method in Main Thread, it works fine. Am I missing anything?
`public class Plc4xConnect2 {
private static final Logger logger = LogManager.getLogger(Plc4xConnect.class.getName());
private static boolean closingFlag = false;
public static void main(String[] args) throws PlcConnectionException, Exception {
String connectionString = "opcua:tcp://LAPTOP-A1865M3M:4841?discovery=true&security-policy=Basic128Rsa15&message-security=SIGN_ENCRYPT&key-store-file=D://Plc4x//plc4xConnect//src//test-mxopc-keystore.p12&key-store-password=pass";
try (PlcConnection connection = new DefaultPlcDriverManager().getConnection(connectionString)) {
ExecutorService esVirtual = Executors.newFixedThreadPool(4);
esVirtual.submit(() -> {
Plc4xConnect2.readValues(connection);
});
}
}
static void readValues(PlcConnection connection) {
while (true) {
System.out.println("Thread : ..............................." + Thread.currentThread());
//Read tags in OPC-UA
PlcReadRequest readRequest = connection.readRequestBuilder().addTagAddress("readTag", "ns=4;s=Address Space.Dev00.Tag000;BOOL").build();
// Code is not moving beyond this line
CompletableFuture<? extends PlcReadResponse> readFuture = readRequest.execute();
readFuture.whenComplete((response, error) -> {
try {
for (String tag : response.getTagNames()) {
if (response.getResponseCode(tag) == PlcResponseCode.OK) {
int numValues = response.getNumberOfValues(tag);
System.out.println("numValues = "+numValues);
if (numValues == 1) {
boolean needReceipe = (boolean) response.getObject(tag);
if (needReceipe) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_db", "root", "pass")) {
Statement stmt = con.createStatement();
String query = "select * from table";
ResultSet rs = stmt.executeQuery(query);
int sno = 0;
String first_name = "";
int flow = 0;
double outer_shaping = 0F;
while (rs.next()) {
sno = rs.getInt("sno");
first_name = rs.getString("first_name");
flow = rs.getInt("flow");
outer_shaping = rs.getDouble("outer_shaping");
System.out.println(sno + " " + first_name + " " + flow + " " + outer_shaping);
}
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(Plc4xConnect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
}
} else {
System.out.println("Something went wrong ............." + response.getResponseCode(tag));
}
}
} catch (Exception e) {
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(Plc4xConnect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
}
}`
GitHub link: https://github.com/apache/plc4x/discussions/1920
----
This is an automatically sent email for dev@plc4x.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@plc4x.apache.org
Re: [D] Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below readValues() method in Main Thread, it works fine. Am I missing anything?
Posted by "codekundan (via GitHub)" <gi...@apache.org>.
GitHub user codekundan edited a discussion: Can we read Tags if we run PlcReadRequest in ExecutorService thread? If I run the below readValues() method in Main Thread, it works fine. Am I missing anything?
`public class Plc4xConnect2 {
private static final Logger logger = LogManager.getLogger(Plc4xConnect.class.getName());
private static boolean closingFlag = false;
public static void main(String[] args) throws PlcConnectionException, Exception {
String connectionString = "opcua:tcp://LAPTOP-A1865M3M:4841?discovery=true&security-policy=Basic128Rsa15&message-security=SIGN_ENCRYPT&key-store-file=D://Plc4x//plc4xConnect//src//test-mxopc-keystore.p12&key-store-password=pass";
try (PlcConnection connection = new DefaultPlcDriverManager().getConnection(connectionString)) {
ExecutorService esVirtual = Executors.newFixedThreadPool(4);
esVirtual.submit(() -> {
Plc4xConnect2.readValues(connection);
});
}
}
static void readValues(PlcConnection connection) {
while (true) {
System.out.println("Thread : ..............................." + Thread.currentThread());
//Read tags in OPC-UA
PlcReadRequest readRequest = connection.readRequestBuilder().addTagAddress("readTag", "ns=4;s=Address Space.Dev00.Tag000;BOOL").build();
// readRequest.execute is thowing Exception
CompletableFuture<? extends PlcReadResponse> readFuture = readRequest.execute();
readFuture.whenComplete((response, error) -> {
try {
for (String tag : response.getTagNames()) {
if (response.getResponseCode(tag) == PlcResponseCode.OK) {
int numValues = response.getNumberOfValues(tag);
System.out.println("numValues = "+numValues);
if (numValues == 1) {
boolean needReceipe = (boolean) response.getObject(tag);
if (needReceipe) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_db", "root", "pass")) {
Statement stmt = con.createStatement();
String query = "select * from table";
ResultSet rs = stmt.executeQuery(query);
int sno = 0;
String first_name = "";
int flow = 0;
double outer_shaping = 0F;
while (rs.next()) {
sno = rs.getInt("sno");
first_name = rs.getString("first_name");
flow = rs.getInt("flow");
outer_shaping = rs.getDouble("outer_shaping");
System.out.println(sno + " " + first_name + " " + flow + " " + outer_shaping);
}
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(Plc4xConnect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
}
} else {
System.out.println("Something went wrong ............." + response.getResponseCode(tag));
}
}
} catch (Exception e) {
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(Plc4xConnect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
}
}`
GitHub link: https://github.com/apache/plc4x/discussions/1920
----
This is an automatically sent email for dev@plc4x.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@plc4x.apache.org