非阻塞并行处理并相互传递数据

时间:2018-09-27 14:35:52

标签: java multithreading jdbc spring-data-jpa rx-java2

我想使每个父级和子级实体都具有并行性,该过程必须快速返回childEntities。因此,我无法明确决定哪种方法适合此过程。因为在那种情况下,并行线程还会一次调用http调用和springdataRepository的save方法(由于JDBC连接池的大小,我将管理线程的大小)。

顺便说一句,我刚刚尝试过RxJava-2库。

我希望->如果并行流程引发异常,则必须继续执行onErrorResumeNext方法(或附近的某种方法)并在异常发生后完成所有过程。但是它完全中止了流程。

所以我需要->完全非阻塞并行流,如果其中之一抛出异常,只需捕获它,然后继续其余并行过程即可。

有什么想法吗?任何其他解决方案的想法都是可以接受的。(例如手动线程管理)

那是我尝试过的,但是没有按预期工作。

package com.mypackage;

import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TestApp {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        List<String> createdParentEntities = new ArrayList<>();
        List<String> erroredResponses = new ArrayList<>();
        List<String> childEntities = new ArrayList<>();

        Flowable.range(1, 100) // 100: is not fixed normalle
                .parallel(100) // It will be changed according to size
                .runOn(Schedulers.io())
                .map(integer -> createParentEntity(String.valueOf(integer)))
                .sequential()
                .onErrorResumeNext(t -> {
                    System.out.println(t.getMessage());
                    if (t instanceof Exception) {
                        erroredResponses.add(t.getMessage());
                        return Flowable.empty();
                    } else {
                        return Flowable.error(t);
                    }
                })
                .blockingSubscribe(createdParentEntities::add);

        if (!createdParentEntities.isEmpty()) {
            Flowable.fromIterable(createdParentEntities)
                    .parallel(createdParentEntities.size())
                    .runOn(Schedulers.io())
                    .doOnNext(TestApp::createChildEntity)
                    .sequential()
                    .blockingSubscribe(childEntities::add);
        }

        System.out.println("====================");
        long time = System.currentTimeMillis() - start;
        log.info("Total Time : " + time);

        log.info("TOTAL CREATED ENTITIES : " + createdParentEntities.size());
        log.info("CREATED ENTITIES " + createdParentEntities.toString());
        log.info("ERRORED RESPONSES " + erroredResponses.toString());
        log.info("TOTAL ENTITIES : " + childEntities.size());
    }

    public static String createParentEntity(String id) throws Exception {
        Thread.sleep(1000); // Simulated for creation call
        if (id.equals("35") || id.equals("75")) {
            throw new Exception("ENTITIY SAVE ERROR " + id);
        }
        log.info("Parent entity saved : " + id);
        return id;
    }

    public static String createChildEntity(String parentId) throws Exception {
        Thread.sleep(1000);// Simulated for creation call
        log.info("Incoming entity: " + parentId);
        return "Child Entity:  " + parentId + " parentId";
    }
}

0 个答案:

没有答案