import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* 测试spring定时任务执行
*/
@Component
@Configuration
@EnableScheduling
public class MyTestTask {
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Scheduled(cron="* * * * * *") // 关于定时任务执行时间,后续会在别的文章中添加
public void executeUpdateYqTask() {
System.out.println(Thread.currentThread().getName() + " >>> task one " + format.format(new Date()));
} @Scheduled(cron="* * * * * *")
public void executeRepaymentTask() throws InterruptedException {
System.out.println(Thread.currentThread().getName() + " >>> task two " + format.format(new Date()));
}
}
 import java.util.concurrent.Executor;

 import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer, AsyncConfigurer{ /** 异步处理 */
public void configureTasks(ScheduledTaskRegistrar taskRegistrar){
TaskScheduler taskScheduler = taskScheduler();
taskRegistrar.setTaskScheduler(taskScheduler);
} /** 定时任务多线程处理 */
@Bean(destroyMethod = "shutdown")
public ThreadPoolTaskScheduler taskScheduler(){
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(20);
scheduler.setThreadNamePrefix("task-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
} /** 异步处理 */
public Executor getAsyncExecutor(){
Executor executor = taskScheduler();
return executor;
} /** 异步处理 异常 */
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
return new SimpleAsyncUncaughtExceptionHandler();
}
}

最后:

在启动类加上    @EnableScheduling 就可以了

 /**
* 启动类
*/
@SpringBootApplication
@EnableScheduling
public class AppApplication extends SpringBootServletInitializer {}

springboot中定时任务的更多相关文章

  1. Springboot中定时任务的使用

    在springboot中已经集成了定时任务,只需要在启动类上加注解@EnableScheduling即可 例如: 添加类加上@Component注解,添加方法加上@Scheduler即可

  2. SpringBoot中定时任务默认是串行执行 如何设置并行

    SpringBoot项目中,定时任务默认是串行执行的,不论启动多少任务,都是一个执行完成,再执行下一个. 如何设置并行呢? @EnableAsync  和@Async 这两个注解来实现 ,具体如下: ...

  3. springboot:定时任务

    在日常的开发过程中经常使用到定时任务,在springMVC的开发中,经常和quartz框架进行集成使用,但在springboot中没有这么做,而是使用了java的线程池来实现定时任务. 一.概述 在s ...

  4. 在SpringBoot中配置定时任务

    前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...

  5. springBoot中使用定时任务

    简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...

  6. springBoot中的定时任务

    springBoot中的定时任务 1:在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 2:新建ScheduledTasks任务类 : package c ...

  7. 定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html

    Springboot中使用Scheduled做定时任务---http://www.cnblogs.com/lirenqing/p/6596557.html 已经验证的方案: pom文件加入依赖 < ...

  8. SpringBoot中执行定时任务

    一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...

  9. SpringBoot中使用@Scheduled创建定时任务

    SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...

随机推荐

  1. MariaDB数据库性能优化

    1. 硬件优化 1.1 内存(Memory) 内存是最重要的因素,因为它允许您调整服务器系统变量.更多的内存意味着可以将更大的密钥和表缓存存储在内存中,从而减少磁盘访问速度,降低一个数量级. 如果未将 ...

  2. 四、MYSQL的数据类型

    类型选择原则 1.储存空间越少越好: 2.简单就好:例如整型比字符串更简单: 3.尽量避免null: 一.整数类型 1.有tinyint(8位).SMALLINT(16位).MEDIUMINT(24位 ...

  3. QTableWidget 详细使用

    QTableWidget 详细使用

  4. kali黑客渗透测试基础环境准备

    1.apt-get install python-nmap 2.apt-get install python-setuptools 正在读取软件包列表... 完成 正在分析软件包的依赖关系树      ...

  5. java 自定义异常输出信息(使用构造器)

    throw new Exception("上传的脚本类型不匹配,当前只支持类unix系列的远程扫描,请上传后缀名为 .sh .pl 的脚本文件"); 这样就可以了,结合配置的异常信 ...

  6. 通过实例来理解paxos算法

    背景   Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的”La”,此人现在在微软研究院)于1990年提出的一种基于消息传递的一致性算法.由于算法难以理解起初并没有 ...

  7. Flask学习-Flask app接受第一个HTTP请求

    一.__call__() 在Flask app启动后,一旦uwsgi收到来自web server的请求,就会调用后端app,其实此时就是调用app的__call__(environ,start_res ...

  8. Android Studio开发实用网站收集

    重点 1.Android Studio 调试技巧-断点调试 http://blog.csdn.net/qq_32452623/article/details/53769708 2.android st ...

  9. NodeMCU学习(三) : 进入网络世界

    把NodeMCU连接到路由器网络上 NodeMCU可以被配置为Station模式和softAP模式或者Station + AP模式,当它被配置为Station模式时,就可以去连接Access Poin ...

  10. win10引导错误的修复(内容系转载)

    #!尊重原作者,再此声明此内容属于网络转载,只是为了能保留下来方便日后查阅!!! win10误删引导文件,0xc0000098的解决方案,bcd引导文件受损情况分析 一.※相对简单的解决方法,对应的情 ...