package org.example.timedTask; import org.example.enums.EStockType; import org.example.server.ISiteNewsService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; //import org.springframework.scheduling.annotation.Scheduled; @Component public class NewsTask { private static final Logger log = LoggerFactory.getLogger(NewsTask.class); @Autowired ISiteNewsService iSiteNewsService; private final Lock lock = new ReentrantLock(); /* * 新聞資訊抓取 * */ @Scheduled(cron = "0 * 0/5 * * ?") public void NewsInfoTask() { if (lock.tryLock()) { log.info("新聞資訊抓取--------->开始"); try { iSiteNewsService.grabNews(); } finally { lock.unlock(); log.info("新聞資訊抓取--------->结束"); } } else { log.info("新聞資訊抓取--------->上次任务还未执行完成,本次任务忽略"); } } }