Last active
March 5, 2019 11:00
-
-
Save brachi-wernick/06581106589c1997c9be5563c3130114 to your computer and use it in GitHub Desktop.
Field Configuration loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FieldsConfigurationProvider { | |
private Logger logger = LoggerFactory.getLogger(FieldsConfigurationProvider.class); | |
private Long lastUpdateTime; | |
private FieldsConfiguration config; | |
private static FieldsConfigurationProvider instance = null; | |
public static String bucketName; | |
public static String filePath; | |
private static long fieldsConfigLoadInterval; | |
private final static Object lock = new Object(); | |
public static FieldsConfigurationProvider getInstance() { | |
if (instance == null) { | |
synchronized (lock) { | |
if (instance == null) { | |
instance = new FieldsConfigurationProvider(); | |
} | |
} | |
} | |
return instance; | |
} | |
private FieldsConfigurationProvider(){ | |
config = loadConfig(filePath); | |
watch(); | |
} | |
public static void init(String bucketName, long fieldsConfigLoadInterval, String filePath ) { | |
FieldsConfigurationProvider.bucketName = bucketName; | |
FieldsConfigurationProvider.fieldsConfigLoadInterval = fieldsConfigLoadInterval; | |
FieldsConfigurationProvider.filePath=filePath; | |
} | |
private void watch() { | |
new Thread(() -> { | |
logger.info("start watching for bucket changes...."); | |
Storage storage = StorageOptions.getDefaultInstance().getService(); | |
Bucket bucket = storage.get(bucketName); | |
while(true) { | |
logger.debug("Checking for new fields configuration in the bucket"); | |
Blob blob = bucket.get(filePath); | |
long currentTime = blob.getUpdateTime(); | |
if (currentTime > lastUpdateTime) { | |
logger.info("Found new fields configuration in the bucket"); | |
config = loadConfig(filePath); | |
} else { | |
logger.debug("Didn't find new fields configuration in bucket"); | |
} | |
try { | |
Thread.sleep(fieldsConfigLoadInterval); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}).start(); | |
} | |
private FieldsConfiguration loadConfig(String path) { | |
try { | |
ObjectMapper mapper = new ObjectMapper(); | |
logger.info("Reading fields configuration from gs://" + bucketName + "/" + path); | |
Storage storage = StorageOptions.getDefaultInstance().getService(); | |
Bucket bucket = storage.get(bucketName); | |
Blob blob = bucket.get(path); | |
lastUpdateTime = blob.getUpdateTime(); | |
ByteArrayInputStream byteStream = new ByteArrayInputStream(blob.getContent()); | |
return mapper.readValue(byteStream, FieldsConfiguration.class); | |
} catch (IOException e) { | |
logger.error("Unable reading fields configuration file", e); | |
} | |
return null; | |
} | |
public FieldsConfiguration getConfig() { | |
return config; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment