SpringBoot的定时任务实现--SpringTask

SpringTask是Spring自带的功能。实现起来比较简单。

使用SpringTask实现定时任务有两种方式:

1.注解方式

   基于注解 @Scheduled 

@Scheduled(cron = "*/1 * * * * ?")
    public  void up(){
       System.out.println("定时任务开启:"+System.currentTimeMillis());
    }

cron表达式定义定时任务如何去执行。

2.配置文件xml方式

      基于xml的方式 【@Configuration + @ImportResource + xml】 需要重启应用才能生效

      2.1配置xml文件,定义xml文件的名称为task.xml,放置文件在resources文件夹下:

 xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <!--声明一个具有一个线程的池,如果定义多个,每个对象将获取同样的运行机会-->
    <task:scheduler id="sch" pool-size="10"/>

    <!--任务的调度类-->
    <bean id="scheduleTask" class="com.cloudtop.base.task.ScheduleTask"/>

    <!--引用线程池-->
    <task:scheduled-tasks scheduler="sch">
        <!--年报调度任务 5秒-->
        <task:scheduled ref="scheduleTask" method="yearReportTask" cron="0/5 * * * * ?"/>
    </task:scheduled-tasks>

</beans>

2.2配置类加载xml文件

package com.cloudtop.base.task;


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * 加载调度的配置文件
 */
@Configuration
@ImportResource(locations={"classpath:task/task.xml"})//加载调度xml
public class SpringTaskConfig {
}

2.3任务的调度类实现

package com.cloudtop.base.task;


import com.cloudtop.base.error.exception.BusinessException;
import com.cloudtop.core.service.EnvironmentUpService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 定时任务类
 */
public class ScheduleTask {
    @Autowired
    EnvironmentUpService environmentUpService;
    public void yearReportTask() throws BusinessException {
        System.out.println("*******定时任务执行的业务代码******");
    }

}

最后,第一种使用注解@EnableScheduling开启定时任务,第二种使用xml的方式配置好上面的三个文件就开启了定时任务,不用使用注解@EnableScheduling来开启定时任务。

@SpringBootApplication
@ServletComponentScan
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
@EnableScheduling
public class CloudtopWebFrameApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(CloudtopWebFrameApplication.class);
    }

    /**
     * 主程序入口
     * 所有SpringBoot项目均采用main方法启动主程序,该部分为必须项
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(CloudtopWebFrameApplication.class, args);
    }



}

最后在控制台会输出结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值