TimerClusterDinamicoThread.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.pdd.config.DynamicClusterManager;
  22. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  23. import org.openspcoop2.pdd.core.controllo_traffico.policy.PolicyVerifier;
  24. import org.openspcoop2.utils.threads.BaseThread;
  25. import org.slf4j.Logger;


  26. /**    
  27.  * TimerClusterDinamicoThread
  28.  *
  29.  * @author Poli Andrea (poli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class TimerClusterDinamicoThread extends BaseThread{

  34.     private static TimerState STATE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  35.    
  36.     public static TimerState getSTATE() {
  37.         return STATE;
  38.     }
  39.     public static void setSTATE(TimerState sTATE) {
  40.         STATE = sTATE;
  41.     }

  42.     public static final String ID_MODULO = "TimerClusterDinamico";
  43.        
  44.     /** Logger utilizzato per debug. */
  45.     private Logger log = null;
  46.    
  47.     /** Variables */    
  48.     private OpenSPCoop2Properties properties;
  49.     private DynamicClusterManager manager;
  50.    
  51.    
  52.     /** Costruttore */
  53.     public TimerClusterDinamicoThread(Logger log) throws Exception{
  54.    
  55.         this.log = log;
  56.    
  57.         this.properties = OpenSPCoop2Properties.getInstance();
  58.         this.setTimeout(this.properties.getClusterDinamicoRefreshSecondsInterval());

  59.         this.manager = DynamicClusterManager.getInstance();
  60.     }

  61.    
  62.     @Override
  63.     public void process(){
  64.    
  65.         if(TimerState.ENABLED.equals(STATE)) {
  66.        
  67.             try{
  68.                 this.manager.refresh(this.log);
  69.             }catch(Exception e){
  70.                 this.log.error("Errore durante l'aggiornamento del cluster id dinamico: "+e.getMessage(),e);
  71.             }
  72.            
  73.             try{
  74.                 this.manager.deleteOldHostnames(this.log);
  75.             }catch(Exception e){
  76.                 this.log.error("Errore durante lo svecchiamento dei nodi cluster dinamici: "+e.getMessage(),e);
  77.             }
  78.            
  79.            
  80.             try {
  81.                 if(this.manager.isRateLimitingGestioneCluster()) {
  82.                     PolicyVerifier.setListClusterNodes(this.manager.getHostnames(this.log));
  83.                 }
  84.             }catch(Exception e){
  85.                 this.log.error("Errore durante l'aggiornamento della lista dei nodi per il rate limiting: "+e.getMessage(),e);
  86.             }
  87.         }
  88.         else {
  89.             this.log.info("Timer "+ID_MODULO+" disabilitato");
  90.         }
  91.                
  92.     }
  93.    
  94.     @Override
  95.     public void close(){
  96.         this.log.info("Thread per il refresh del cluster id dinamico terminato");
  97.     }
  98.        
  99. }