com.mntviews.base.service.impl.JobSchedulerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mnt-base Show documentation
Show all versions of mnt-base Show documentation
Basis for mnt-bridge services
The newest version!
package com.mntviews.base.service.impl;
import com.mntviews.base.BridgeJob;
import com.mntviews.base.module.BridgeModule;
import com.mntviews.base.service.BridgeBaseService;
import com.mntviews.base.service.exception.JobSchedulerException;
import com.mntviews.bridge.service.BridgeContext;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Map;
import static org.quartz.JobBuilder.newJob;
@Service
@DependsOn("bridgeBaseServiceImpl")
public class JobSchedulerImpl {
private final static Logger log = LoggerFactory.getLogger(JobSchedulerImpl.class);
private final BridgeBaseService bridgeBaseService;
public JobSchedulerImpl(BridgeBaseService bridgeBaseService) {
this.bridgeBaseService = bridgeBaseService;
}
private static Scheduler scheduler;
private void scheduleJob(BridgeModule bridgeModule) throws SchedulerException {
if (bridgeModule.getCron() != null) {
for (Map.Entry entry : bridgeModule.getBridgeContextMap().entrySet()) {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("context", entry.getValue());
String group = entry.getKey().toUpperCase() + "_" + bridgeModule.getGroupTag();
JobDetail job = newJob(BridgeJob.class)
.withIdentity(bridgeModule.getMetaTag(), group)
.usingJobData(jobDataMap)
.build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(bridgeModule.getMetaTag(), group)
.withSchedule(CronScheduleBuilder.cronSchedule(bridgeModule.getCron()))
.forJob(bridgeModule.getMetaTag(), group)
.build();
if (!scheduler.checkExists(job.getKey()))
scheduler.scheduleJob(job, trigger);
}
}
}
@PostConstruct
public void init() {
try {
if (scheduler != null)
scheduler.clear();
else
scheduler = new StdSchedulerFactory().getScheduler();
for (BridgeModule bridgeModule : bridgeBaseService.getBridgeBase().getModules()) {
log.debug(bridgeModule.getTypeTag() + " cron={}", bridgeModule.getCron());
scheduleJob(bridgeModule);
}
scheduler.start();
} catch (SchedulerException e) {
throw new JobSchedulerException(e);
}
}
}