TimerGestoreRepositoryBusteLib.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.sql.Connection;
  22. import java.util.Date;
  23. import java.util.List;

  24. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  25. import org.openspcoop2.pdd.core.CostantiPdD;
  26. import org.openspcoop2.pdd.core.GestoreMessaggi;
  27. import org.openspcoop2.pdd.core.state.OpenSPCoopStateful;
  28. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  29. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  30. import org.openspcoop2.protocol.engine.constants.Costanti;
  31. import org.openspcoop2.protocol.engine.driver.RepositoryBuste;
  32. import org.openspcoop2.protocol.sdk.state.StateMessage;
  33. import org.openspcoop2.utils.TipiDatabase;
  34. import org.openspcoop2.utils.Utilities;
  35. import org.openspcoop2.utils.date.DateManager;
  36. import org.openspcoop2.utils.id.serial.InfoStatistics;
  37. import org.openspcoop2.utils.semaphore.Semaphore;
  38. import org.openspcoop2.utils.semaphore.SemaphoreConfiguration;
  39. import org.openspcoop2.utils.semaphore.SemaphoreMapping;
  40. import org.slf4j.Logger;

  41. /**
  42.  * Implementazione dell'interfaccia {@link TimerGestoreRepositoryBuste} del Gestore
  43.  * dei threads di servizio di OpenSPCoop.
  44.  *
  45.  *  
  46.  * @author Poli Andrea (apoli@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */

  50. public class TimerGestoreRepositoryBusteLib {

  51.     private static TimerState STATE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  52.    
  53.     public static TimerState getSTATE() {
  54.         return STATE;
  55.     }
  56.     public static void setSTATE(TimerState sTATE) {
  57.         STATE = sTATE;
  58.     }

  59.     private MsgDiagnostico msgDiag = null;
  60.     private Logger logTimer = null;
  61.     private OpenSPCoop2Properties propertiesReader = null;
  62.     private boolean logQuery = false;
  63.     private int limit = CostantiPdD.LIMIT_MESSAGGI_GESTORI;
  64.     private boolean orderByQuery;

  65.     private TimerLock timerLock = null;

  66.     /** Semaforo */
  67.     private Semaphore semaphore = null;
  68.     private InfoStatistics semaphore_statistics;

  69.     public TimerGestoreRepositoryBusteLib(MsgDiagnostico msgDiag,Logger log,OpenSPCoop2Properties p,boolean logQuery,int limit,boolean orderByQuery) throws TimerException{
  70.         this.msgDiag = msgDiag;
  71.         this.logTimer = log;
  72.         this.propertiesReader = p;
  73.         this.logQuery = logQuery;
  74.         this.limit = limit;
  75.         this.orderByQuery = orderByQuery;

  76.         // deve essere utilizzato lo stesso lock per GestoreMessaggi, ConsegnaContenuti, GestoreBuste per risolvere problema di eliminazione descritto in GestoreMessaggi metodo deleteMessageWithLock
  77.         if(this.propertiesReader.isMsgGiaInProcessamentoUseLock()) {
  78.             this.timerLock = new TimerLock(TipoLock._getLockGestioneRepositoryMessaggi());
  79.         }
  80.         else {
  81.             this.timerLock = new TimerLock(TipoLock.GESTIONE_PULIZIA_REPOSITORY_BUSTE);
  82.         }

  83.         if(this.propertiesReader.isTimerLockByDatabase()) {
  84.             this.semaphore_statistics = new InfoStatistics();

  85.             SemaphoreConfiguration config = GestoreMessaggi.newSemaphoreConfiguration(this.propertiesReader.getTimerGestoreRepositoryBusteLockMaxLife(),
  86.                     this.propertiesReader.getTimerGestoreRepositoryBusteLockIdleTime());

  87.             TipiDatabase databaseType = TipiDatabase.toEnumConstant(this.propertiesReader.getDatabaseType());
  88.             try {
  89.                 this.semaphore = new Semaphore(this.semaphore_statistics, SemaphoreMapping.newInstance(this.timerLock.getIdLock()),
  90.                         config, databaseType, this.logTimer);
  91.             }catch(Exception e) {
  92.                 throw new TimerException(e.getMessage(),e);
  93.             }
  94.         }
  95.     }

  96.     public void check() throws TimerException {

  97.         // Controllo che il sistema non sia andando in shutdown
  98.         if(OpenSPCoop2Startup.contextDestroyed){
  99.             this.logTimer.error("["+TimerGestoreRepositoryBuste.ID_MODULO+"] Rilevato sistema in shutdown");
  100.             return;
  101.         }

  102.         // Controllo che l'inizializzazione corretta delle risorse sia effettuata
  103.         if(!OpenSPCoop2Startup.initialize){
  104.             this.msgDiag.logFatalError("inizializzazione di OpenSPCoop non effettuata", "Check Inizializzazione");
  105.             String msgErrore = "Riscontrato errore: inizializzazione del Timer o di OpenSPCoop non effettuata";
  106.             this.logTimer.error(msgErrore);
  107.             throw new TimerException(msgErrore);
  108.         }

  109.         // Controllo risorse di sistema disponibili
  110.         if( !TimerMonitoraggioRisorseThread.isRisorseDisponibili()){
  111.             this.logTimer.error("["+TimerGestoreRepositoryBuste.ID_MODULO+"] Risorse di sistema non disponibili: "+TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile().getMessage(),TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile());
  112.             return;
  113.         }
  114.         if( !MsgDiagnostico.gestoreDiagnosticaDisponibile){
  115.             this.logTimer.error("["+TimerGestoreRepositoryBuste.ID_MODULO+"] Sistema di diagnostica non disponibile: "+MsgDiagnostico.motivoMalfunzionamentoDiagnostici.getMessage(),MsgDiagnostico.motivoMalfunzionamentoDiagnostici);
  116.             return;
  117.         }
  118.        
  119.         // Controllo che il timer non sia stato momentaneamente disabilitato
  120.         if(!TimerState.ENABLED.equals(STATE)) {
  121.             this.msgDiag.logPersonalizzato("disabilitato");
  122.             this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("disabilitato"));
  123.             return;
  124.         }
  125.        

  126.         this.msgDiag.logPersonalizzato("controlloInCorso");
  127.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("controlloInCorso"));
  128.         long startControlloRepositoryBuste = DateManager.getTimeMillis();

  129.         OpenSPCoopStateful openspcoopstate = new OpenSPCoopStateful();
  130.         try {

  131.             openspcoopstate.initResource(this.propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), TimerGestoreRepositoryBuste.ID_MODULO, null);
  132.             Connection connectionDB = ((StateMessage)openspcoopstate.getStatoRichiesta()).getConnectionDB();

  133.             // Messaggi da eliminare
  134.             RepositoryBuste repositoryBuste = new RepositoryBuste(openspcoopstate.getStatoRichiesta(),this.logTimer,null);
  135.             boolean trovatiMessaggi = true;

  136.             while(trovatiMessaggi){

  137.                 trovatiMessaggi = false;

  138.                
  139.                 Date now = null;
  140.                 if(!this.propertiesReader.isMsgGiaInProcessamentoUseLock()) {
  141.                     now = DateManager.getDate(); // vedi spiegazione nel metodo deleteMessageByOraRegistrazione di GestoreMessaggi
  142.                 }
  143.                
  144.                
  145.                 // Eliminazione Messaggi from INBOX
  146.                 String causaMessaggiINBOX = "Eliminazione buste (INBOX) marcate logicamente da eliminare";
  147.                 List<String> idMsgINBOX = null;
  148.                 try{
  149.                     GestoreMessaggi.acquireLock(
  150.                             this.semaphore, connectionDB, this.timerLock,
  151.                             this.msgDiag, causaMessaggiINBOX,
  152.                             this.propertiesReader.getTimerGestoreRepositoryBuste_getLockAttesaAttiva(),
  153.                             this.propertiesReader.getTimerGestoreRepositoryBuste_getLockCheckInterval());

  154.                     idMsgINBOX = repositoryBuste.getBusteDaEliminareFromInBox(this.limit,this.logQuery,
  155.                             this.propertiesReader.isForceIndex(),this.propertiesReader.isRepositoryBusteFiltraBusteScaduteRispettoOraRegistrazione(),
  156.                             this.orderByQuery, now);
  157.                     int gestiti = 0;
  158.                     if(idMsgINBOX!=null && idMsgINBOX.size()>0){
  159.                         if(this.logQuery)
  160.                             this.logTimer.info("Trovate "+idMsgINBOX.size()+" buste da eliminare nel repository (INBOX) ...");
  161.                         trovatiMessaggi = true;

  162.                         this.msgDiag.addKeyword(CostantiPdD.KEY_TIPO_MESSAGGIO,Costanti.INBOX);
  163.                         for(int i=0; i<idMsgINBOX.size(); i++){

  164.                             String idMsgDaEliminare = idMsgINBOX.get(i);
  165.                             this.msgDiag.addKeyword(CostantiPdD.KEY_ID_MESSAGGIO_DA_ELIMINARE,idMsgDaEliminare);

  166.                             try{
  167.                                 try{
  168.                                     GestoreMessaggi.updateLock(
  169.                                             this.semaphore, connectionDB, this.timerLock,
  170.                                             this.msgDiag, "Eliminazione busta INBOX con id ["+idMsgDaEliminare+"] ...");
  171.                                 }catch(Throwable e){
  172.                                     this.msgDiag.logErroreGenerico(e,"EliminazioneBustaInbox("+idMsgDaEliminare+")-UpdateLock");
  173.                                     this.logTimer.error("ErroreEliminazioneBustaInbox("+idMsgDaEliminare+")-UpdateLock: "+e.getMessage(),e);
  174.                                     break;
  175.                                 }

  176.                                 repositoryBuste.eliminaBustaFromInBox(idMsgDaEliminare,now);

  177.                                 this.msgDiag.logPersonalizzato("eliminazioneMessaggio");
  178.                                 if(this.logQuery)
  179.                                     this.logTimer.debug(this.msgDiag.getMessaggio_replaceKeywords("eliminazioneMessaggio"));

  180.                                 gestiti++;

  181.                             }catch(Exception e){
  182.                                 this.msgDiag.logErroreGenerico(e,"EliminazioneBustaInbox("+idMsgDaEliminare+")");
  183.                                 this.logTimer.error("ErroreEliminazioneBustaInbox("+idMsgDaEliminare+"): "+e.getMessage(),e);
  184.                             }
  185.                         }

  186.                         if(this.logQuery)
  187.                             this.logTimer.info("Eliminate "+gestiti+" buste nel repository (INBOX)");
  188.                     }
  189.                     else{
  190.                         if(this.logQuery)
  191.                             this.logTimer.info("Non sono state trovate buste da eliminare nel repository (INBOX)");
  192.                     }
  193.                 }finally{
  194.                     try{
  195.                         GestoreMessaggi.releaseLock(
  196.                                 this.semaphore, connectionDB, this.timerLock,
  197.                                 this.msgDiag, causaMessaggiINBOX);
  198.                     }catch(Exception e){
  199.                         // ignore
  200.                     }
  201.                 }

  202.                 //  Eliminazione Messaggi from OUTBOX
  203.                 String causaMessaggiOUTBOX = "Eliminazione buste (OUTBOX) marcate logicamente da eliminare";
  204.                 List<String> idMsgOUTBOX = null;
  205.                 try{
  206.                     GestoreMessaggi.acquireLock(
  207.                             this.semaphore, connectionDB, this.timerLock,
  208.                             this.msgDiag, causaMessaggiOUTBOX,
  209.                             this.propertiesReader.getTimerGestoreRepositoryBuste_getLockAttesaAttiva(),
  210.                             this.propertiesReader.getTimerGestoreRepositoryBuste_getLockCheckInterval());

  211.                     idMsgOUTBOX = repositoryBuste.getBusteDaEliminareFromOutBox(this.limit,this.logQuery,
  212.                             this.propertiesReader.isForceIndex(),this.propertiesReader.isRepositoryBusteFiltraBusteScaduteRispettoOraRegistrazione(),
  213.                             this.orderByQuery,now);
  214.                     int gestiti = 0;
  215.                     if(idMsgOUTBOX!=null && idMsgOUTBOX.size()>0){
  216.                         if(this.logQuery)
  217.                             this.logTimer.info("Trovate "+idMsgOUTBOX.size()+" buste da eliminare nel repository (OUTBOX) ...");
  218.                         trovatiMessaggi = true;

  219.                         this.msgDiag.addKeyword(CostantiPdD.KEY_TIPO_MESSAGGIO,Costanti.OUTBOX);
  220.                         for(int i=0; i<idMsgOUTBOX.size(); i++){

  221.                             String idMsgDaEliminare = idMsgOUTBOX.get(i);
  222.                             this.msgDiag.addKeyword(CostantiPdD.KEY_ID_MESSAGGIO_DA_ELIMINARE,idMsgDaEliminare);

  223.                             try{
  224.                                 try{
  225.                                     GestoreMessaggi.updateLock(
  226.                                             this.semaphore, connectionDB, this.timerLock,
  227.                                             this.msgDiag, "Eliminazione busta OUTBOX con id ["+idMsgDaEliminare+"] ...");
  228.                                 }catch(Throwable e){
  229.                                     this.msgDiag.logErroreGenerico(e,"EliminazioneBustaOutbox("+idMsgDaEliminare+")-UpdateLock");
  230.                                     this.logTimer.error("ErroreEliminazioneBustaOutbox("+idMsgDaEliminare+")-UpdateLock: "+e.getMessage(),e);
  231.                                     break;
  232.                                 }

  233.                                 repositoryBuste.eliminaBustaFromOutBox(idMsgDaEliminare,now);

  234.                                 this.msgDiag.logPersonalizzato("eliminazioneMessaggio");
  235.                                 if(this.logQuery)
  236.                                     this.logTimer.debug(this.msgDiag.getMessaggio_replaceKeywords("eliminazioneMessaggio"));

  237.                                 gestiti++;

  238.                             }catch(Exception e){
  239.                                 this.msgDiag.logErroreGenerico(e,"EliminazioneBustaOutbox("+idMsgDaEliminare+")");
  240.                                 this.logTimer.error("ErroreEliminazioneBustaOutbox("+idMsgDaEliminare+"): "+e.getMessage(),e);
  241.                             }
  242.                         }

  243.                         if(this.logQuery)
  244.                             this.logTimer.info("Eliminate "+gestiti+" buste nel repository (OUTBOX)");
  245.                     }
  246.                     else{
  247.                         if(this.logQuery)
  248.                             this.logTimer.info("Non sono state trovate buste da eliminare nel repository (OUTBOX)");
  249.                     }
  250.                 }finally{
  251.                     try{
  252.                         GestoreMessaggi.releaseLock(
  253.                                 this.semaphore, connectionDB, this.timerLock,
  254.                                 this.msgDiag, causaMessaggiOUTBOX);
  255.                     }catch(Exception e){
  256.                         // ignore
  257.                     }
  258.                 }


  259.                 if(trovatiMessaggi){
  260.                     this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER_GESTORE_REPOSITORY_BUSTE_NUM_MSG_INBOX, idMsgINBOX!=null ? idMsgINBOX.size()+"" : 0+"");
  261.                     this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER_GESTORE_REPOSITORY_BUSTE_NUM_MSG_OUTBOX, idMsgOUTBOX!=null ? idMsgOUTBOX.size()+"" : 0+"");
  262.                     this.msgDiag.logPersonalizzato("ricercaMessaggiDaEliminare");
  263.                 }

  264.             }


  265.             long endControlloRepositoryBuste = DateManager.getTimeMillis();
  266.             long diff = (endControlloRepositoryBuste-startControlloRepositoryBuste);
  267.             this.logTimer.info("Controllo Repository Buste terminato in "+Utilities.convertSystemTimeIntoStringMillisecondi(diff, true));

  268.         }
  269.         catch(TimerLockNotAvailableException t) {
  270.             // msg diagnostico emesso durante l'emissione dell'eccezione
  271.             this.logTimer.info(t.getMessage(),t);
  272.         }
  273.         catch (Exception e) {
  274.             this.msgDiag.logErroreGenerico(e,"GestioneBuste");
  275.             this.logTimer.error("Riscontrato errore durante l'eliminazione delle buste: "+e.getMessage(),e);
  276.         }finally{
  277.             if(openspcoopstate!=null)
  278.                 openspcoopstate.releaseResource();
  279.         }
  280.     }

  281. }