[英]Using custom code to repopulate cache instead of Spring + Quartz integration
读完实现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,并且计划的作业没有重新填充缓存。 与使用Java ScheduledExecutorService相比,配置Quartz并与Spring集成似乎要花更多的工夫,但是使用上述我可能不知道的代码可能会出现潜在的问题?
据我了解,如果您调用缓存刷新的方法是正确的,则执行该操作的计划无关紧要。 您可以按原样保留它(尽管您还应该在服务器关闭过程中在scheduleExecutorService上添加一个shutdownNow调用,然后在scheduleExecutorService上添加awaitTermination以停止它-例如,通过在上下文侦听器的contextDestroyed方法中调用它),使用spring和crystal或使用纯spring的调度由于无论如何您都使用spring,因此支持@Scheduled注释(有关spring调度的更多详细信息,请参见http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html )。
所以不,如果您的问题集中在调度部分,则不需要使用石英。 如果您的问题侧重于魅力的运作方式,对不起,我对此一无所知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.