原生quartz:
基本思路:
1.任务:创建一个实现Job接口的实现类(就是要具体做的事情,可以具体调用自己写的service)
定义一个Job,并绑定我们自己实现Job接口的实现类(例如通过JobBuilder的方式)
2.触发器:创建Trigger,并设置相关参数,如启动时间等。
3.调度器:通过工厂创建一个Scheduler
将job和trigger绑定到scheduler对象上,并启动
简易案例:
quartz层:
import java.io.File;
import java.io.IOException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloWorldQuartz implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException {
File file2 = new File("*****/test/HelloJob1.java");
if (file2.exists()) {
System.out.println("存在文件夹或者文件");
} else {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
调用层:
//注意以下几个导入很重要
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
public class TestQuartz {
public static void main(String[] args) throws Exception {
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
// computer a time that is on the next round minute
Date runTime = evenMinuteDate(new Date());
// define the job and tie it to our HelloJob class
JobDetail job = new JobDetail("ClaimVoucherMonthStatisticsQuartz", "group2", ClaimVoucherMonthStatisticsJob.class);
// JobDetail job = newJob(HelloWorld.class).withIdentity("job1", "group1").build();
// Trigger the job to run on the next round minute
// Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
SimpleTrigger simTrig = new SimpleTrigger("触发器名", SimpleTrigger.REPEAT_INDEFINITELY, 3000);
simTrig.setStartTime(new Date(System.currentTimeMillis()+1000));
//CronTrigger
// CronTrigger cronTrig;
// cronTrig = new CronTrigger("remindJob", "group2", "* * 11 * * ?");
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, simTrig);
// Start up the scheduler (nothing can actually run until the
// scheduler has been started)
sched.start();
// wait long enough so that the scheduler as an opportunity to
// run the job!
try {
Thread.sleep(10000);
// executing...
//关闭
sched.shutdown();
} catch (Exception e) {
//
}
}
}
spring和quartz整合:
applicationContext-xxx.xml配置:
<!-- ======================================== 配置quartz ======================================== -->
<bean id="biz_claim_voucherStatisticsServiceImpl" class="cn.jboasystem.service.impl.Biz_claim_voucherStatisticsServiceImpl"></bean>
<!-- 配置job -->
<bean id="claimVoucherMonthStatisticsJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<!-- <bean id="claimVoucherMonthStatisticsJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> -->
<!-- 指定jobClass是claimVoucherMonthStatisticsJob -->
<property name="jobClass" value="cn.jboasystem.quartz.ClaimVoucherMonthStatisticsJob"></property>
<!-- 指定任务数据 -->
<property name="jobDataAsMap">
<map>
<entry key="biz_claim_voucherStatisticsServiceImpl" value-ref="biz_claim_voucherStatisticsServiceImpl"></entry>
<entry key="userName" value="张三"></entry>
</map>
</property>
</bean>
<!-- 配置触发器 -->
<!-- SimpleTrigger -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- 配置jobDetail -->
<property name="jobDetail" ref="claimVoucherMonthStatisticsJob"></property>
<!-- 配置启动后延迟时间1s -->
<property name="startDelay" value="1000"></property>
<!-- 配置重复执行的时间间隔3s -->
<property name="repeatInterval" value="3000"></property>
</bean>
<!-- CronTrigger -->
<bean id="myTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 配置jobDetail -->
<property name="jobDetail" ref="claimVoucherMonthStatisticsJob"></property>
<!-- 配置表达式 -->
<property name="cronExpression" value="30 47 * ? * *"></property>
</bean>
<!-- 配置调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 指定触发器 -->
<ref bean="myTrigger2"/>
<!-- 可以在这里添加更多的触发器 -->
</list>
</property>
</bean>
代码:
//报表的任务调度
public class ClaimVoucherMonthStatisticsJob extends QuartzJobBean{
Logger logger = Logger.getLogger(this.getClass());
@Resource
private Biz_claim_voucherStatisticsService biz_claim_voucherStatisticsServiceImpl;
//注入Biz_claim_voucherStatisticsService业务逻辑层
public void setBiz_claim_voucherStatisticsServiceImpl(
Biz_claim_voucherStatisticsService biz_claim_voucherStatisticsServiceImpl) {
this.biz_claim_voucherStatisticsServiceImpl = biz_claim_voucherStatisticsServiceImpl;
}
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
//获得当前年和月
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH )+1;
biz_claim_voucherStatisticsServiceImpl.modifyMonthStatistics(year, month-1);
}