1
zj
2025-07-10 37670b2ff5379e8603d3b0eec6d493daf2d6cfcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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.atomic.AtomicBoolean;
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;
 
 
    private final AtomicBoolean NewsInfoTask = new AtomicBoolean(false);
 
 
    private final Lock NewsInfoTaskLock = new ReentrantLock();
    /*
     * 新聞資訊抓取
     * */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void NewsInfoTask() {
        if (NewsInfoTask.get()) { // 判断任务是否在处理中
            return;
        }
        if (NewsInfoTaskLock.tryLock()) { // 加锁
            try {
                NewsInfoTask.set(true); // 设置处理中标识为true
                System.out.println("新闻定时任务---->开始");
                iSiteNewsService.grabNews();
            } catch (Exception e) {
                System.err.println("新闻抓取出现异常: " + e.getMessage()); // 打印具体的异常信息
            } finally {
                NewsInfoTaskLock.unlock(); // 释放锁
                NewsInfoTask.set(false); // 设置处理中标识为false
                System.out.println("新闻定时任务---->结束");
            }
        }
    }
 
 
 
    /*
     * 新聞公告抓取
     * */
//    @Scheduled(cron = "0 0/30 9-20 * * ?")
//    public void ArtInfoTask() {
//        this.iSiteArticleService.grabArticle();
//    }
}