注解支持定时任务和异步处理

Spring支持使用注解开启定时任务和异步方法执行

为了支持@Scheduled 和 @Async 注解,需要在你的@Configuration类上加 @EnableScheduling  和@EnableAsync  注解

@EnableScheduling
@EnableAsync
@SpringBootApplication
public class SpringBootApiApplication {

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

使用@Scheduled注解

@Component
public class HelloJob {

    @Scheduled(cron = "0/10 * * * * * ")
    public void hello(){
        System.out.println("hello");
    }

}

除了时间表达式,还有其他的方式,比如:

@Scheduled(fixedDelay=5000) ---固定延迟每5秒执行一次,从上次执行完成开始测量时间
@Scheduled(fixedRate=5000) ---两次任务执行时间间隔为5秒

 

使用@Async

可以在方法上使用@Async注解,以便异步调用该方法,调用者将在调用时立刻返回,实际的执行已经提交给Spring taskExecutor的任务中。

@Async
void doSomething() {
    // this will be executed asynchronously
}

方法可使用参数

@Async
void doSomething(String s) {
    // this will be executed asynchronously
}

还可以异步调用返回值的方法,这些方法需要具有Future类型的返回值。

@Async
Future<String> returnSomething(int i) {
    // this will be executed asynchronously
}

@Async不能与生命周期回调一起使用,例如@PostConstruct,如果要异步初始化Spring Bean,必须使用单独的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方法具有Future类型返回值时,很容易管理在方法执行期间抛出的异常,因为在调用get结果时会抛出此异常。 但是,对于void返回类型,异常未被捕获且无法传输。 对于这些情况,可以提供AsyncUncaughtExceptionHandler来处理此类异常。
 

public class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {

    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        // handle exception
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值