1、定制调度任务:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rd.ifaes.core.project.domain.Project;
/**
* 项目缓存延时调度任务
* @author lh
* @version 3.0
* @since 2016-8-26
*
*/
public class ProjectCacheDelaySchedule implements Runnable {
private final static Logger LOGGER = LoggerFactory.getLogger(ProjectCacheDelaySchedule.class);
//项目信息
private Project project;
//延时时间(单位:秒)
private long delay;
public ProjectCacheDelaySchedule(Project project) {
super();
this.project = project;
delay = project.getShowTime().getTime() - System.currentTimeMillis();
delay = (delay > 0) ? delay / 1000 : 0;
}
/**
* 取得延时时间
* @return
*/
public long getDelay() {
return delay;
}
/**
* project添加到缓存
*/
@Override
public void run() {
LOGGER.info("project to cache, projectNo="+project.getProjectNo());
ProjectCache.setProject(project);
}
}
2、加入调度任务:
private static ScheduledExecutorService projectSaleService = Executors.newScheduledThreadPool(1);
/**
* 项目上架 ( 仅供后台产品或借贷上架用 )
* @param project
*/
public static void projectSale(Project project){
ProjectCacheDelaySchedule executor = new ProjectCacheDelaySchedule(project);
projectSaleService.schedule(executor, executor.getDelay(), TimeUnit.SECONDS);
}
The end!