day14

本文介绍了在SpringBoot项目中配置定时任务,包括导入MySQL连接库,使用JodaTime处理日期,以及如何使用`@Scheduled`注解创建并运行定时任务。还提到在Linux环境下检查虚拟化设置以确保任务正常执行。
摘要由CSDN通过智能技术生成

Spring boot定时任务

导入jar包

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

定时任务初体验

在这里插入图片描述
(正常情况下不指定年)

package com.example.springboot_5_task.task;

import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskTest {

    @Scheduled(cron = "0/10 * * * * ? ")
    public void testPrint(){
        DateTime dateTime = new DateTime();
        String dateStr = dateTime.plusDays(1).toString("yyyy-MM-dd");
        System.out.println("dateStr = " + dateStr);
    }
}

在这里插入图片描述

按照指定格式输出
在这里插入图片描述

输出错误,修改后
在这里插入图片描述

主程序添加开启定时任务注解

在这里插入图片描述

package com.atguigu.task;

import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskTest {

    @Scheduled(cron = "30 30 10 26 7 ? ")
    public void test01(){
        DateTime dateTime = new DateTime();
        String dateStr = dateTime.plusDays(1).toString("yyyy-MM-dd hh-mm-ss");
        System.out.println("dateStr = " + dateStr);
    }
}

主程序:

package com.example.springboot_5_task;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
//开启定时任务
@EnableScheduling
public class Springboot5TaskApplication {

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

}

Linux

检查是否启用虚拟化

任务管理器 -> 性能 -> cpu
在这里插入图片描述

启动虚拟化查询教程

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值