rx java在多个不同的线程上运行observables

时间:2016-06-16 14:37:12

标签: multithreading asynchronous rx-java

我是rx java的新手,所以我可能会遗漏一些非常基本的东西。在下面的代码示例中,我喜欢的内容是:

  1. SampleController在http-nio线程上接收请求

  2. CompositeService处理在新线程A上运行并释放nio请求线程。

  3. CompositeService调用HelloService,它在线程B上进行网络调用。

  4. CompositeService调用WorldService,它在线程C上进行网络调用。

  5. 3和4同时运行,当结果准备就绪时,我们使用结果在线程A上进行网络调用。

    相反,我看到的是3和4在http-nio线程上顺序执行,只有CompositeService在新线程上执行。似乎我在3和4中的subscribeOn调用没有任何效果。如何同时运行3和4?

    SampleController

    @RestController
    @RequestMapping("/rx-java-sample")
    public class SampleController {
    
        private static Logger log = LoggerFactory.getLogger(SampleController.class);
    
        @Autowired
        private CompositeService compositeService;
    
        @RequestMapping(method = RequestMethod.GET, 
            produces = { MediaType.APPLICATION_JSON_VALUE })
        public DeferredResult<String> getCompositeString() 
                        throws ApiGatewayException, ApiValidationException {
            log.info("Received getCompositeObject request");
    
            Observable<String> compositeObject = compositeService.getCompositeString();
    
            return toDeferredResult(compositeObject);
        }
    
        private DeferredResult<String> toDeferredResult(Observable<String> compositeObject) {
            DeferredResult<String> result = new DeferredResult<String>();
    
            compositeObject.subscribeOn(Schedulers.newThread()).subscribe(new Observer<String>() {
                @Override
                public void onCompleted() {
                }
    
                @Override
                public void onError(Throwable throwable) {
                    result.setErrorResult(throwable);
                }
    
                @Override
                public void onNext(String compositeString) {
                    log.info("Returning compositeObject: " + compositeString);
                    result.setResult(compositeString);
                }
            });
    
            return result;
        }
    }
    

    HelloService

    @Service
    public class HelloService {
    
        private Logger log = LoggerFactory.getLogger(HelloService.class);
    
        public Observable<String> getHello() {
            log.trace("calling get hello");
            return Observable.just(makeNetworkCall());
        }
    
        private String makeNetworkCall() {
            log.trace("making hello network call");
            return "hello";
        }
    }
    

    WorldService

    @Service
    public class WorldService {
    
    private Logger log = LoggerFactory.getLogger(HelloService.class);
    
        public Observable<String> getWorld() {
            log.trace("calling get world");
            return Observable.just(makeNetworkCall());
        }
    
        private String makeNetworkCall() {
            log.trace("making world network call");
            return "world";
        }
    }
    

    CompositeService

    @Service
    public class CompositeService {
    
        private Logger log = LoggerFactory.getLogger(CompositeService.class);
    
        @Autowired
        private HelloService helloService;
    
        @Autowired
        private WorldService worldService;
    
        public Observable<String> getCompositeString() {
            log.trace("Calling getCompositeObject");
    
            Observable<String> foo = helloService.getHello().subscribeOn(Schedulers.newThread());
            Observable<String> bar = worldService.getWorld().subscribeOn(Schedulers.newThread());
    
            return Observable.zip(foo, bar, (f, b) -> makeNetworkCall(f,b));
        }
    
        private String makeNetworkCall(String hello, String world) {
            log.trace("making composite network call");
            return hello + " " +  world;
        }
    }
    

    登录

    2016-06-16 07:10:13 INFO [http-nio-9050-exec-1] [SampleController.java:32] Received getCompositeObject request
    2016-06-16 07:10:13 TRACE [http-nio-9050-exec-1] [CompositeService.java:23] Calling getCompositeObject
    2016-06-16 07:10:13 TRACE [http-nio-9050-exec-1] [HelloService.java:15] calling get hello
    2016-06-16 07:10:13 TRACE [http-nio-9050-exec-1] [HelloService.java:20] making hello network call
    2016-06-16 07:10:13 TRACE [http-nio-9050-exec-1] [WorldService.java:15] calling get world
    2016-06-16 07:10:13 TRACE [http-nio-9050-exec-1] [WorldService.java:20] making world network call
    2016-06-16 07:10:13 TRACE [RxNewThreadScheduler-3] [CompositeService.java:32] making composite network call
    2016-06-16 07:10:13 INFO [RxNewThreadScheduler-3] [SampleController.java:54] Returning compositeObject: hello world
    

