千峰商城-springboot项目搭建-85-订单超时取消-定时任务框架quartz的整合使用

1.创建新的springboot项目

2.导入依赖

删除多余文件

 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

3.创建定时任务

定时任务:每隔指定的时间就执行一次任务。
案例:每隔三秒打印一次helloworld。

 创建一个service包:

创建PrintHelloWorldJob类:

可以生成Scheduled中的定时语法的定时器网址:https://cron.qqe2.com/
PrintHelloWorldJob :
@Component
public class PrintHelloWorldJob {

    //https://cron.qqe2.com
    //生成器,可以生成Scheduled中的定时语法
    @Scheduled(cron = "0/3 * * * * ?")
    public void printHelloWorld(){
        System.out.println("----------HelloWorld.");
    }
}
Scheduled中的定时语法:
字段******
含义小时星期
取值0-590-590-231-31

1-12或月份对应的

前三个英文字母(大小写均可)

0-7(0、7表示周日)或星期对应的

前三个英文字母(大小写均可)

允许的特殊字符, - * /, - * /, - * /, - * / ? L, - * /, - * / ? L

 特殊字符含义:

特殊字符-*/L
含义枚举区间任意值步长日/星期冲突匹配符最后
举例

"1,3,5 * * * * *"

任意时间的1、3、5秒钟执行

"0 0-5 14 * * ?"

每天14:00-14:05触发

"0 0 12 * * ?"

每天12:00触发

"0/5 * * * * *"

每5秒触发一次

"0 * * 26 * ?"

每月的26日的每分钟执行

"0 0 * L * ?"

每月最后一日的每一小时执行

application.properties:(由于商城项目已占用8080端口,所以修改新项目的端口为9999)
server.port=9999

在启动类上添加注解@EnableScheduling,启动定时任务:

@SpringBootApplication
@EnableScheduling
public class QuartzDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(QuartzDemoApplication.class, args);
    }

}

启动api,进行测试:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合Quartz实现cron定时任务的步骤如下: 1.在pom.xml中添加依赖 ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 2.创建Job类 ``` public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写具体的定时任务 } } ``` 3.创建JobDetail和Trigger ``` @Configuration public class QuartzConfig { @Bean public JobDetail myJobDetail() { return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably().build(); } @Bean public Trigger myTrigger() { return TriggerBuilder.newTrigger().forJob(myJobDetail()) .withIdentity("myTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build(); } } ``` 4.启动定时任务 ``` @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Autowired private Scheduler scheduler; @Autowired private Trigger myTrigger; @Autowired private JobDetail myJobDetail; @PostConstruct public void start() throws SchedulerException { scheduler.scheduleJob(myJobDetail, myTrigger); } } ``` 这里的注解`@PostConstruct`表示在Bean初始化之后执行该方法。 5.编写具体的定时任务逻辑 ``` public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("定时任务执行了!"); } } ``` 完整的代码示例: ``` // MyJob.java public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("定时任务执行了!"); } } // QuartzConfig.java @Configuration public class QuartzConfig { @Bean public JobDetail myJobDetail() { return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably().build(); } @Bean public Trigger myTrigger() { return TriggerBuilder.newTrigger().forJob(myJobDetail()) .withIdentity("myTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build(); } } // Application.java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Autowired private Scheduler scheduler; @Autowired private Trigger myTrigger; @Autowired private JobDetail myJobDetail; @PostConstruct public void start() throws SchedulerException { scheduler.scheduleJob(myJobDetail, myTrigger); } } ``` 这里的定时任务是每天凌晨0点执行一次,并打印一句话。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值