响应式API

之前在concrete client部分看到如何同步调用concrete service,本小节我们来看看如何进行响应式调用。

示例

demo-api模块的pom.xml

        <!-- 异步接口编译期生成器 -->
        <dependency>
            <groupId>org.coodex</groupId>
            <artifactId>concrete-rx-builder</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- rxjava2 异步接口生成插件 -->
        <dependency>
            <groupId>org.coodex</groupId>
            <artifactId>concrete-rx-observable</artifactId>
            <scope>provided</scope>
        </dependency>

demo-boot模块的pom.xml

        <!-- Observable 异步调用的插件 -->
        <dependency>
            <groupId>org.coodex</groupId>
            <artifactId>concrete-rx-observable</artifactId>
            <scope>test</scope>
        </dependency>

demo-boot的org.coodex.concrete.demo.client.JaxRSClientDemo1


public class JaxRSClientDemo1 {
    private final static Logger log = LoggerFactory.getLogger(JaxRSClientDemo1.class);

    public static void main(String[] args) {
        log.info("restful service invoked: {}",
                Client.getInstance(SubjoinExampleService.class, "jaxrs")
                        .add(11, 12)
        );

        Client.getInstance(CompletableSubjoinExampleService.class, "jaxrs")
                .add(11, 12)
                .whenComplete((integer, throwable) -> {
            if (throwable != null) {
                log.error(throwable.getLocalizedMessage(), throwable);
            } else {
                log.info("completable restful service invoked: {}", integer);
            }
        });

        Client.getInstance(ObservableSubjoinExampleService.class, "jaxrs")
                .add(11,12)
                .subscribe(integer -> {
                    log.info("observable restful service invoked: {}", integer);
                });

    }
}

上面的CompletableSubjoinExampleServiceObservableSubjoinExampleServicedemo-api包在编译期生成的,类似于lombok,不需要自行编写。

run service, run JaxRXClientDemo1

09:13:13.489 [main] INFO org.coodex.concrete.demo.client.JaxRSClientDemo1 - restful service invoked: 23
09:13:13.803 [rx-client.scheduler-1] INFO org.coodex.concrete.demo.client.JaxRSClientDemo1 - completable restful service invoked: 23
09:13:13.804 [rx-client.scheduler-1] INFO org.coodex.concrete.demo.client.JaxRSClientDemo1 - observable restful service invoked: 23

注意看一下线程池,同步调用方式是main线程,两个响应式接口使用的是rx-client.scheduler线程池

扩展点

concrete service在编译期能够自动生成响应式API,默认支持Completable,concrete也提供了rxjava2的Observable插件,开发者也可以扩展自己顺手的响应式API。

扩展时,需要做的就是:

(ɔ) coodex.org 2016 - 2020            修订时间: 2020-03-09 02:56:40

results matching ""

    No results matching ""