3.3 Spring高级话题: 计划任务

3.3 Spring高级话题: 计划任务

在这里插入图片描述
3.3 Spring 计划任务

1.Spring3.1开始,计划任务开始异常的简单
2.配置
  2.1 首先通过配置类注解@EnableScheduling来开启计划任务的支持
  2.2 然后在需要计划任务的方法上注解@Scheduled,声明这个是一个计划

配置过程

1.通过@EnableScheduling注解开启计划任务支持
2.通过@Scheduled注解表明方法是计划方法

1.java配置

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@ComponentScan("com.sgx.inspect.bug")
@EnableScheduling
public class ScheduleConfigurate {
}

2.使用@Async注解

@Service
public class ScheduleService {
    @Scheduled(fixedRate = 5000)
    public void scheduleTask() {
        System.out.println("5M秒执行一次:" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
    }
}

3.测试使用

import com.sgx.inspect.bug.service.ScheduleConfigurate;
import com.sgx.inspect.bug.service.ScheduleService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main2 {

    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScheduleConfigurate.class);
        ScheduleService bean = context.getBean(ScheduleService.class);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值