TimerThresholdThread.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.pdd.timers;

  21. import java.util.List;

  22. import org.openspcoop2.pdd.config.ClassNameProperties;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.pdd.core.CostantiPdD;
  25. import org.openspcoop2.pdd.core.threshold.IThreshold;
  26. import org.openspcoop2.pdd.core.threshold.ThresholdException;
  27. import org.openspcoop2.pdd.logger.MsgDiagnosticiProperties;
  28. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  29. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  30. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  31. import org.openspcoop2.utils.resources.Loader;
  32. import org.openspcoop2.utils.threads.BaseThread;
  33. import org.slf4j.Logger;

  34. /**
  35.  * Thread per la gestione del Threshold
  36.  *
  37.  *  
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class TimerThresholdThread extends BaseThread{

  43.     private static TimerState STATE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  44.    
  45.     public static TimerState getSTATE() {
  46.         return STATE;
  47.     }
  48.     public static void setSTATE(TimerState sTATE) {
  49.         STATE = sTATE;
  50.     }

  51.     /** Indicazione di uno spazio delle risorse di sistema corretto */
  52.     public static boolean freeSpace = true;
  53.    
  54.      /** Variabile che indica il Nome del modulo dell'architettura di OpenSPCoop rappresentato da questa classe */
  55.     public static final String ID_MODULO = "GestoreThreshold";
  56.      
  57.    
  58.     /** Properties Reader */
  59.     private OpenSPCoop2Properties propertiesReader;
  60.     /** MessaggioDiagnostico */
  61.     private MsgDiagnostico msgDiag;
  62.     /** Gestore Soglia */
  63.     private IThreshold[] gestore = null;
  64.     /** tipi Gestore Soglia */
  65.     private List<String> tipiThreshold = null;
  66.     /** Logger */
  67.     private Logger log;
  68.     /** LastImage */
  69.     private boolean lastCheck = true;
  70.    
  71.     /** Costruttore */
  72.     public TimerThresholdThread() throws ThresholdException{
  73.         this.propertiesReader = OpenSPCoop2Properties.getInstance();
  74.        
  75.         this.msgDiag = MsgDiagnostico.newInstance(TimerThresholdThread.ID_MODULO);
  76.         this.msgDiag.setPrefixMsgPersonalizzati(MsgDiagnosticiProperties.MSG_DIAG_TIMER_THRESHOLD);
  77.         this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER_THRESHOLD, TimerThresholdThread.ID_MODULO);
  78.        
  79.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopResources();
  80.         this.tipiThreshold = this.propertiesReader.getRepositoryThresholdTypes();
  81.         this.gestore = new IThreshold[this.tipiThreshold.size()];
  82.         Loader loader = Loader.getInstance();
  83.         for(int i=0;i<this.tipiThreshold.size();i++){
  84.             String tipoClass = ClassNameProperties.getInstance().getThreshold(this.tipiThreshold.get(i));
  85.             if(tipoClass == null){
  86.                 throw new ThresholdException("Riscontrato errore durante l'istanziazione del threshold di tipo ["+this.tipiThreshold.get(i)+"]: is null");
  87.             }
  88.             try{
  89.                 this.gestore[i] = (IThreshold) loader.newInstance(tipoClass);
  90.                 this.gestore[i].toString();
  91.             }catch(Exception e){
  92.                 //e.printStackTrace();
  93.                 throw new ThresholdException("Riscontrato errore durante l'istanziazione del threshold di tipo ["+this.tipiThreshold.get(i)+"]: "+e.getMessage());
  94.             }
  95.         }
  96.     }
  97.    

  98.     @Override
  99.     public void process(){
  100.         // nop: ho ridefinito il metodo run
  101.     }
  102.    
  103.     @Override
  104.     public void run(){
  105.        
  106.         try {
  107.        
  108.             String sec = "secondi";
  109.             if(this.propertiesReader.getRepositoryThresholdCheckInterval() == 1)
  110.                 sec = "secondo";
  111.             this.msgDiag.addKeyword(CostantiPdD.KEY_TIMEOUT, this.propertiesReader.getRepositoryThresholdCheckInterval()+" "+sec);
  112.             this.msgDiag.logPersonalizzato("avvioEffettuato");
  113.            
  114.             while(!this.isStop()){
  115.                
  116.                 // Controllo che il sistema non sia andando in shutdown
  117.                 if(OpenSPCoop2Startup.contextDestroyed){
  118.                     this.log.error("["+TimerThresholdThread.ID_MODULO+"] Rilevato sistema in shutdown");
  119.                     return;
  120.                 }
  121.                
  122.                 // Controllo risorse di sistema disponibili
  123.                 if( !TimerMonitoraggioRisorseThread.isRisorseDisponibili()){
  124.                     this.log.error("["+TimerThresholdThread.ID_MODULO+"] Risorse di sistema non disponibili: "+TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile().getMessage(),TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile());
  125.                     this.sleepForNextCheck((int) this.propertiesReader.getRepositoryThresholdCheckInterval(), 1000);
  126.                     continue;
  127.                 }
  128.                
  129.                 if(TimerState.ENABLED.equals(STATE)) {
  130.                
  131.                     boolean checkThreshold = true;
  132.                     for(int i=0;i<this.tipiThreshold.size();i++){
  133.                         this.log.debug("Verifica '"+this.tipiThreshold.get(i)+"' in corso ...");
  134.                         this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER_THRESHOLD_TIPO, this.tipiThreshold.get(i));
  135.                         this.msgDiag.logPersonalizzato("controlloInCorso");
  136.                         try{
  137.                             checkThreshold = this.gestore[i].check(this.propertiesReader.getRepositoryThresholdParameters(this.tipiThreshold.get(i)));
  138.                         }catch(Exception e){
  139.                             this.msgDiag.logErroreGenerico(e,"checkThreshold("+this.tipiThreshold.get(i)+")");
  140.                             this.log.debug("Controllo threshould non riuscito (tipo:"+this.tipiThreshold.get(i)+")",e);
  141.                             checkThreshold = false;
  142.                         }
  143.                         this.log.debug("Verifica '"+this.tipiThreshold.get(i)+"' completato con esito "+(checkThreshold?"ok":"ko"));
  144.                         if(checkThreshold == false)
  145.                             break;
  146.                     }
  147.                        
  148.                    
  149.                     // avvisi
  150.                     if(checkThreshold==false && this.lastCheck==true){
  151.                         this.msgDiag.logPersonalizzato("risorsaNonDisponibile");
  152.                     }else if(checkThreshold==true && this.lastCheck==false){
  153.                         this.msgDiag.logPersonalizzato("risorsaRitornataDisponibile");
  154.                     }
  155.                     this.lastCheck = checkThreshold;
  156.                     TimerThresholdThread.freeSpace = checkThreshold;
  157.                    
  158.                 }
  159.                 else {
  160.                     this.log.info("Timer "+ID_MODULO+" disabilitato");
  161.                 }
  162.                
  163.                
  164.                 // CheckInterval
  165.                 this.sleepForNextCheck((int) this.propertiesReader.getRepositoryThresholdCheckInterval(), 1000);
  166.                
  167.             }
  168.         }finally {
  169.             this.finished();
  170.         }
  171.     }
  172.    
  173. }