RedissonManager.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.core.controllo_traffico.policy.driver.redisson;

  21. import java.util.List;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.redisson.Redisson;
  24. import org.redisson.api.RedissonClient;
  25. import org.redisson.config.Config;
  26. import org.redisson.config.ReadMode;
  27. import org.slf4j.Logger;

  28. /**    
  29.  *  RedissonManager
  30.  *
  31.  * @author Francesco Scarlato (scarlato@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class RedissonManager {
  36.    
  37.     private RedissonManager() {}

  38.     private static Logger logStartup;
  39.     private static Logger log;
  40.    
  41.     private static List<String> connectionUrl = null;
  42.    
  43.     private static final String INIZIALIZZAZIONE_FALLITA = "Inizializzazione RedissonClient fallita: ";
  44.    
  45.     public static synchronized void initialize(Logger logStartup, Logger log, List<String> connectionUrl) {
  46.        
  47.         if(RedissonManager.connectionUrl==null){
  48.             RedissonManager.logStartup = logStartup;
  49.             RedissonManager.log = log;
  50.             RedissonManager.connectionUrl = connectionUrl;
  51.         }
  52.        
  53.     }
  54.    
  55.     private static RedissonClient redisson = null;
  56.     private static synchronized void initRedissonClient(boolean throwInitializingException) throws UtilsException {
  57.         if(redisson==null) {
  58.            
  59.             RedissonManager.logStartup.info("Inizializzazione RedissonClient ...");
  60.             RedissonManager.log.info("Inizializzazione RedissonClient ...");
  61.            
  62.             try {
  63.                 Config redisConf = new Config();
  64.                 redisConf.useClusterServers()
  65.                     .addNodeAddress(RedissonManager.connectionUrl.toArray(new String[1]))
  66.                     .setReadMode(ReadMode.MASTER_SLAVE)
  67.                     .setSslEnableEndpointIdentification(true);
  68.                 redisson = Redisson.create(redisConf);
  69.                 RedissonManager.logStartup.info("Inizializzazione RedissonClient effettuata con successo");
  70.                 RedissonManager.log.info("Inizializzazione RedissonClient effettuata con successo");
  71.             }catch(Exception t) {
  72.                 if(throwInitializingException) {
  73.                     throw new UtilsException(INIZIALIZZAZIONE_FALLITA+t.getMessage(),t);
  74.                 }
  75.                 else {
  76.                     RedissonManager.logStartup.error(INIZIALIZZAZIONE_FALLITA+t.getMessage(),t);
  77.                     RedissonManager.log.error(INIZIALIZZAZIONE_FALLITA+t.getMessage(),t);
  78.                 }
  79.             }
  80.            
  81.         }
  82.     }
  83.     public static RedissonClient getRedissonClient(boolean throwInitializingException) throws UtilsException {
  84.         if(redisson==null) {
  85.             initRedissonClient(throwInitializingException);
  86.         }
  87.         return RedissonManager.redisson;
  88.     }
  89.     public static boolean isRedissonClientInitialized() {
  90.         return RedissonManager.redisson!=null;
  91.     }

  92. }