Updated cache behavior
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@134003 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
e040d787f0
commit
fb23cb9789
|
@ -47,6 +47,8 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
|
||||
private static Logger logger = LoggerFactory
|
||||
.getLogger(AccountingManagerServiceImpl.class);
|
||||
|
||||
private static AccountingCache accountingCache;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
|
@ -54,13 +56,31 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
@Override
|
||||
public void init() throws ServletException {
|
||||
super.init();
|
||||
System.out.println("Fix JAXP: jdk.xml.entityExpansionLimit=0");
|
||||
logger.info("Fix JAXP: jdk.xml.entityExpansionLimit=0");
|
||||
System.setProperty("jdk.xml.entityExpansionLimit", "0");
|
||||
|
||||
System.out.println("initializing AccountingManager");
|
||||
logger.info("initializing AccountingManager");
|
||||
|
||||
try {
|
||||
accountingCache = new AccountingCache();
|
||||
} catch (ServiceException e) {
|
||||
logger.error("Error initializing AccountingCache: "+e.getLocalizedMessage(),e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
logger.info("Clear AccountingCache");
|
||||
try {
|
||||
accountingCache.finalize();
|
||||
} catch (Throwable e) {
|
||||
logger.error("Error initializing AccountingCache: "+e.getLocalizedMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
|
@ -125,11 +145,9 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
String token = SessionUtil.getToken(aslSession);
|
||||
logger.debug("UserToken: " + token);
|
||||
|
||||
AccountingCache accountingCache = SessionUtil
|
||||
.getAccountingCache(session,token);
|
||||
String key = new String(accountingType.name() + "_"
|
||||
+ seriesRequest.toString());
|
||||
SeriesResponse seriesResponse = accountingCache.get(key);
|
||||
SeriesResponse seriesResponse = accountingCache.get(token, key);
|
||||
|
||||
if (seriesResponse == null) {
|
||||
|
||||
|
@ -141,7 +159,7 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
|
|||
}
|
||||
seriesResponse = accountingCaller.getSeries(accountingType,
|
||||
seriesRequest);
|
||||
accountingCache.put(key, seriesResponse);
|
||||
accountingCache.put(token, key, seriesResponse);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -112,18 +112,7 @@ public class SessionUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static AccountingCache getAccountingCache(HttpSession httpSession,
|
||||
String token) throws ServiceException {
|
||||
AccountingCache accountingCache = (AccountingCache) httpSession
|
||||
.getAttribute(Constants.ACCOUNTING_CACHE);
|
||||
if (accountingCache == null) {
|
||||
accountingCache = new AccountingCache(token);
|
||||
httpSession.setAttribute(Constants.ACCOUNTING_CACHE,
|
||||
accountingCache);
|
||||
}
|
||||
return accountingCache;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static Context getContext(ASLSession aslSession)
|
||||
throws ServiceException {
|
||||
|
|
|
@ -15,6 +15,12 @@ import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceExcept
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Giancarlo Panichi email: <a
|
||||
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
||||
*
|
||||
*/
|
||||
public class AccountingCache implements Serializable {
|
||||
private static final long serialVersionUID = -4352823042594405108L;
|
||||
|
||||
|
@ -22,35 +28,47 @@ public class AccountingCache implements Serializable {
|
|||
.getLogger(AccountingCache.class);
|
||||
|
||||
private static final String ACCOUNTING_CACHE = "AccountingCache";
|
||||
private Cache<String, SeriesResponse> cache;
|
||||
private CacheManager cacheManager;
|
||||
private String cacheName;
|
||||
|
||||
public AccountingCache(String token) throws ServiceException {
|
||||
|
||||
public AccountingCache() throws ServiceException {
|
||||
super();
|
||||
try {
|
||||
cacheName=ACCOUNTING_CACHE+token;
|
||||
|
||||
|
||||
// Resolve a cache manager
|
||||
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||
cacheManager = cachingProvider.getCacheManager();
|
||||
|
||||
// configure the cache MutableConfiguration<String, Integer>
|
||||
MutableConfiguration<String, SeriesResponse> config = new MutableConfiguration<String,SeriesResponse>()
|
||||
.setTypes(String.class, SeriesResponse.class)
|
||||
.setExpiryPolicyFactory(
|
||||
AccessedExpiryPolicy
|
||||
.factoryOf(Duration.THIRTY_MINUTES))
|
||||
.setStatisticsEnabled(true);
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
throw new ServiceException(e.getLocalizedMessage(), e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Cache<String, SeriesResponse> initCache(String token)
|
||||
throws ServiceException {
|
||||
try {
|
||||
String cacheName = ACCOUNTING_CACHE + token;
|
||||
|
||||
|
||||
// create the cache
|
||||
cache=cacheManager.getCache(cacheName);
|
||||
|
||||
if(cache==null){
|
||||
Cache<String, SeriesResponse> cache = cacheManager
|
||||
.getCache(cacheName, String.class, SeriesResponse.class);
|
||||
|
||||
if (cache == null) {
|
||||
// configure the cache MutableConfiguration<String, Integer>
|
||||
MutableConfiguration<String, SeriesResponse> config = new MutableConfiguration<String, SeriesResponse>()
|
||||
.setTypes(String.class, SeriesResponse.class)
|
||||
.setExpiryPolicyFactory(
|
||||
AccessedExpiryPolicy
|
||||
.factoryOf(Duration.THIRTY_MINUTES))
|
||||
.setStatisticsEnabled(true);
|
||||
|
||||
|
||||
cache = cacheManager.createCache(cacheName, config);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ResourcePoolsBuilder builder = ResourcePoolsBuilder
|
||||
* .newResourcePoolsBuilder().heap(5L, MemoryUnit.MB);
|
||||
|
@ -66,6 +84,7 @@ public class AccountingCache implements Serializable {
|
|||
* Eh107Configuration
|
||||
* .fromEhcacheCacheConfiguration(cacheConfiguration));
|
||||
*/
|
||||
return cache;
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
|
@ -75,45 +94,43 @@ public class AccountingCache implements Serializable {
|
|||
|
||||
}
|
||||
|
||||
public void put(String key, SeriesResponse value) {
|
||||
public void put(String token, String key, SeriesResponse value)
|
||||
throws ServiceException {
|
||||
Cache<String, SeriesResponse> cache = initCache(token);
|
||||
cache.put(key, value);
|
||||
logger.debug("Cached: [" + key + ", " + value + "]");
|
||||
|
||||
}
|
||||
|
||||
public SeriesResponse get(String key) {
|
||||
public SeriesResponse get(String token, String key) throws ServiceException {
|
||||
Cache<String, SeriesResponse> cache = initCache(token);
|
||||
SeriesResponse value = cache.get(key);
|
||||
logger.debug("Cached value: " + value);
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
public void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
logger.debug("Release the resource " + cacheName);
|
||||
if (cache != null) {
|
||||
logger.debug("Release the cache resources");
|
||||
if (cacheManager != null) {
|
||||
try {
|
||||
cache.close();
|
||||
for (String cacheName : cacheManager.getCacheNames()) {
|
||||
cacheManager.destroyCache(cacheName);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
logger.error("Error closing the cache " + cacheName
|
||||
+ ": " + e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
try {
|
||||
cacheManager.destroyCache(ACCOUNTING_CACHE);
|
||||
} catch (Throwable e) {
|
||||
logger.error("Error destroying the cache " + cacheName
|
||||
+ ": " + e.getLocalizedMessage(), e);
|
||||
logger.error(
|
||||
"Error destroying the AccountingCache: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
try {
|
||||
cacheManager.close();
|
||||
} catch (Throwable e) {
|
||||
logger.error(
|
||||
"Error closing cache manager: "
|
||||
"Error closing AccountingCache manager: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,8 +30,7 @@ public class Constants {
|
|||
public static final String EXPORT_SERVLET_ACCOUNTING_TYPE_PARAMETER = "AccountingType";
|
||||
|
||||
public static final String SESSION_ACCOUNTING_STATE = "ACCOUNTING_STATE";
|
||||
public static final String ACCOUNTING_CACHE = "AccountingCache";
|
||||
|
||||
|
||||
|
||||
public static final AccountingType[] DEFAULT_TABS = new AccountingType[] { AccountingType.STORAGE };;
|
||||
|
||||
|
|
|
@ -28,12 +28,14 @@ public class TestAccountingManangerCache extends TestCase {
|
|||
logger.debug("Test Enabled");
|
||||
|
||||
try {
|
||||
AccountingCache accountingCache=new AccountingCache("token");
|
||||
AccountingCache accountingCache=new AccountingCache();
|
||||
SeriesResponse s=new SeriesResponse();
|
||||
accountingCache.put("key", s);
|
||||
accountingCache.put("token","key", s);
|
||||
accountingCache.finalize();
|
||||
assertTrue(true);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(),e);
|
||||
assertTrue("Error in cache!", false);
|
||||
|
||||
|
|
Loading…
Reference in New Issue