rxjava2在多线程上执行服务调用

时间:2018-06-15 16:22:48

标签: multithreading rx-java2

我是Rxjava2的新手,基本上我正在尝试对多线程进行一些服务调用。我在某些方面获得了多线程,但并不是我预期的所有地方。

例如:我已附上样本单元测试

import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import lombok.Data;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlowableTest
{
    static Map<String, ClaimProcessor> mp = new HashMap<>();

    static {
        mp.putIfAbsent("vehicle", new VehicleClaimProcessor());
        mp.putIfAbsent("hospital", new HospitalClaimProcessor());
    }

    public static void main(String[] args)
    {
        Claims claims = new Claims();
        claims.setClaimType("vehicle");
        Claims claims1 = new Claims();
        claims1.setClaimType("hospital");

        ClaimUser user1 = new ClaimUser();
        user1.setClaimList(Arrays.asList(claims, claims1));

        ClaimUser user2 = new ClaimUser();
        user2.setClaimList(Arrays.asList(claims, claims1));

        List<ClaimUser> userList = Arrays.asList(user1, user2);
        Flowable.fromIterable(userList)
                .flatMap(d -> Flowable.just(getClaimStatus(d)).subscribeOn(Schedulers.io()))
                .map(d -> updateClaimStatus(d))
                //.subscribeOn(Schedulers.io())
                .subscribe(d -> System.out.println("Processed"),
                err -> System.err.println(err.getMessage()));
    }

    private static ClaimUser updateClaimStatus(ClaimUser d)
    {
        //Update to DB
        return d;
    }

    private static ClaimUser getClaimStatus(ClaimUser d)
    {
        System.out.println(" ClaimUser Thread "+ Thread.currentThread().getName());
        return Flowable.fromIterable(d.getClaimList()).flatMap(dd -> {
            return Flowable.just(mp.get(dd.getClaimType()).processClaim(dd))
                    .subscribeOn(Schedulers.io());
        }).subscribeOn(Schedulers.io())
                .toList().map(ddd -> calculateOverallStatus(d))
                .subscribeOn(Schedulers.io())
                        .blockingGet();

    }

    private static ClaimUser calculateOverallStatus(ClaimUser user)
    {
        //calculating overall status by all claims status
        user.setStatus("DOne");
        return user;
    }

    private interface ClaimProcessor
    {
        Claims processClaim(Claims claims);
    }

    private static class VehicleClaimProcessor implements ClaimProcessor
    {
        @Override public Claims processClaim(Claims claims)
        {
            System.out.println(" VehicleClaimProcessor Thread "+ Thread.currentThread().getName());
            return claims;
        }
    }

    private static class HospitalClaimProcessor implements ClaimProcessor
    {
        @Override public Claims processClaim(Claims claims)
        {
            System.out.println(" HospitalClaimProcessor Thread "+ Thread.currentThread().getName());
            return claims;
        }
    }
}

@Data class ClaimUser
{
    String name;
    List<Claims> claimList;
    String status;
}

,输出控制台是

ClaimUser Thread main
 VehicleClaimProcessor Thread RxCachedThreadScheduler-2
 HospitalClaimProcessor Thread RxCachedThreadScheduler-2
 ClaimUser Thread main
 Processed
 VehicleClaimProcessor Thread RxCachedThreadScheduler-3
 HospitalClaimProcessor Thread RxCachedThreadScheduler-3
 Processed

我期待的是 - 在多线程上执行用户并在多线程上执行claimprocessor并阻止所有claimprocessor完成并更新状态。

有人可以指导我正确使用它吗?

0 个答案:

没有答案