一、引入依赖
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-quartz</ artifactId>
</ dependency>
二、示例任务
import lombok. extern. slf4j. Slf4j ;
import org. quartz. JobExecutionContext ;
import org. quartz. JobExecutionException ;
import org. springframework. scheduling. quartz. QuartzJobBean ; import java. util. Date ; @Slf4j
public class DemoJob extends QuartzJobBean { @Override protected void executeInternal ( JobExecutionContext context) throws JobExecutionException { context. getJobDetail ( ) . getJobDataMap ( ) . forEach ( ( k, v) -> log. info ( "param, key:{}, value:{}" , k, v) ) ; log. info ( "执行时间: " + new Date ( ) ) ; } }
三、配置
import org. quartz. * ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ; @Configuration
public class QuartzConfig { @Bean ( "demoJob" ) public JobDetail helloJobDetail ( ) { return JobBuilder . newJob ( DemoJob . class ) . withIdentity ( "demoJob" ) . usingJobData ( "msg" , "Hello Quartz" ) . storeDurably ( ) . build ( ) ; } @Bean public Trigger printTimeJobTrigger ( ) { CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder . cronSchedule ( "0/1 * * * * ?" ) ; return TriggerBuilder . newTrigger ( ) . forJob ( helloJobDetail ( ) ) . withIdentity ( "quartzTaskService" ) . withSchedule ( cronScheduleBuilder) . build ( ) ; }
}