Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确管理反应堆中的可关闭资源_Java_Project Reactor - Fatal编程技术网

Java 如何正确管理反应堆中的可关闭资源

Java 如何正确管理反应堆中的可关闭资源,java,project-reactor,Java,Project Reactor,我有一个http客户端和执行器,当所有的工作都完成时应该关闭它 我正在尝试使用Flux.using方法,其使用方式如RxJava 1.x所述: 我的资源创建方法: public static Flux<GithubClient> createResource(String token, int connectionCount) { return Flux.using(

我有一个http客户端和执行器,当所有的工作都完成时应该关闭它

我正在尝试使用Flux.using方法,其使用方式如RxJava 1.x所述:

我的资源创建方法:

public static Flux<GithubClient> createResource(String token,
                                                int connectionCount) {

    return Flux.using(
            () -> {
                logger.info(Thread.currentThread().getName() + " : Created and started the client.");
                return new GithubClient(token, connectionCount);
            },
            client -> {
                logger.info(Thread.currentThread().getName() + " : About to create Observable.");
                return Flux.just(client);
            },
            client -> {
                logger.info(Thread.currentThread().getName() + " : Closing the client.");
                client.close();
            },
            false
    ).doOnSubscribe(subscription -> logger.info("subscribed"));
}
并没有找到任何反应堆的例子


谢谢

我阅读了再次使用的文档,发现了我的错误。使用
返回流量返回客户端。just(客户端)没有意义,因为流量立即终止,从而触发客户端关闭

我最终实现了:

public static Flux<StateMutator> createAndExecute(GithubPublicConfiguration config,
                                                  Function<GithubClient, Flux<StateMutator>> toExecute) {

    return Flux.using(
            () -> {
                logger.debug(Thread.currentThread().getName() + " : Created and started the client.");
                return new GithubClient(entityModelHandler, config.getAccessToken(), config.getConnectionCount());
            },
            client -> toExecute.apply(client),
            client -> {
                logger.debug(Thread.currentThread().getName() + " : Closing the client.");
                client.close();
            },
            false
    );
}
现在所有的操作都按顺序进行了

[main] INFO com.sapho.services.githubpublic.client.GithubClient - main : Created and started the client.
[main] INFO com.sapho.services.githubpublic.client.GithubClient - main : About to create Observable.
[main] INFO com.sapho.services.githubpublic.client.GithubClient - subscribed
[main] INFO com.sapho.services.githubpublic.client.GithubClient - main : Closing the client.

java.lang.IllegalStateException: Client instance has been closed.

at jersey.repackaged.com.google.common.base.Preconditions.checkState(Preconditions.java:173)
at org.glassfish.jersey.client.JerseyClient.checkNotClosed(JerseyClient.java:273)
public static Flux<StateMutator> createAndExecute(GithubPublicConfiguration config,
                                                  Function<GithubClient, Flux<StateMutator>> toExecute) {

    return Flux.using(
            () -> {
                logger.debug(Thread.currentThread().getName() + " : Created and started the client.");
                return new GithubClient(entityModelHandler, config.getAccessToken(), config.getConnectionCount());
            },
            client -> toExecute.apply(client),
            client -> {
                logger.debug(Thread.currentThread().getName() + " : Closing the client.");
                client.close();
            },
            false
    );
}
GithubClient.createAndExecute(config,
            client -> client.loadRepository(organization, repository))