TimerSvecchiamentoOperazioniRemote.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.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.pdd.services.connector.proxy.IProxyOperationService;
  29. import org.openspcoop2.pdd.services.connector.proxy.ProxyOperationServiceFactory;
  30. import org.openspcoop2.pdd.timers.TimerException;
  31. import org.openspcoop2.utils.Utilities;
  32. import org.openspcoop2.utils.date.DateManager;
  33. import org.openspcoop2.utils.date.DateUtils;
  34. import org.openspcoop2.utils.threads.BaseThread;
  35. import org.slf4j.Logger;


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

  44.     public static final String ID_MODULO = "TimerSvecchiamentoOperazioniRemote";
  45.    
  46.    
  47.     /** Logger utilizzato per debug. */
  48.     private Logger logTimer = null;
  49.     private void logError(String msgErrore, Exception e) {
  50.         if(this.logTimer!=null) {
  51.             this.logTimer.error(msgErrore,e);
  52.         }
  53.     }
  54.     private MsgDiagnostico msgDiag = null;

  55.     /** OpenSPCoop2Properties */
  56.     private OpenSPCoop2Properties op2Properties = null;
  57.    
  58.     private int olderThanMinutes;
  59.    
  60.     private IProxyOperationService proxyOperationService = null;
  61.    
  62.    
  63.     /** Costruttore */
  64.     public TimerSvecchiamentoOperazioniRemote(long timeout) throws TimerException{
  65.    
  66.         // Aspetto inizializzazione di OpenSPCoop (aspetto mezzo minuto e poi segnalo errore)
  67.         int attesa = 90;
  68.         int secondi = 0;
  69.         while( (!OpenSPCoop2Startup.initialize) && (secondi<attesa) ){
  70.             Utilities.sleep(1000);
  71.             secondi++;
  72.         }
  73.         if(secondi>= 90){
  74.             throw new TimerException("Riscontrata inizializzazione OpenSPCoop non effettuata");
  75.         }  

  76.         this.logTimer = OpenSPCoop2Logger.getLoggerOpenSPCoopTimers();
  77.        
  78.         try {
  79.             this.msgDiag = MsgDiagnostico.newInstance(ID_MODULO);
  80.             this.msgDiag.setPrefixMsgPersonalizzati(MsgDiagnosticiProperties.MSG_DIAG_TIMER_SVECCHIAMENTO_OPERAZIONI_ASINCRONE);
  81.             this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER, ID_MODULO);
  82.         } catch (Exception e) {
  83.             String msgErrore = "Riscontrato Errore durante l'inizializzazione del MsgDiagnostico";
  84.             this.logError(msgErrore,e);
  85.             throw new TimerException(msgErrore,e);
  86.         }

  87.         this.msgDiag.logPersonalizzato("avvioInCorso");
  88.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioInCorso"));
  89.        
  90.         try {
  91.             this.op2Properties = OpenSPCoop2Properties.getInstance();
  92.         } catch (Exception e) {
  93.             this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer");
  94.             String msgErrore = "Riscontrato errore durante l'inizializzazione del Reader delle Properties di OpenSPCoop: "+e.getMessage();
  95.             this.logError(msgErrore,e);
  96.             throw new TimerException(msgErrore,e);
  97.         }
  98.        
  99.         String className = this.op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerServiceImplClass();
  100.         try {
  101.             this.proxyOperationService = ProxyOperationServiceFactory.newInstance(className, this.logTimer);
  102.         }catch(Exception e) {
  103.             this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer-ProxyOperationService");
  104.             String msgErrore = "Riscontrato errore durante l'inizializzazione del ProxyOperationService: "+e.getMessage();
  105.             this.logError(msgErrore,e);
  106.             throw new TimerException(msgErrore,e);
  107.         }
  108.        
  109.         this.olderThanMinutes = this.op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerCheckOldRecordDeleteOlderThanMinutes();
  110.        
  111.         this.setTimeout((int)timeout);
  112.         String sec = "secondi";
  113.         if(this.getTimeout() == 1)
  114.             sec = "secondo";
  115.         this.msgDiag.addKeyword(CostantiPdD.KEY_TIMEOUT, this.getTimeout()+" "+sec);
  116.        
  117.         this.msgDiag.logPersonalizzato("avvioEffettuato");
  118.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioEffettuato"));
  119.        
  120.     }
  121.    

  122.     @Override
  123.     public void process(){
  124.        
  125.         try{
  126.            
  127.             long oldestMs =  DateManager.getTimeMillis() - (this.olderThanMinutes*60*1000);
  128.             Date oldest = new Date(oldestMs);
  129.            
  130.             this.msgDiag.addKeyword(CostantiPdD.DATA_AGGIORNAMENTO, DateUtils.getSimpleDateFormatMs().format(oldest));
  131.            
  132.             // Prendo la gestione
  133.             TimerSvecchiamentoOperazioniRemoteLib timer = new TimerSvecchiamentoOperazioniRemoteLib(this.logTimer, this.msgDiag,
  134.                     this.proxyOperationService,
  135.                     oldest);
  136.             timer.check();
  137.            
  138.         }catch(Exception e){
  139.             this.msgDiag.logErroreGenerico(e,"TimerSvecchiamentoOperazioniRemote.check()");
  140.             this.logError("Errore generale: "+e.getMessage(),e);
  141.         }
  142.        
  143.     }
  144.    
  145. }