导入依赖
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
实现quartz的Job接口
java
package org.pocket.transaction.center.job;
import lombok.extern.slf4j.Slf4j;
import org.pocket.common.dto.accmgr.RedEnvelopeAns;
import org.pocket.common.dto.accmgr.RedEnvelopeReq;
import org.pocket.transaction.center.feign.TxMgrService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author: mirror
* @create: 2019-09-17 11:50
*/
@Slf4j
public class RedEnvelopeJob implements Job {
public static final String RED_ENVELOPE_JOB_KEY = "addRedEnvelopeInvalidTask";
@Autowired
private TxMgrService txMgrService;
@Override
public void execute(JobExecutionContext jobExecutionContext) {
log.info(" ----- 红包失效,任务开始:{}", jobExecutionContext.getJobDetail().getJobDataMap().get(RED_ENVELOPE_JOB_KEY));
RedEnvelopeAns redEnvelopeAns = txMgrService.rejectRedEnvelope((RedEnvelopeReq) jobExecutionContext.getJobDetail().getJobDataMap().get(RED_ENVELOPE_JOB_KEY));
log.info(" ----- 红包失效,任务结束,结果:{}", redEnvelopeAns);
}
}
Service
java
package org.pocket.transaction.center.service.impl;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.pocket.common.dto.accmgr.RedEnvelopeReq;
import org.pocket.transaction.center.job.RedEnvelopeJob;
import org.pocket.transaction.center.service.TcRedEnvelopeService;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Service;
import java.util.HashMap;
/**
* @author mirror
*/
@Service
@Slf4j
public class TcRedEnvelopeServiceImpl implements TcRedEnvelopeService {
private static final int ONE_DAY = 1;
private static final String RED_ENVELOPE_TASK_GROUP = "red_envelope_group";
@Override
public void addRedEnvelopeInvalidTask(RedEnvelopeReq redEnvelopeReq) {
try {
//1、获得一个scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
//2、获得一个jobDetail
HashMap<Object, Object> map = Maps.newHashMap();
map.put(RedEnvelopeJob.RED_ENVELOPE_JOB_KEY, redEnvelopeReq);
JobDetail jobDetail = JobBuilder
.newJob(RedEnvelopeJob.class)
.setJobData(new JobDataMap(map))
.withIdentity(redEnvelopeReq.getTxid())
.build();
//3、获得一个trigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(redEnvelopeReq.getTxid(), RED_ENVELOPE_TASK_GROUP)
.startAt(DateTime.now().plusDays(ONE_DAY).toDate())
.build();
//4、把任务和触发器放到scheduler中
scheduler.scheduleJob(jobDetail, trigger);
//5、开始任务调度
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
测试
java
@PostMapping("/testjob")
public void test(String id) {
try {
//1、获得一个scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
//2、获得一个jobDetail
HashMap<Object, Object> map = Maps.newHashMap();
map.put(RED_ENVELOPE_JOB_KEY, id);
JobDetail job = JobBuilder
.newJob(RedEnvelopeJob.class)
//传入数据
.setJobData(new JobDataMap(map))
//设置标识
.withIdentity(id)
.build();
//3、获得一个trigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(id, "group1")
//什么时候开始,或者是立即开始.startNow()
.startAt(DateTime.now().plusSeconds(5).toDate())
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
//多少间隔内重复
.withIntervalInSeconds(5)
//循环次数
.withRepeatCount(0))
.build();
//4、把任务和触发器放到scheduler中
scheduler.scheduleJob(job, trigger);
//5、开始任务调度
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}