首页 > 解决方案 > 如何使解析器异步运行

问题描述

我正在设置基于 java 的 graphQl 应用程序并发现 graphql-java-tools 确实很方便解决问题,虽然它非常简单使用 graphql-java 使字段解析器异步我找不到使用 graphql-java-tools 的方法

我试过了

@Bean
    public ExecutionStrategy executionStrategy() {
        return new AsyncExecutionStrategy();
    }

我在这里使用解析器进行测试

@Component
public class VideoResolver  implements GraphQLResolver<Video> {


     public Episode getEpisode(Video video){

        Episode result = new Episode();

        result.setTitle("episodeTitle");
        result.setUuid("EpisodeUuid");
        result.setBrand("episodeBrand");
        try {
            Thread.sleep(2000L);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

    public List<Images> getImages(Video video){

        Images image = new Images();
        image.setFileName("Image FileName1");
        List<Images> imageList = new ArrayList<>();
        imageList.add(image);

        try {
            Thread.sleep(2000L);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {

            System.out.println("Exxxxxxxxxx");
        }
        return imageList;
    }
}

假设这应该在大约 2 秒内运行并打印两个不同的流,但不需要 4 并且打印它都在同一个流中

标签: graphqlgraphql-java

解决方案


推荐阅读