如何在Spring Boot中实现单线程调度程序?

问题描述 投票:0回答:1

我想每隔00秒执行一次处理。(服务器时间)但是,这些过程可能需要一分钟或更长时间。

[当进程仍在运行时,有没有优雅的方法来阻止同一进程(方法)运行? (我不想运行该过程)

((如果#2进程结束,即使进程#1正在运行,进程#2也必须独立运行。)

@Scheduled(cron = "0 * * * * ?")
    public void process1() {
        //doing something during 
        //It may take a minute or more
    }
@Scheduled(cron = "0 * * * * ?")
    public void process2() {
        //doing something during 
        //It may take a minute or more
    }

java spring-boot thread-safety scheduler
1个回答
0
投票

我用下面的源代码自己解决了。

@Service
public class Scheduler {
    @Autowired
    private Job1Service job1;   
    @Autowired
    private Job2Service job2;

    @Scheduled(cron = "0 * * * * ?")
    private void job1() {
        new Thread(job1).start();
    }

    @Scheduled(cron = "0 * * * * ?")
    private void job2() {
        new Thread(job2).start();
    }}
}
public class Job1ServiceImpl implements Job1Service, Runnable{
    private final ReentrantLock lock = new ReentrantLock(true);

    public void run() {
        try {
            if(lock.tryLock(1, TimeUnit.MILLISECONDS)) {
                try {
                    logger.info("job1 start");

                    /* do heavy job
                    for(int i = 0 ; i < 100 ; i++) {
                        logger.info("test#1 : " + i);
                        for(int j = 0 ; j < 999999999 ; j++) {
                            int k = 1;
                            k++;
                            k++;
                        }
                    }
                    */
                }catch(Exception e) {
                    logger.error("job#1 error", e);
                }finally {
                    logger.info("job #1 end");
                    lock.unlock();
                }
            }else {
                logger.info("skip job#1. job #1 is running still");
            }
        }catch(InterruptedException e) {
            logger.info("Job #1 Aborted", e);
        }
    }
}
public class Job2ServiceImpl implements Job2Service, Runnable{
    private final ReentrantLock lock = new ReentrantLock(true);

    public void run() {
        try {
            if(lock.tryLock(1, TimeUnit.MILLISECONDS)) {
                try {
                    logger.info("job2 start");

                    /* do heavy job
                    for(int i = 0 ; i < 100 ; i++) {
                        logger.info("test#2 : " + i);
                        for(int j = 0 ; j < 999999999 ; j++) {
                            int k = 1;
                            k++;
                            k++;
                        }
                    }
                    */
                }catch(Exception e) {
                    logger.error("job#2 error", e);
                }finally {
                    logger.info("job #2 end");
                    lock.unlock();
                }
            }else {
                logger.info("skip job#2. job #2 is running still");
            }
        }catch(InterruptedException e) {
            logger.info("Job #2 Aborted", e);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.