import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.tdtech.eplatform.gatekeeper.quartz.QuartzCenter; import com.tdtech.eplatform.gatekeeper.util.ContextUtil; public class Cleaner { public static Cleaner insance = new Cleaner(); private BlockingQueue<String> list = new ArrayBlockingQueue<String>(2000); java.util.concurrent.Executor executor = Executors.newFixedThreadPool(1); private Logger logger = LoggerFactory.getLogger(Cleaner.class); private QuartzCenter quart; public static Cleaner getInstance() { return insance; } private Cleaner() { Runnable task = new Runnable() { @Override public void run() { try { while (true) { logger.info("before clean"); String jobName = list.take(); logger.info("cleanJobName:" + jobName); if (quart == null) { quart = ContextUtil.getBean("quartzCenter", QuartzCenter.class);
/*
private static ClassPathXmlApplicationContext applicationContext;
public static <T> T getBean(String name, Class<T> requiredType) {
try {
return applicationContext.getBean(name, requiredType);
} catch (Exception e) {
logger.error("I wanna get " + name, e);
return null;
}
}
*/ } if (quart != null) {
// 终止线程 quart.terminateJob(jobName); logger.info("after cleanJobName.Done:" + jobName); } } } catch (InterruptedException e) { logger.error("InterruptedException", e); } } }; executor.execute(task);
// sumit区别 需要返回执行结果。 // Future future = execute.submit(task); } /**
加入队列以便cleaner处理
*/ public void put(String name) { list.offer(name); } }