frankpuppa opened a new issue, #2229:
URL: https://github.com/apache/plc4x/issues/2229

   ### What happened?
   
   While I was testing the stability of a modbus application based on plc4x, I 
faced the problem that on successful reconnection attempts the number of live 
threads keeps increasing continuously. Specifically, there is a pool as such: 
pool-N-thread-M (probably related to netty) which sits waiting and does not 
shut down. 
   The same applies when using the CachedPlcConnectionManager
   Connection string: modbus-tcp://127.0.0.1:9081?unit-identifier=1
   
   Here is a sample code with springboot:
   ```
    @Scheduled(fixedDelayString = "${scheduling.interval}") 
       public void update() {
           logger.info("Executing update method...")
   
           try {
               reconnect(false);
   
               if (plcConnection == null || !plcConnection.isConnected()) {
                   logger.info("PLC connection is not alive, exiting...");
                   return;
               }
   
               logger.info("OK");
           } catch (Exception e) {
               logger.error("Error while trying to read ... reconnecting", e);
           }
       }
   
         protected void reconnect(boolean error) {
   
           try {
               try {
                   if (plcConnection != null && !plcConnection.isConnected()) {
                       logger.info("Reconnecting...");
                       // either with plcConnection.connect() or
                       // connectionManager.getConnection(plcConnectionString); 
There is a n increment
                       // of threads
                       // plcConnection = 
connectionManager.getConnection(plcConnectionString);
                       plcConnection.connect();
                       return;
                   }
               } catch (Exception e) {
                   logger.error("Error: {}", e.getMessage());
                   return;
               }
   
               // Open a new one
               if (plcConnection == null) {
                   logger.info("Connecting...");
                   plcConnection = 
connectionManager.getConnection(plcConnectionString);
               }
           } catch (Exception ex) {
               logger.error("Error while trying to reconnect to the plc");
           }
       }
   ```
   The function runs with a 10 seconds interval, in this time window I simulate 
the disconnection/reconnection of the cable.
   I'm attaching a small video which shows this behavior more clearly. 
   
   I understand that this is a corner case but it can lead to a waste of 
resources which I would like to avoid.
   
   
https://github.com/user-attachments/assets/003b4d80-e600-498a-b7bd-44c8697c4bd9
   
   Thank you.
   
   ### Version
   
   v0.13.0
   
   ### Programming Languages
   
   - [ ] plc4c
   - [ ] plc4go
   - [x] plc4j
   - [ ] plc4net
   - [ ] plc4py
   
   ### Protocols
   
   - [ ] AB-Ethernet
   - [ ] ADS /AMS
   - [ ] BACnet/IP
   - [ ] C-Bus
   - [ ] CANopen
   - [ ] EtherNet/IP
   - [ ] Firmata
   - [ ] IEC-69870
   - [ ] KNXnet/IP
   - [x] Modbus
   - [ ] OPC-UA
   - [ ] Profinet
   - [ ] S7
   - [ ] S7-light


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@plc4x.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to