TimerStatisticheThread.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 org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  22. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  23. import org.openspcoop2.pdd.core.CostantiPdD;
  24. import org.openspcoop2.pdd.logger.MsgDiagnosticiProperties;
  25. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  26. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  27. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  28. import org.openspcoop2.utils.Utilities;
  29. import org.openspcoop2.utils.threads.BaseThread;
  30. import org.slf4j.Logger;


  31. /**    
  32.  * TimerStatisticheThread
  33.  *
  34.  * @author Poli Andrea (poli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class TimerStatisticheThread extends BaseThread{

  39.     public static final String ID_MODULO = "TimerStatistiche";
  40.    
  41.    
  42.     /** Tipo di Intervallo Statistico */
  43.     private TipoIntervalloStatistico tipoStatistica;
  44.    
  45.     /** Logger utilizzato per debug. */
  46.     private Logger logTimer = null;
  47.     private void logError(String msgErrore, Exception e) {
  48.         if(this.logTimer!=null) {
  49.             this.logTimer.error(msgErrore,e);
  50.         }
  51.     }
  52.     private MsgDiagnostico msgDiag = null;

  53.     /** OpenSPCoop2Properties */
  54.     private OpenSPCoop2Properties op2Properties = null;

  55.    
  56.    
  57.    
  58.     /** Costruttore */
  59.     public TimerStatisticheThread(long timeout, TipoIntervalloStatistico tipo) throws TimerException{
  60.    
  61.         // Aspetto inizializzazione di OpenSPCoop (aspetto mezzo minuto e poi segnalo errore)
  62.         int attesa = 90;
  63.         int secondi = 0;
  64.         while( (!OpenSPCoop2Startup.initialize) && (secondi<attesa) ){
  65.             Utilities.sleep(1000);
  66.             secondi++;
  67.         }
  68.         if(secondi>= 90){
  69.             throw new TimerException("Riscontrata inizializzazione OpenSPCoop non effettuata");
  70.         }  

  71.         this.logTimer = OpenSPCoop2Logger.getLoggerOpenSPCoopTimers();
  72.        
  73.         try {
  74.             this.msgDiag = MsgDiagnostico.newInstance(ID_MODULO);
  75.             this.msgDiag.setPrefixMsgPersonalizzati(MsgDiagnosticiProperties.MSG_DIAG_TIMER_STATISTICHE);
  76.             this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER, "Timer"+tipo.getValue());
  77.         } catch (Exception e) {
  78.             String msgErrore = "Riscontrato Errore durante l'inizializzazione del MsgDiagnostico";
  79.             this.logError(msgErrore,e);
  80.             throw new TimerException(msgErrore,e);
  81.         }

  82.         this.msgDiag.logPersonalizzato("avvioInCorso");
  83.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioInCorso"));
  84.        
  85.         try {
  86.             this.op2Properties = OpenSPCoop2Properties.getInstance();
  87.         } catch (Exception e) {
  88.             this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer");
  89.             String msgErrore = "Riscontrato errore durante l'inizializzazione del Reader delle Properties di OpenSPCoop: "+e.getMessage();
  90.             this.logError(msgErrore,e);
  91.             throw new TimerException(msgErrore,e);
  92.         }

  93.         this.tipoStatistica = tipo;
  94.        
  95.         this.setTimeout((int)timeout);
  96.         String sec = "secondi";
  97.         if(this.getTimeout() == 1)
  98.             sec = "secondo";
  99.         this.msgDiag.addKeyword(CostantiPdD.KEY_TIMEOUT, this.getTimeout()+" "+sec);
  100.        
  101.         this.msgDiag.logPersonalizzato("avvioEffettuato");
  102.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioEffettuato"));
  103.     }
  104.    

  105.     @Override
  106.     public void process(){
  107.        
  108.         try{
  109.             // Prendo la gestione
  110.             TimerStatisticheLib timerStatistiche =
  111.                 new TimerStatisticheLib(this.tipoStatistica,this.msgDiag,this.logTimer,this.op2Properties);
  112.            
  113.             timerStatistiche.check();
  114.            
  115.         }catch(Exception e){
  116.             this.msgDiag.logErroreGenerico(e,"TimerStatisticheLib.check()");
  117.             this.logError("Errore generale: "+e.getMessage(),e);
  118.         }
  119.        
  120.     }
  121.    
  122. }