docs/Spring全家桶/SpringBoot/SpringBoot中的任务调度与@Async.md
ݽվѸѧϰʼǡܽоղء֤ȷԣʹöķ뱾վأ
</header> <script>( adsbygoogle = window.adsbygoogle || []).push({});</script>ִضʱεĹ̡Spring BootΪSpringӦóϱдȳṩ˺ܺõ֧֡
Java CronʽCronTriggerʵorg.quartz.Triggerࡣ йJava cronʽĸϢĴ -
[@EnableScheduling](https://github.com/EnableScheduling "@EnableScheduling")עΪӦóõȳעӵSpring BootӦóļС
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
[@Scheduled](https://github.com/Scheduled "@Scheduled")עضʱڴȳ
@Scheduled(cron = "0 * 9 * * ?")
public void cronJobSch() throws Exception {
}
һʾ룬ʾÿ9:00ʼÿ9:59ִ
package com.yiibai.demo.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
@Scheduled(cron = "0 * 9 * * ?")
public void cronJobSch() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("Java cron job expression:: " + strDate);
}
}
ĻͼʾӦó09:03:23Ҵʱÿһִһcronҵȳ
̶ʵȳضʱִȴǰһɡ ֵԺΪλ ʾʾڴ˴ -
@Scheduled(fixedRate = 1000)
public void fixedRateSch() {
}
˴ʾӦóʱÿִʾ -
package com.yiibai.demo.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
@Scheduled(fixedRate = 1000)
public void fixedRateSch() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("Fixed Rate scheduler:: " + strDate);
}
}
עĻͼʾ09:12:00Ӧó֮ÿһ̶ʵȳִ
̶ӳٵȳضʱִ Ӧõȴһɡ ֵӦԺΪλ ˴ʾʾ -
@Scheduled(fixedDelay = 1000, initialDelay = 1000)
public void fixedDelaySch() {
}
initialDelayڳʼӳֵ֮һִʱ䡣
Ӧó3ÿִһʾʾ -
package com.yiibai.demo.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
@Scheduled(fixedDelay = 1000, initialDelay = 3000)
public void fixedDelaySch() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("Fixed Delay scheduler:: " + strDate);
}
}
ִʾ09:18:39ʼӦóÿ3̶ӳټƻ(ÿִһ)
//Ķhttps://www.yiibai.com/spring-boot/spring_boot_scheduling.html
@EnableAsync ע Ҫʹ @AsyncҪʹ @EnableAsync ע Spring Boot е첽ԡ
@Configuration @EnableAsync public class AppConfig { } ϸ˵ԲοAsyncConfigurer(opens new window)
#@Async ע #ֵ֧÷ 1ֵ
@Async עη첽ʽá仰˵ڵô˷ʱأʵִзύ Spring TaskExecutor С£ԽעӦڷ void ķʾʾ
@Async void doSomething() { // this will be executed asynchronously } 2ֵ
ʹ @Scheduled עע͵ķͬЩָΪʱɵԡʽãĵá磬´ @Async עĺϷӦã
@Async void doSomething(String s) { // this will be executed asynchronously } 3зֵ
첽÷ֵķǣЩҪ Future ͵ķֵȻṩ첽ִеĺôԱ߿ڵ Future ϵ get() ֮ǰִʾʾڷֵķʹ@Async
@Async Future<String> returnSomething(int i) { // this will be executed asynchronously } #ֵ֧÷ @Async ڻصһʹã @PostConstruct
Ҫ첽ʼ Spring beanʹõijʼ Spring beanȻĿϵ @Async ע͵ķʾʾ
public class SampleBeanImpl implements SampleBean {
@Async
void doSomething() {
// ...
}
}
public class SampleBeanInitializer {
private final SampleBean bean;
public SampleBeanInitializer(SampleBean bean) {
this.bean = bean;
}
@PostConstruct
public void initialize() {
bean.doSomething();
}
} #ȷִָ Ĭ£ڷָ @Async ʱʹõִ첽֧ʱõִʹ XML AsyncConfigurer ʵ֣УΪ annotation-driven ԪءǣҪָʾִиʱӦʹĬִֵʹ @Async ע value ԡʾʾִд˲
@Async("otherExecutor") void doSomething(String s) { // this will be executed asynchronously by "otherExecutor" } £otherExecutor Spring κ Executor bean ƣҲκ Executor ƣ磬ʹ <qualifier> Ԫػ Spring @Qualifier עָ
@Async ķֵΪ Future ʱڷִڼ׳쳣Ϊڵ get ʱ׳쳣ǣڷֵΪ void ͵ķ쳣ᱻ䡣ṩ AsyncUncaughtExceptionHandler 쳣ʾʾִд˲
public class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
// handle exception
}
} Ĭ£¼쳣ʹ AsyncConfigurer <taskannotation-driven /> XML ԪضԶ AsyncUncaughtExceptionHandler