2 package org.onap.champ.service;
4 import java.util.concurrent.TimeUnit;
6 import org.onap.aai.champcore.ChampGraph;
7 import org.onap.aai.champcore.ChampTransaction;
8 import org.onap.aai.champcore.exceptions.ChampTransactionException;
9 import org.onap.champ.service.logging.ChampMsgs;
10 import org.onap.aai.cl.api.Logger;
11 import org.onap.aai.cl.eelf.LoggerFactory;
13 import com.google.common.cache.Cache;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.RemovalCause;
16 import com.google.common.cache.RemovalListener;
17 import com.google.common.cache.RemovalNotification;
20 * Self expiring Cache to hold request transactionIds . Events are expired
21 * automatically after configured interval
23 public class ChampTransactionCache {
24 private static Logger logger = LoggerFactory.getInstance().getLogger(ChampTransactionCache
28 private ChampGraph graphImpl;
29 private Cache<String, ChampTransaction> cache;
33 public ChampTransactionCache(long txTimeOutInSec,ChampGraph graphImpl) {
34 CacheBuilder builder = CacheBuilder.newBuilder().expireAfterWrite(txTimeOutInSec, TimeUnit.SECONDS)
35 .removalListener(new RemovalListener() {
37 public void onRemoval(RemovalNotification notification) {
38 if(notification.getCause()==RemovalCause.EXPIRED){
39 logger.info(ChampMsgs.CHAMP_TX_CACHE, "Following transaction: "+notification.getKey()+" is being evicted from cache due to timeout of " + txTimeOutInSec+" seconds");
41 graphImpl.rollbackTransaction((ChampTransaction) notification.getValue());
42 logger.info(ChampMsgs.CHAMP_TX_CACHE, "Transaction rolledback successfully :" + notification.getKey());
43 } catch (ChampTransactionException e) {
44 // TODO Auto-generated catch block
50 cache = builder.build();
52 this.graphImpl = graphImpl;
56 public void put(String txId, ChampTransaction tx) {
61 public ChampTransaction get(String txId) {
64 if(cache.getIfPresent(txId)==null){
65 //cleanup cache so that removalListener is called
68 return cache.getIfPresent(txId);
71 public void invalidate(String txId) {
72 cache.invalidate(txId);