Я решил это сам с исходным кодом ниже.
@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);
}
}
}