阅读有关实现ehcache计划刷新的配置时,似乎需要实现Quartz调度程序。我可能过于简化了,但下面的代码并没有达到相同的结果:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
public class MyCacheScheduler {
public void repopulateCache(){
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(5);
@SuppressWarnings({ "rawtypes", "unchecked" })
ScheduledFuture scheduledFuture =
scheduledExecutorService.schedule(new Callable() {
public Object call() throws Exception {
myCache.clearCache();
myCache.populateCache();
return "";
}
},
30,
TimeUnit.SECONDS);
try {
System.out.println("result = " + scheduledFuture.get());
} catch (Exception e) {
e.printStackTrace();
}
}
@Autowired
private MyCacheManagerImpl myCache;
}我只是想每隔X分钟刷新一次缓存。因此,当缓存超过TTL时,多个用户并不都同时填充缓存。如果用户在超过TTL的确切时间访问缓存,并且计划作业没有重新填充缓存,则仍有可能发生这种情况。配置Quartz并与Spring集成似乎要比使用Java做更多的工作,但是使用上面的代码可能会出现我不知道的潜在问题?
发布于 2014-11-11 10:23:02
据我所知,如果调用缓存刷新的方式是正确的,那么如何执行该操作的调度并不重要。您可以保持原样(虽然您还应该在服务器关机期间添加一个调用shutdownNow,然后在scheduledExecutorService上添加一个awaitTermination,以便通过在上下文侦听器的contextDestroyed方法中调用它来阻止它),可以在石英中使用spring,或者在使用spring时使用普通spring的调度支持,因为您无论如何都使用spring(有关春季调度的更多细节,请参见http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html )。
所以,如果您的问题集中在日程安排部分,则不需要使用石英。如果你的问题集中在机会的运作方式上,对不起,我对此一无所知。
https://stackoverflow.com/questions/26853803
复制相似问题