TimerSvecchiamentoOperazioniRemoteLib.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.proxy;


  21. import java.util.Date;

  22. import org.openspcoop2.core.commons.CoreException;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.pdd.core.CostantiPdD;
  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.pdd.services.connector.proxy.IProxyOperationService;
  29. import org.openspcoop2.pdd.timers.TimerException;
  30. import org.openspcoop2.pdd.timers.TimerMonitoraggioRisorseThread;
  31. import org.openspcoop2.pdd.timers.TimerState;
  32. import org.openspcoop2.utils.Utilities;
  33. import org.openspcoop2.utils.date.DateManager;
  34. import org.slf4j.Logger;


  35. /**    
  36.  * TimerSvecchiamentoOperazioniRemoteLib
  37.  *
  38.  * @author Poli Andrea (poli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class TimerSvecchiamentoOperazioniRemoteLib {

  43.     private static TimerState state = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  44.     public static TimerState getState() {
  45.         return state;
  46.     }
  47.     public static void setState(TimerState stateParam) {
  48.         state = stateParam;
  49.     }
  50.    
  51.     private OpenSPCoop2Properties op2Properties;
  52.     private String tipoDatabase = null;
  53.    
  54.     private Logger logCore = null;
  55.     private Logger logTimer = null;
  56.     private MsgDiagnostico msgDiag = null;
  57.    
  58.     private void logCoreInfo(String msg) {
  59.         if(this.logCore!=null) {
  60.             this.logCore.info(msg);
  61.         }
  62.     }
  63.    
  64.     private void logTimerError(String msgErrore, Throwable e) {
  65.         if(this.logTimer!=null) {
  66.             this.logTimer.error(msgErrore,e);
  67.         }
  68.     }
  69.     private void logTimerError(String msgErrore) {
  70.         if(this.logTimer!=null) {
  71.             this.logTimer.error(msgErrore);
  72.         }
  73.     }
  74.     private void logTimerInfo(String msg) {
  75.         if(this.logTimer!=null) {
  76.             this.logTimer.info(msg);
  77.         }
  78.     }

  79.     private Date oldest;
  80.    
  81.     private IProxyOperationService proxyOperationService;
  82.    
  83.     /** Costruttore */
  84.     public TimerSvecchiamentoOperazioniRemoteLib(Logger logTimer, MsgDiagnostico msgDiag,
  85.             IProxyOperationService proxyOperationService,
  86.             Date oldest) throws TimerException{
  87.        
  88.         this.op2Properties = OpenSPCoop2Properties.getInstance();
  89.         this.tipoDatabase = this.op2Properties.getDatabaseType();
  90.         if(this.tipoDatabase==null){
  91.             throw new TimerException("Tipo Database non definito");
  92.         }
  93.        
  94.         boolean debug = this.op2Properties.isProxyReadJMXResourcesAsyncProcessByTimerDebug();
  95.        
  96.         this.logCore = OpenSPCoop2Logger.getLoggerOpenSPCoopGestoreOperazioniRemote(debug);
  97.         this.logTimer = logTimer;
  98.         this.msgDiag = msgDiag;
  99.        
  100.         this.oldest = oldest;
  101.        
  102.         this.proxyOperationService = proxyOperationService;
  103.        
  104.     }
  105.    
  106.     public void check() throws TimerException {
  107.        
  108.         // Controllo che il sistema non sia andando in shutdown
  109.         if(OpenSPCoop2Startup.contextDestroyed){
  110.             this.logTimerError("["+TimerSvecchiamentoOperazioniRemote.ID_MODULO+"] Rilevato sistema in shutdown");
  111.             return;
  112.         }

  113.         // Controllo che l'inizializzazione corretta delle risorse sia effettuata
  114.         if(!OpenSPCoop2Startup.initialize){
  115.             this.msgDiag.logFatalError("inizializzazione di OpenSPCoop non effettuata", "Check Inizializzazione");
  116.             String msgErrore = "Riscontrato errore: inizializzazione del Timer o di OpenSPCoop non effettuata";
  117.             this.logTimerError(msgErrore);
  118.             throw new TimerException(msgErrore);
  119.         }

  120.         // Controllo risorse di sistema disponibili
  121.         if( !TimerMonitoraggioRisorseThread.isRisorseDisponibili()){
  122.             this.logTimerError("["+TimerSvecchiamentoOperazioniRemote.ID_MODULO+"] Risorse di sistema non disponibili: "+TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile().getMessage(),TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile());
  123.             return;
  124.         }
  125.         if( !MsgDiagnostico.gestoreDiagnosticaDisponibile){
  126.             this.logTimerError("["+TimerSvecchiamentoOperazioniRemote.ID_MODULO+"] Sistema di diagnostica non disponibile: "+MsgDiagnostico.motivoMalfunzionamentoDiagnostici.getMessage(),MsgDiagnostico.motivoMalfunzionamentoDiagnostici);
  127.             return;
  128.         }
  129.        
  130.         boolean enabled = TimerState.ENABLED.equals(TimerSvecchiamentoOperazioniRemoteLib.state);
  131.         if(!enabled) {
  132.             emitDiagnosticLog("disabilitato");
  133.             return;
  134.         }
  135.        
  136.         long startControlloTimer = DateManager.getTimeMillis();
  137.            
  138.         try{

  139.             process();
  140.            
  141.             // end
  142.             long endControlloTimer = DateManager.getTimeMillis();
  143.             long diff = (endControlloTimer-startControlloTimer);
  144.             this.logTimerInfo("Svecchiamento operazioni remote terminata in "+Utilities.convertSystemTimeIntoStringMillisecondi(diff, true));
  145.            
  146.            
  147.         }
  148.         catch (Exception e) {
  149.             this.msgDiag.logErroreGenerico(e,"TimerSvecchiamentoOperazioniRemoteLib");
  150.             this.logTimerError("Riscontrato errore durante lo svecchiamento delle operazioni remote: "+ e.getMessage(),e);
  151.         }
  152.            
  153.     }

  154.     private void process() throws CoreException {
  155.        
  156.         try {
  157.             long startGenerazione = DateManager.getTimeMillis();
  158.            
  159.             emitDiagnosticLog("svecchiamento.inCorso");
  160.            
  161.             int eliminate = this.proxyOperationService.clear(this.oldest);
  162.    
  163.             this.msgDiag.addKeyword(CostantiPdD.KEY_NUMERO_OPERAZIONI, eliminate+"");
  164.            
  165.             long endGenerazione = DateManager.getTimeMillis();
  166.             String tempoImpiegato = Utilities.convertSystemTimeIntoStringMillisecondi((endGenerazione-startGenerazione), true);
  167.             this.msgDiag.addKeyword(CostantiPdD.KEY_TEMPO_GESTIONE, tempoImpiegato);
  168.            
  169.             emitDiagnosticLog("svecchiamento.effettuata");
  170.            
  171.         }catch(Exception e) {
  172.             this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, e.getMessage());
  173.             emitDiagnosticLog("svecchiamento.fallita");
  174.         }
  175.     }
  176.    

  177.     private void emitDiagnosticLog(String code) {
  178.         this.msgDiag.logPersonalizzato(code);
  179.         String msg = this.msgDiag.getMessaggio_replaceKeywords(code);
  180.         emitLog(msg);
  181.     }
  182.     private void emitLog(String msg) {
  183.         this.logCoreInfo(msg);
  184.         this.logTimerInfo(msg);
  185.     }
  186.    
  187.    
  188. }