Runnable.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 org.openspcoop2.utils.Utilities;
  22. import org.openspcoop2.utils.UtilsException;

  23. /**
  24.  * Runnable
  25.  *  
  26.  * @author Poli Andrea (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class Runnable extends Thread{

  31.     /** Logger utilizzato per debug. */
  32.     private RunnableLogger log = null;

  33.     // VARIABILE PER STOP
  34.     private boolean stop = false;
  35.    
  36.     public boolean isStop() {
  37.         return this.stop;
  38.     }

  39.     public void setStop(boolean stop) {
  40.         this.stop = stop;
  41.     }
  42.    
  43.     private boolean finished = false;
  44.     public boolean isFinished() {
  45.         return this.finished;
  46.     }

  47.     private int checkIntervalMs = -1; // ogni X ms reinvoco l'instance
  48.     private IRunnableInstance instance;
  49.    
  50.     private boolean initialized = false;
  51.    
  52.     /** Costruttore */
  53.     public Runnable(RunnableLogger runnableLogger, IRunnableInstance instance,int checkIntervalMs) throws UtilsException{
  54.        
  55.         this.initialized = true;
  56.        
  57.         this.log = runnableLogger;
  58.         this.instance = instance;
  59.         this.checkIntervalMs = checkIntervalMs;
  60.        
  61.         this.instance.initialize(runnableLogger);
  62.        
  63.         this.log.info("Avviato");
  64.     }
  65.    
  66.     public Runnable(IRunnableInstance instance,int checkIntervalMs) throws UtilsException{
  67.        
  68.         this.instance = instance;
  69.         this.checkIntervalMs = checkIntervalMs;
  70.        
  71.     }
  72.     public void initialize(RunnableLogger runnableLogger)  throws UtilsException{
  73.                
  74.         this.initialized = true;
  75.        
  76.         this.log = runnableLogger;
  77.        
  78.         this.instance.initialize(runnableLogger);
  79.        
  80.         this.log.info("Avviato");
  81.     }
  82.     public String getIdentifier() {
  83.         return this.instance.getIdentifier();
  84.     }
  85.    
  86.     /**
  87.      * Metodo che fa partire il Thread.
  88.      *
  89.      */
  90.     @Override
  91.     public void run(){
  92.        
  93.         if(this.initialized==false) {
  94.             return; // non inizializzato correttamente
  95.         }
  96.        
  97.         while(this.stop == false){
  98.            
  99.             try{
  100.                 this.instance.check();
  101.             }catch(Exception e){
  102.                 this.log.error("Errore generale: "+e.getMessage(),e);
  103.             }finally{
  104.             }
  105.            
  106.             if(this.instance.isContinuousRunning()==false) {
  107.                 // dopo una invocazione del metodo 'check' il thread termina
  108.                 this.stop = true;
  109.             }
  110.                    
  111.             // CheckInterval
  112.             if(this.stop==false){
  113.                 if(this.checkIntervalMs<=1000) {
  114.                     Utilities.sleep(1000);
  115.                 }
  116.                 else {      
  117.                     int checkIntervalSeconds = this.checkIntervalMs / 1000;
  118.                     int checkInterval_resto = this.checkIntervalMs % 1000;
  119.                     int i=0;
  120.                     while(i<checkIntervalSeconds){
  121.                         Utilities.sleep(1000);
  122.                         if(this.stop){
  123.                             break; // thread terminato, non lo devo far piu' dormire
  124.                         }
  125.                         i++;
  126.                     }
  127.                     if(!this.stop && checkInterval_resto>0){
  128.                         Utilities.sleep(checkInterval_resto);
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.        
  134.         this.finished=true;
  135.     }
  136. }