rxjava - 获得响应并并行插入diff

时间:2017-02-10 20:57:13

标签: java asynchronous rx-java observable

我使用rxjava使用Observable.zip并行处理两个请求。我想要做的是,在一个observable say response我得到一个回复​​而在其他observable say diff我试图获得响应并在DB中保存这个差异。问题是我不确定如何实现我的要求,因为如果diff observable得到回复,response observable没有完成

这是我正在做的......

public ServiceResponse getDummyResponse(ServiceRequest serviceRequest, String prodId){
    Observable<ServiceResponse> subInfoDummyObservable = getDummyResonseGenericObservable();
    Observable<ServicesDiff> reObservable = getServicesDiffGenericObservable(serviceRequest, prodId);

    Observable<ServiceResponse> responseObservable = Observable.zip(
            subInfoDummyObservable,
            reObservable,
            new Func2<ServiceResponse, ServicesDiff, ServiceResponse>() {
                @Override
                public ServiceResponse call(ServiceResponse serviceResponse, ServicesDiff diffResponse) {
                    return serviceResponse;
                }
            }
    );

    ServiceResponse serviceResponse = responseObservable.toBlocking().single();

    return serviceResponse;
}

Observable<ServiceResponse> getDummyResonseGenericObservable() {
    return GenericHystrixCommand.toObservable("getDummyResonseGenericObservable", "getDummyResonseGenericObservable", () -> new ServiceResponse(),(t) -> {return null;} );
}

Observable<ServicesDiff> getServicesDiffGenericObservable(ServiceRequest serviceRequest, String prodId) {
    return GenericHystrixCommand.toObservable("getServicesDiffGenericObservable", "getServicesDiffGenericObservable", () -> getBothServiceResponses(serviceRequest, prodId),(t) -> {return null;} );
}

public ServicesDiff getBothServiceResponses(ServiceRequest serviceRequest, String prodId) {
    Observable<String> service1ResponseObservable = getService1GenericObservable(prodId);
    Observable<ServiceResponse> service2ResponseObservable = getService2GenericObservable(serviceRequest, prodId);

    Observable<ServicesDiff> observable = Observable.zip(
            service1ResponseObservable, service2ResponseObservable,
            new Func2<String, ServiceResponse, ServicesDiff>() {
                @Override
                public ServicesDiff call(String service1Response, ServiceResponse service2Response) {
                    return aggregate(service1Response, service2Response); // never reaches this point**********
                }
            }
    );
    ServicesDiff response = observable.toBlocking().single();

    return response;
}

我在aggregate方法中将差异插入到数据库中,但它根本不会到达aggregate。请让我知道我在这里做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

Observable是如何使用数据的说明。在您的代码示例中,您没有订阅,您实际上并没有使用数据。您刚刚描述了如何请求,但订阅部分(触发请求的部分)丢失了。

所以,如果我重写一下你的代码:

class Aggregate {
    Aggregate(String reponse, ServicesDiff diff) {
        ...
    }
}

Observable<String> getService1GenericObservable(String prodId) {
    ...
}

Observable<ServicesDiff> getServicesDiffGenericObservable(ServiceRequest serviceRequest, String prodId) {
    ...
}

public Observable<Aggregate> getBothServiceResponses(ServiceRequest serviceRequest, String prodId) {
    Observable<String> service1ResponseObservable = getService1GenericObservable(prodId);
    Observable<ServiceResponse> service2ResponseObservable = getService2GenericObservable(serviceRequest, prodId);

    return Observable<Aggregate> observable = Observable.zip(
            service1ResponseObservable, service2ResponseObservable,
            new Func2<String, ServiceResponse, ServicesDiff>() {
                @Override
                public ServicesDiff call(String service1Response, ServiceResponse service2Response) {
                    return aggregate(service1Response, service2Response);
                }
            }
    );
}

您只需执行此操作即可访问结果聚合:

getBothServiceResponses(serviceRequest, prodId).subscribe(...)