2 个答案:

答案 0 :(得分:1)

您希望Observable::defer用于这些情况:

  public Observable<String> getWorld() {
      log.trace("calling get world");

      return Observable.defer(() -> makeNetworkCall());
  }

这可以确保您的代码被调用每个时间,并且可以观察到observable。

另外,我建议使用Schedulers.io();它是一个可配置的线程池,默认情况下会根据需要进行扩展。

答案 1 :(得分:0)

看来问题是我使用Observable.just而不是Observable.fromCallable。从上面修改服务代码如下所示产生了我正在寻找的行为。如果这是“推荐”的方式,我仍在寻找反馈。特别是我不确定在CompositeService中使用toBlocking()是否正确。我可能会在我的代码中广泛使用这种模式,我希望能够做到这一点。

<强> HelloService的

@Service
public class HelloService {

    private Logger log = LoggerFactory.getLogger(HelloService.class);

    public Observable<String> getHello() {
        log.trace("calling get hello");

        return Observable.fromCallable(() -> { 
            return makeNetworkCall();
        });
    }

    private String makeNetworkCall() {
        log.trace("making hello network call");
        return "hello";
    }
}

<强> WorldService

@Service
public class WorldService {

private Logger log = LoggerFactory.getLogger(HelloService.class);

    public Observable<String> getWorld() {
        log.trace("calling get world");

        return Observable.fromCallable(() -> { 
            return makeNetworkCall();
        });
    }

    private String makeNetworkCall() {
        log.trace("making world network call");
        return "world";
    }
}

CompositeService

@Service
public class CompositeService {

    private Logger log = LoggerFactory.getLogger(CompositeService.class);

    @Autowired
    private HelloService helloService;

    @Autowired
    private WorldService worldService;

    public Observable<String> getCompositeString() {
        return Observable.fromCallable(() -> { 
            return getCompositeStringImpl().toBlocking().single();
        });
    }

    public Observable<String> getCompositeStringImpl() {
        log.trace("Calling getCompositeObject");

        Observable<String> foo = helloService.getHello().subscribeOn(Schedulers.newThread());
        Observable<String> bar = worldService.getWorld().subscribeOn(Schedulers.newThread());

        return Observable.zip(foo, bar, (f, b) -> makeNetworkCall(f, b));
    }

    private String makeNetworkCall(String hello, String world) {
        log.trace("making composite network call");
        return hello + " " + world;
    }
}

<强>日志

2016-06-16 08:15:50 TRACE [RxNewThreadScheduler-1] [CompositeService.java:29] Calling getCompositeObject
2016-06-16 08:15:50 TRACE [RxNewThreadScheduler-1] [HelloService.java:15] calling get hello
2016-06-16 08:15:50 TRACE [RxNewThreadScheduler-1] [WorldService.java:15] calling get world
2016-06-16 08:15:50 TRACE [RxNewThreadScheduler-2] [HelloService.java:23] making hello network call
2016-06-16 08:15:50 TRACE [RxNewThreadScheduler-3] [WorldService.java:23] making world network call
2016-06-16 08:15:50 TRACE [RxNewThreadScheduler-3] [CompositeService.java:38] making composite network call
2016-06-16 08:15:50 INFO [RxNewThreadScheduler-1] [SampleController.java:54] Returning compositeObject: hello world