首页 > 编程知识 正文

springboot异步注解(springboot异步返回值)

时间:2023-05-05 02:17:54 阅读:77206 作者:3845

定时任务是在后台以我们设定的时间和频率打开线程执行我们定义的相关代码。

例如,计时器分析器、计时器统计器、计时器发送邮件等

下面,我将向大家介绍我们是如何基于Spring Boot开发的最简单快捷的方法

首先,我要简要了解一些评论。

@Scheduled :定时任务配置

@Async :异步执行

@EnableScheduling :打开定时任务支持

@EnableAsync :打开异步事件支持

、同步定时任务。 前面的完整代码下面是spring boot的启动程序。 @EnableScheduling启用@Scheduled注释。

@EnableScheduling //打开定时任务支持@ springbootapplication (exclude={ datasourceautoconfiguration.class } ) )。 publicclasswebapplication { publicstaticvoidmain (string [ ] args ) springapplication.run ) webapplication.class,args } importorg.spring帧. scheduling.annotation.scheduled; importorg.spring framework.stereotype.com ponent;/* * @ class name :方案1 * @描述:任务*@authortan*@date2019/7/5*/@ componentpublicclassschedule1corn从左向右(以空格分隔)秒分钟时间月中的日期星期一的日期年@scheduled ) cron='0/5**** ' ) public void calculed System.out.println (这里是定时任务1执行的内容。 请参阅。 ); System.out.println (定时任务1执行成功! ' ); }@scheduled(cron='0/5**** ' ) public void calculate2) { System.out.println开始执行定时任务2!' ); System.out.println (这里是定时任务2执行的内容。 请参阅。 ); System.out.println (定时任务2执行成功! ' ); }至此,简单的定时任务完成了。 但是,由于每个Scheduled方法都是以这种方式同步执行的,因此万一发生死锁,就无法执行其他任务。 介绍异步计时器任务

另一方面,异步定时任务Spring为任务调度和异步方法的执行提供注释支持。 也就是说,可以通过在方法中设置@Async注释来异步调用方法。

异步调用的实现是交给Spring的任务执行器。

可以自定义此任务执行程序

1、配置文件application.properties

添加内容:

corepoolsize=10 maxpools ize=50 queue capacity=102,线程池配置importorg.spring framework.beans.factory.annotation.vation importorg.spring framework.context.annotation.configuration; importorg.spring framework.context.annotation.property source; importorg.spring帧. scheduling.annotation.enable async; importorg.spring framework.scheduling.concurrent.threadpooltaskexecutor; import Java.util.concurrent.executor; /** *基于注释的配置文件* * @ class name : async config * @ description : todo * @ author tan * @ date 2019/7/5 */@ confion 支持异步事件@ property source (value=' class path : application.properties ' ) publicclassasyncconfig(/** )线程池维护线程池管理线程的最大数量*/@value('${corepoolsize} ) private int maxPoolSize; /** *缓存队列*/@value('${corepoolsize} ' )专用队列容量; @ beanpublicexecutortaskexecutor () threadpooltaskexecutorexecutor=newthreadpooltaskexecutor ); 执行程序. setcorepoolsize (corepoolsize; executor.setmaxpoolsize (maxpools ize; executor.setqueuecapacity; executor.initialize (; 返回执行程序; }3、然后像以前一样启动班级【很多@EnableAsync】

@启用同步//启用同步@EnableScheduling //打开定时任务支持@ springbootapplication (exclude={ datasourceautoconfiguration.clation publicclasswebapplication { publicsswebation ) args ) springapplication.run ) webapplication.class,args }任务类【为每个方法添加注释@Async,添加到类中时,所有方法都将异步执行

importorg.spring帧. scheduling.annotation.async; importorg.spring帧. scheduling.annotation.scheduled; importorg.spring framework.stereotype.com ponent;/* * @ class name :方案1 * @描述:任务*@authortan*@date2019/7/5*/@ componentpublicclassschedule1corn从左向右(以空格分隔)秒分钟时间月中的日期星期一的日期年@scheduled ) cron='0/5**** ' ) public void calculed System.out.println (这里是定时任务1执行的内容。 请参阅。 ); System.out.println (定时任务1执行成功! ' ); } @ async @ scheduled (cron='0/5* * * * * ' ) (public void calculate2) ({ System.out.println ) )开始执行定时任务2!' ); System.out.println (这里是定时任务2执行的内容。 请参阅。 ); System.out.println (定时任务2执行成功! ' ); (二、一些说明上的两个例子基本满足了对定时任务的开发需求

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。