1
zj
2024-05-14 5259432d9a97529cf1ec2be5489a5b592bee78c4
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
package com.nq.utils.task;
 
 
import com.nq.service.IEchoServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * 余额宝定时任务
 */
@Slf4j
@Component
public class YEBTask {
 
 
    @Autowired
    IEchoServices echoServices;
 
    boolean sendYEBMoney = true;
 
    private final Lock sendYEBMoneyLock = new ReentrantLock();
 
    /**
     * 余额宝发放利息
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void sendYEBMoney() {
        if (sendYEBMoney) // 定义一个布尔变量,代表新闻任务是否在处理中
            return;
        sendYEBMoney = true; // 设置处理中标识为 true
        if (sendYEBMoneyLock.tryLock()) {
            try {
 
                log.info("基金定时任务-------开始");
                echoServices.sendMoney();
                log.info("基金定时任务-------结束");
 
            } finally {
                sendYEBMoneyLock.unlock();
                sendYEBMoney = false;
            }
        }
    }
 
}