欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Moke-Java 高级工程师 "ZUI 新 2024 结束"。

最编程 2024-07-16 14:27:11
...

Java高级工程师

核心代码,注释必读

// download:3w ukoou com

Spring异步任务

开启异步任务使用方法:

1).方法上加@Async注解

2).启动类或者配置类上@EnableAsync

回到顶部 2.源码解析 虽然spring5已经出来了,但是我们还是使用的spring4,本文就根据spring-context-4.3.14.RELEASE.jar来分析源码。

2.1.@Async org.springframework.scheduling.annotation.Async 源码注释翻译:

1 /**
 2  * Annotation that marks a method as a candidate for <i>asynchronous</i> execution.
 3  * Can also be used at the type level, in which case all of the type's methods are
 4  * considered as asynchronous.该注解可以标记一个异步执行的方法,也可以用来标注类,表示类中的所有方法都是异步执行的。
 5  *
 6  * <p>In terms of target method signatures, any parameter types are supported.
 7  * However, the return type is constrained to either {@code void} or
 8  * {@link java.util.concurrent.Future}. In the latter case, you may declare the
 9  * more specific {@link org.springframework.util.concurrent.ListenableFuture} or
10  * {@link java.util.concurrent.CompletableFuture} types which allow for richer
11  * interaction with the asynchronous task and for immediate composition with
12  * further processing steps.入参随意,但返回值只能是void或者Future.(ListenableFuture接口/CompletableFuture类)13  *
14  * <p>A {@code Future} handle returned from the proxy will be an actual asynchronous
15  * {@code Future} that can be used to track the result of the asynchronous method
16  * execution. However, since the target method needs to implement the same signature,
17  * it will have to return a temporary {@code Future} handle that just passes a value
18  * through: e.g. Spring's {@link AsyncResult}, EJB 3.1's {@link javax.ejb.AsyncResult},
19  * or {@link java.util.concurrent.CompletableFuture#completedFuture(Object)}.
20  * Future是代理返回的切实的异步返回,用以追踪异步方法的返回值。当然也可以使用AsyncResult类(实现ListenableFuture接口)(Spring或者EJB都有)或者CompletableFuture类
21  * @author Juergen Hoeller
22  * @author Chris Beams
23  * @since 3.0
24  * @see AnnotationAsyncExecutionInterceptor
25  * @see AsyncAnnotationAdvisor
26  */
27 @Target({ElementType.METHOD, ElementType.TYPE})
28 @Retention(RetentionPolicy.RUNTIME)
29 @Documented
30 public @interface Async {
31 
32     /**
33      * A qualifier value for the specified asynchronous operation(s).
34      * <p>May be used to determine the target executor to be used when executing this
35      * method, matching the qualifier value (or the bean name) of a specific
36      * {@link java.util.concurrent.Executor Executor} or
37      * {@link org.springframework.core.task.TaskExecutor TaskExecutor}
38      * bean definition.用以限定执行方法的执行器名称(自定义):Executor或者TaskExecutor
39      * <p>When specified on a class level {@code @Async} annotation, indicates that the
40      * given executor should be used for all methods within the class. Method level use
41      * of {@code Async#value} always overrides any value set at the class level.
42      * @since 3.1.2   加在类上表示整个类都使用,加在方法上会覆盖类上的设置
43      */
44     String value() default "";
45 
46 }

SaaS作为租户系统,需要为租户(C端)提供注册、购买、业务系统的入口,还得为B端(运营/运维)提供租户管理、流量监控、服务状态监控运维入口,示意图如下: 上图源码注释已经写的很清晰了哈,主要注意3点:

1)返回值:不要返回值直接void;需要返回值用AsyncResult或者CompletableFuture

2)可自定义执行器并指定例如:@Async("otherExecutor")

3)@Async 必须不同类间调用: A类--》B类.C方法()(@Async注释在B类/方法中),如果在同一个类中调用,会变同步执行,例如:A类.B()-->A类.@Async C(),原因是:底层实现是代理对注解扫描实现的,B方法上没有注解,没有生成相应的代理类。(当然把@Async加到类上也能解决但所有方法都异步了,一般不这么用!)

慕课网 Java高级工程师 - SaaS服务

SaaS的服务对象是租户,那么新进入平台未进行服务购买及认证的用户我们暂且称为散户,为了推广平台增加销售成功率,散户登录进入后会跳转进入产品介绍及销售页面,提供详细的产品功能清单及费用信息,提供演示平台供散户进行试用。 处。