UniversallyUniqueIdentifierProducer.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.id;

  21. import java.util.concurrent.ArrayBlockingQueue;
  22. import java.util.concurrent.TimeUnit;

  23. import org.openspcoop2.utils.Utilities;
  24. import org.openspcoop2.utils.resources.AbstractBaseThread;
  25. import org.slf4j.Logger;

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

  34.     private static UniversallyUniqueIdentifierProducer staticInstance;
  35.     public static synchronized void initialize(int buffer, Logger log) {
  36.         if(staticInstance==null) {
  37.             staticInstance = new UniversallyUniqueIdentifierProducer(buffer, log);
  38.         }
  39.     }
  40.     public static UniversallyUniqueIdentifierProducer getInstance() {
  41.         // volutamente non inizializzo se null, viene fatto allo startup
  42.         // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  43.         if (staticInstance == null) {
  44.             synchronized (UniversallyUniqueIdentifierProducer.class) {
  45.                 if (staticInstance == null) {
  46.                     return null;
  47.                 }
  48.             }
  49.         }
  50.         return staticInstance;
  51.     }
  52.    
  53.    
  54.     private ArrayBlockingQueue<IUniqueIdentifier> idsQueue;
  55.     private Logger log;
  56.    
  57.     private UniversallyUniqueIdentifierProducer(int buffer, Logger log) {
  58.         this.setTimeout(-1); // equivale ad un while true
  59.         this.idsQueue = new ArrayBlockingQueue<>(buffer);
  60.         this.log = log;
  61.         String msg = "Started UUID producer with buffer size: " + buffer;
  62.         this.log.info(msg);
  63.     }
  64.    
  65.     public IUniqueIdentifier newUniqueIdentifier() throws UniqueIdentifierException {
  66.         try {
  67.             return this.idsQueue.take();
  68.         } catch ( InterruptedException e ) {
  69.             Thread.currentThread().interrupt();
  70.             throw new UniqueIdentifierException( e );
  71.         } catch ( Exception e ) {
  72.             throw new UniqueIdentifierException( e );
  73.         } /**finally {
  74.             System.out.println("PRESO DALLA CODA");
  75.         }*/
  76.     }
  77.    
  78.     @Override
  79.     protected void process() {
  80.         try {
  81.             /**System.out.println("RUN");*/
  82.             IUniqueIdentifier newUUID = UniqueIdentifierManager.newUniqueIdentifier(false);
  83.             //this.idsQueue.put( newUUID ); causa un blocco nello shutdown
  84.             while(!this.isStop()){
  85.                 try {
  86.                     boolean insert = this.idsQueue.offer(newUUID, 1000, TimeUnit.MILLISECONDS);
  87.                     if(insert) {
  88.                         break;
  89.                     }
  90.                     /**else {
  91.                         System.out.println("ATTENDO");
  92.                     }*/
  93.                 }catch(InterruptedException ie) {
  94.                     Thread.currentThread().interrupt();
  95.                     /**System.out.println("IE!");*/
  96.                 }
  97.             }
  98.             /**System.out.println("AGGIUNTO IN CODA");*/
  99.         }
  100.         catch( Exception t ) {
  101.             if(t instanceof InterruptedException) {
  102.                 Thread.currentThread().interrupt();
  103.             }
  104.             this.log.error("UniversallyUniqueIdentifierProducer - generation failed: "+t.getMessage(),t);
  105.             Utilities.sleep(5000); // per non continuare nel loop di errori (non dovrebbe comunque mai entrare in questo catch)
  106.         }
  107.     }

  108. }