AbstractBaseThread.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.utils.resources;

  21. import org.openspcoop2.utils.Utilities;

  22. /**
  23.  * BaseThread
  24.  *  
  25.  * @author Poli Andrea (apoli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public abstract class AbstractBaseThread extends Thread {

  30.     // VARIABILE PER STOP
  31.     private boolean stop = false;
  32.    
  33.     public boolean isStop() {
  34.         return this.stop;
  35.     }

  36.     public void setStop(boolean stop) {
  37.         this.stop = stop;
  38.     }
  39.    
  40.     // VARIABILE PER CONTROLLARE OGNI QUANTO VIENE ESEGUITA LA BUSINESS LOGIC DEL TIMER
  41.    
  42.     private int timeout = 10; // ogni 10 secondi per default
  43.    
  44.     public int getTimeout() {
  45.         return this.timeout;
  46.     }

  47.     public void setTimeout(int timeout) {
  48.         this.timeout = timeout;
  49.     }
  50.    
  51.     // VARIABILE PER CONTROLLARE LO STATO DI VITA
  52.    
  53.     private boolean finished = false;
  54.    
  55.     public void setFinished(boolean finished) {
  56.         this.finished = finished;
  57.     }
  58.     public void finished() {
  59.         this.finished = true;
  60.     }
  61.     public boolean isFinished() {
  62.         return this.finished;
  63.     }
  64.     public void waitShutdown() {
  65.         this.waitShutdown(200, 60000);
  66.     }
  67.     public void waitShutdown(int checkMs, int maxMs) {
  68.         int msWait = 0;
  69.         while(!this.finished && msWait<maxMs) {
  70.             Utilities.sleep(checkMs);
  71.             msWait = msWait + checkMs;
  72.         }
  73.     }
  74.    
  75.     // METODI DI UTILITA
  76.    
  77.     public void sleepForNextCheck(int maxLimitSleepCount, int sleepMs) {
  78.         // CheckInterval
  79.         if(!this.stop){
  80.             int i=0;
  81.             while(i<maxLimitSleepCount){
  82.                 Utilities.sleep(sleepMs);
  83.                 if(this.isStop()){
  84.                     break; // thread terminato, non lo devo far piu' dormire
  85.                 }
  86.                 i++;
  87.             }
  88.         }
  89.     }
  90.    

  91.     // RUN

  92.     protected boolean initialize() {
  93.         return true;
  94.     }
  95.     protected abstract void process();
  96.     protected void close() {}
  97.    
  98.     @Override
  99.     public void run(){
  100.        
  101.         try {
  102.            
  103.             if(!this.initialize()) {
  104.                 return;
  105.             }
  106.            
  107.             while(!this.isStop()){
  108.                
  109.                 this.process();
  110.                
  111.                 // CheckInterval
  112.                 this.sleepForNextCheck(this.timeout, 1000);
  113.             }
  114.            
  115.         }finally {
  116.             this.finished();
  117.         }
  118.        
  119.         try {
  120.             this.close();
  121.         }catch(Exception t) {
  122.             // ignore
  123.         }
  124.     }

  125. }