1
zj
2024-05-15 86e784154596976f582fbcc6f447465309b1a51d
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
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.locks.ReentrantLock;
import java.util.concurrent.atomic.AtomicBoolean;
 
/**
 * 余额宝定时任务
 */
@Slf4j
@Component
public class YEBTask {
 
    @Autowired
    private IEchoServices echoServices;
 
    private final ReentrantLock sendYEBMoneyLock = new ReentrantLock();
    private final AtomicBoolean isSendYEBMoney = new AtomicBoolean(false);
 
    /**
     * 余额宝发放利息
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void sendYEBMoney() {
        if (isSendYEBMoney.get()) { // 判断任务是否在处理中
            return;
        }
        if (sendYEBMoneyLock.tryLock()) { // 尝试获取锁
            isSendYEBMoney.set(true); // 设置处理中标识为true
            try {
                log.info("基金定时任务-------开始");
                echoServices.sendMoney();
                log.info("基金定时任务-------结束");
            } finally {
                sendYEBMoneyLock.unlock(); // 释放锁
                isSendYEBMoney.set(false); // 设置处理中标识为false
            }
        }
    }
}