package com.nq.utils.task.news; import com.nq.enums.EStockType; import com.nq.pojo.SiteNews; import com.nq.service.ISiteArticleService; import com.nq.service.ISiteNewsService; import com.nq.utils.http.HttpRequest; import net.sf.json.JSONArray; import net.sf.json.JSONObject; 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.Date; 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; @Autowired ISiteArticleService iSiteArticleService; boolean NewsInfoTask = true; private final Lock NewsInfoTaskLock = new ReentrantLock(); /* * 新聞資訊抓取 * */ @Scheduled(cron = "0 0/5 * * * ?") public void NewsInfoTask() { if (NewsInfoTask) // 定义一个布尔变量,代表新闻任务是否在处理中 return; NewsInfoTask = true; // 设置处理中标识为 true if (NewsInfoTaskLock.tryLock()) { // 加锁 try { System.out.println("新闻定时任务---->开始"); iSiteNewsService.grabNews(); } catch (Exception e) { System.err.println("新闻抓取出现异常: " + e.getMessage()); // 打印具体的异常信息 } finally { NewsInfoTaskLock.unlock(); // 释放锁 NewsInfoTask = false; // 设置处理中标识为 false System.out.println("新闻定时任务---->结束"); } } } /* * 新聞公告抓取 * */ // @Scheduled(cron = "0 0/30 9-20 * * ?") // public void ArtInfoTask() { // this.iSiteArticleService.grabArticle(); // } }