Hello guys,

I have a component in which the values for the component is cached using
guava cache.
I want to auto update the cache every 10 seconds  and I'm using
ScheduledExecutorService to auto schedule the cache refresh but
unfortunately it is not working for me and throwing exception.

Any Help is appreciated . Or is there an otherway to auto schedule guava
caches, because i saw PeriodicScheduler but it does not work for me.

Here is the code :
ComponentA.java
//create cache and add it to cache manager

Thanks in advance

void createCacheIfNotExists() {
 if(!(cacheManager.containsKey(key)))
     {

      LoadingCache<String, Object> count = CacheBuilder
          .newBuilder().expireAfterWrite(5, TimeUnit.SECONDS)
          .refreshAfterWrite(10, TimeUnit.SECONDS)
          .build(new CacheLoader<String, Object>() {

            @Override
            public Object load(String key) {

              return someService.getCountfromDb();
            }

            @Override
            public ListenableFuture<Object> reload(String key,
                Object oldValue) throws Exception {

              ListenableFutureTask<Object> task =
ListenableFutureTask.create(new Callable<Object>() {

                @Override
                public Object call() throws Exception {

                  return someService.getCountfromDb();
                }
              });
              executor.execute(task);//this works
              return task;
            }
          });

     cacheManager.addCache(key, countCache);

     //this does not work, actually it works because in console i can see
this getting executed every 10 seconds but exception is thrown
     executor.scheduleWithFixedDelay(new Runnable() {

      @Override
      public void run() {
        countCache.refresh(key);

      }
    }, 0, 10, TimeUnit.SECONDS);
}

Exception Log :

WARNING: Exception thrown during refresh
java.util.concurrent.ExecutionException: java.lang.NullPointerException:
Unable to delegate method invocation to property 'request' of <Proxy for
RequestGlobals(org.apache.tapestry5.services.RequestGlobals)>, because the
property is null.
 at
com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:299)
 at
com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:286)
 at
com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
 at
com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly(Uninterruptibles.java:135)
 at
com.google.common.cache.LocalCache$Segment.getAndRecordStats(LocalCache.java:2344)
 at com.google.common.cache.LocalCache$Segment$1.run(LocalCache.java:2327)
 at
com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297)
 at
com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)
 at
com.google.common.util.concurrent.ExecutionList.add(ExecutionList.java:101)
 at
com.google.common.util.concurrent.AbstractFuture.addListener(AbstractFuture.java:170)
 at
com.google.common.cache.LocalCache$Segment.loadAsync(LocalCache.java:2322)
 at com.google.common.cache.LocalCache$Segment.refresh(LocalCache.java:2385)
 at com.google.common.cache.LocalCache.refresh(LocalCache.java:4085)
 at
com.google.common.cache.LocalCache$LocalLoadingCache.refresh(LocalCache.java:4825)

Reply via email to