GestoreRunnable.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */



  20. package org.openspcoop2.utils.threads;

  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.Executors;
  25. import java.util.concurrent.ThreadPoolExecutor;

  26. import org.openspcoop2.utils.Utilities;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.slf4j.Logger;

  29. /**
  30.  * GestoreRunnable
  31.  *
  32.  *  
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class GestoreRunnable extends Thread{
  38.    
  39.     /** Logger utilizzato per debug. */
  40.     private RunnableLogger log = null;
  41.    
  42.     /** ThreadsPool */
  43.     private ExecutorService threadsPool = null;
  44.     private int poolSize = -1;
  45.     private List<Runnable> threads = new ArrayList<>();
  46.    
  47.     /** Instance */
  48.     private IGestoreRunnableInstance gestoreRunnable;
  49.    
  50.     /** Nome */
  51.     private String name;
  52.    
  53.     // VARIABILE PER STOP
  54.     private boolean stop = false;
  55.    
  56.     public boolean isStop() {
  57.         return this.stop;
  58.     }

  59.     public void setStop(boolean stop) {
  60.         this.stop = stop;
  61.     }
  62.    
  63.     public String getThreadsImage() {
  64.         if(this.threadsPool instanceof ThreadPoolExecutor) {
  65.             ThreadPoolExecutor tpe = (ThreadPoolExecutor) this.threadsPool;
  66.             return
  67.                     String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
  68.                             tpe.getPoolSize(),
  69.                             tpe.getCorePoolSize(),
  70.                             tpe.getActiveCount(),
  71.                             tpe.getCompletedTaskCount(),
  72.                             tpe.getTaskCount(),
  73.                             tpe.isShutdown(),
  74.                             tpe.isTerminated());
  75.         }
  76.         return null;
  77.     }
  78.    
  79.    
  80.     /** Costruttore */
  81.     public GestoreRunnable(String name, int poolSize, IGestoreRunnableInstance gestoreRunnable, Logger log) throws UtilsException{
  82.        
  83.         this.name = name;
  84.         this.log = new RunnableLogger(name, log);
  85.         this.gestoreRunnable = gestoreRunnable;
  86.        
  87.         try {
  88.             if(this.gestoreRunnable!=null) {
  89.                 this.gestoreRunnable.initialize(this.log);
  90.             }
  91.         }catch(Throwable t) {
  92.             throw new UtilsException(t.getMessage(),t);
  93.         }  

  94.         try {

  95.             this.poolSize = poolSize;
  96.             if(this.poolSize>0) {
  97.                 this.threadsPool = Executors.newFixedThreadPool(this.poolSize);
  98.                 this.log.info("Inizializzato correttamente");
  99.             }
  100.             else {
  101.                 this.log.info("Non sono stati definiti threads");
  102.             }
  103.            
  104.         }catch(Exception e) {
  105.             throw new UtilsException("Inizializzazione pool di threads non riuscita: "+e.getMessage(),e);
  106.         }
  107.        
  108.        
  109.     }
  110.    
  111.     /**
  112.      * Metodo che fa partire il Thread.
  113.      *
  114.      */
  115.     @Override
  116.     public void run(){
  117.        
  118.         if(this.threadsPool==null) {
  119.             return; // termino subito
  120.         }
  121.        
  122.         try {      
  123.             // Avvio threads
  124.             for (int i = 0; i < this.poolSize; i++) {
  125.                
  126.                 String threadName = this.name+"-t"+(i+1);
  127.                 Runnable thread = this.gestoreRunnable.newRunnable(new RunnableLogger(threadName,this.log.getLog()));
  128.                 this.log.debug("Avvio thread "+threadName+" ...");
  129.                
  130.                 this.threadsPool.execute(thread);
  131.                 this.threads.add(thread);
  132.                 this.log.debug("Avviato thread "+threadName+"");
  133.             }          
  134.         }catch(Throwable t) {
  135.             this.log.error("Errore durante l'avvio dei threads: "+t.getMessage(),t);
  136.         }
  137.        
  138.        
  139.         while(this.stop == false){
  140.            
  141.             Utilities.sleep(1000);
  142.            
  143.         }
  144.        
  145.         try {      
  146.             this.log.debug("Richiedo sospensione threads ...");
  147.             // Fermo threads
  148.             for (int i = 0; i < this.poolSize; i++) {
  149.                 this.threads.get(i).setStop(true);
  150.             }          
  151.         }catch(Throwable t) {
  152.             this.log.error("Errore durante lo stop dei threads: "+t.getMessage(),t);
  153.         }
  154.            
  155.         try{
  156.             // Attendo chiusura dei threads
  157.             int timeout = 10;
  158.             boolean terminated = false;
  159.             while(terminated == false){
  160.                 this.log.info((this.poolSize)+" threads avviati correttamente, attendo terminazione (timeout "+timeout+"s) ...");
  161.                 for (int i = 0; i < timeout*4; i++) {
  162.                     boolean tmpTerminated = true;
  163.                     for (Runnable processorThread : this.threads) {
  164.                         if(processorThread.isFinished()==false){
  165.                             tmpTerminated = false;
  166.                             break;
  167.                         }
  168.                     }
  169.                     if(tmpTerminated==false){
  170.                         org.openspcoop2.utils.Utilities.sleep(250);
  171.                     }
  172.                     else{
  173.                         terminated = true;
  174.                     }
  175.                 }
  176.             }
  177.            
  178.         }catch(Exception e){
  179.             this.log.error("Errore durante l'attesa della terminazione dei threads: "+e.getMessage(),e);
  180.         }finally{
  181.         }
  182.        
  183.        
  184.        
  185.     }
  186. }