diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java index d64a77b0b..0b9806962 100644 --- a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java @@ -64,12 +64,10 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer { private static final String CACHE_MANAGER_NAME = "mqttAuthorizationCacheManager"; private static final String CACHE_NAME = "mqttAuthorizationCache"; private static DeviceAccessAuthorizationAdminService deviceAccessAuthorizationAdminService; - private static Cache cache; public DeviceAccessBasedMQTTAuthorizer() { this.MQTTAuthorizationConfiguration = AuthorizationConfigurationManager.getInstance(); - createCache(); deviceAccessAuthorizationAdminService = Feign.builder() .requestInterceptor(new OAuthRequestInterceptor()) .contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder()) @@ -92,6 +90,7 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer { if (!tenantDomainFromTopic.equals(authorizationSubject.getTenantDomain())) { return false; } + Cache cache = getCache(); if (topics.length < 3) { AuthorizationCacheKey authorizationCacheKey = new AuthorizationCacheKey(tenantDomainFromTopic , authorizationSubject.getUsername(), "", ""); @@ -207,16 +206,15 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer { * This method is used to create the Caches. * @return Cachemanager */ - private void createCache() { + private synchronized Cache getCache() { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); try { - CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME); if (MQTTAuthorizationConfiguration.getCacheDuration() == 0) { - cache = cacheManager.getCache(CACHE_NAME); + return Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME).getCache(CACHE_NAME); } else { - cache = cacheManager.createCacheBuilder(CACHE_NAME). + return Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME).createCacheBuilder(CACHE_NAME). setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration( TimeUnit.SECONDS, MQTTAuthorizationConfiguration.getCacheDuration())). setStoreByValue(false).build();