package com.mridul.software.automation.spring.configuration;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.internal.concurrent.ConcurrentHashSet;
import com.mridul.software.automation.spring.context.properties.gemfire.GemfireCustomProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import javax.annotation.PreDestroy;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@Configuration
@ConditionalOnProperty(value = "module.gemfire.enabled", havingValue = "true")
@Slf4j
@EnableGemfireRepositories
public class GemfireConfig {
private static final ThreadLocal<ClientCache> CLIENT_CACHE_THREAD_LOCAL = new ThreadLocal<>();
////TODO- probably need to refactor to just load from gemfire.properties
@Bean
//keep this name as gemfire generates its own custom gemfireProperties
@Qualifier("gemfireMiscProperties")
public Properties gemfireMiscProperties(GemfireCustomProperties gemfireCustomProperties) {
GemfireCustomProperties.Security security = gemfireCustomProperties.getSecurity();
Properties connProps = new Properties();
connProps.setProperty("security-username", security.getUserName());
connProps.setProperty("security-password", security.getPassword());
connProps.setProperty("security-client-auth-init", security.getClient().getAuthenticationInitializer());
GemfireCustomProperties.Cache cache = gemfireCustomProperties.getCache();
connProps.setProperty("log-level", cache.getLogLevel());
connProps.setProperty("name", cache.getName());
return connProps;
}
@Bean
public ClientCache createClientCache(@Qualifier("gemfireMiscProperties") Properties properties,
GemfireCustomProperties gemfireCustomProperties) {
final ClientCacheFactory clientCacheFactory = createClientCacheFactory(properties, gemfireCustomProperties);
//add locators
gemfireCustomProperties.getPool().getLocators().forEach(
e -> clientCacheFactory.addPoolLocator(
(e.split("\\[")[0]),
Integer.parseInt(e.split("\\[")[1].replaceAll("]", ""))
));
ClientCache clientCache = clientCacheFactory.create();
CLIENT_CACHE_THREAD_LOCAL.set(clientCache);
return clientCache;
}
@PreDestroy
public void destroyClientCache() {
ClientCache clientCache = CLIENT_CACHE_THREAD_LOCAL.get();
if (clientCache != null) {
log.debug("closing gemfire client cache through the spring destroy lifecycle method!");
clientCache.close();
} else {
log.debug("client cache was already closed!");
}
}
private ClientCacheFactory createClientCacheFactory(@Qualifier("gemfireMiscProperties") Properties properties,
GemfireCustomProperties gemfireCustomProperties) {
GemfireCustomProperties.Pool pool = gemfireCustomProperties.getPool();
ClientCacheFactory clientCacheFactory = new ClientCacheFactory(properties)
.setPoolSubscriptionEnabled(pool.isSubscriptionEnabled())
.setPoolFreeConnectionTimeout(pool.getFreeConnectionTimeout())
.setPoolReadTimeout(pool.getReadTimeout());
GemfireCustomProperties.Cache cache = gemfireCustomProperties.getCache();
clientCacheFactory = clientCacheFactory.setPdxReadSerialized(cache.isPdxReadSerialized());
return clientCacheFactory;
}
@Bean
public Set<Region<?, ?>> getRegions(GemfireCustomProperties gemfireCustomProperties, ClientCache clientCache) {
Set<Region<?, ?>> gemfireRegions = new ConcurrentHashSet<>();
Set<String> regionNames = gemfireCustomProperties.getRegion().getRegionNames();
for (String regionName : regionNames) {
Region<?, ?> region = clientCache.getRegion(regionName);
if (region == null) {
region = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
}
log.debug("Region name : {}", region.getName());
gemfireRegions.add(region);
}
log.debug("all regions : {}", clientCache.rootRegions());
return gemfireRegions;
}
@Bean
public Map<String, GemfireTemplate> getGemfireTemplates(Set<Region<?, ?>> regions) {
Map<String, GemfireTemplate> gemfireTemplates = new ConcurrentHashMap<>();
regions.forEach(e -> {
GemfireTemplate template = new GemfireTemplate(e);
String templateName = e.getName() + "Template";
log.debug("Template Name : {}", templateName);
gemfireTemplates.put(templateName, template);
log.debug("GemfireTemplate created with name : {}", e.getName());
});
return gemfireTemplates;
}
}
Comments
Post a Comment