“Java:Module”与“Vertx: Future异步操作设计”:页面之间的差异

来自WHY42
(页面间差异)
无编辑摘要
 
 
第1行: 第1行:
Java Platform Module System (JPMS)是Java9中的最大的变动,在[https://jcp.org/en/jsr/detail?id=376|JSR 376: JavaTM Platform Module System]中引入。
=Future相关操作的设计=
== AsyncResult ==
AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。
<syntaxhighlight lang="java">
interface AsyncResult<T> {
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
    T getResult();
    Throwable cause();
    boolean succeeded();
    boolean failed();


首先,模块化的jar文件(Modular JAR)中需要一个<syntaxhighlight lang="java" inline>module-info.class</syntaxhighlight>文件,这个文件里面存储了模块的信息。倘若没有,那么认为这是一个<syntaxhighlight lang="java" inline>automatic module</syntaxhighlight>,也即用jar的文件名作为模块来导出。
    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
    <V> AsyncResult<V> map(Function<T, V> mapper);
    <V> AsyncResult<V> map(V value);
    <V> AsyncResult<V> mapEmpty();
    AsyncResult<T> otherwise(T value);
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
    AsyncResult<T> otherwiseEmpty();
}
</syntaxhighlight>


除此之外,jar文件中通过<span class="article-label">META-INF</span>目录存储安全、版本等其他信息
==Future==
Future表示一个尚未完成的异步操作,并继承自AsyncResult
<syntaxhighlight lang="java">
interface Future<T>
    extends AsyncResult<T> {
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper,
                      Function<Throwable, Future<V>> failureMapper);
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);
    CompletionStage<T> toCompletionState();
}


* MANIFEST.MF:定义package相关信息
</syntaxhighlight>
* INDEX.LIST:This file is generated by the new "-i" option of the jar tool, which contains location information for packages defined in an application.  It is part of the JarIndex implementation and used by class loaders to speed up their class loading process.
* The signature file for the JAR file.  'x' stands for the base file name.
* x.DSA:The signature block file associated with the signature file with the same base file name. This file stores the digital signature of the corresponding signature file.
* services/:This directory stores all the service provider configuration files.


 
[[Category:Vert.x]]
[[Category:Java]]

2021年7月25日 (日) 08:21的版本

Future相关操作的设计

AsyncResult

AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。

interface AsyncResult<T> {
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
    T getResult();
    Throwable cause();
    boolean succeeded();
    boolean failed();

    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
    <V> AsyncResult<V> map(Function<T, V> mapper);
    <V> AsyncResult<V> map(V value);
    <V> AsyncResult<V> mapEmpty();
    AsyncResult<T> otherwise(T value);
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
    AsyncResult<T> otherwiseEmpty();
}

Future

Future表示一个尚未完成的异步操作,并继承自AsyncResult

interface Future<T>
    extends AsyncResult<T> {
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper, 
                      Function<Throwable, Future<V>> failureMapper);
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);
    CompletionStage<T> toCompletionState();
}