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-unsubscr...@plc4x.apache.org

Reply via email to