简体   繁体   中英

How to reassign a variable in reactive Flux stream?

I'm sending requests to a webservice, transform the results into a large csv and persist the csv lines into a database.

As the requests are long running (10-20s), I want to parallelize the requests. I collect all the data in a single StringBuilder that holds the transformed csv lines.

Question: if my chunks of 1000 lines is reached within the csv, how can I take the data out for persistence, while any other concurrent response will be written to a new StringBuilder ?

Because, final variables for a stream cannot be reinitialized.

final StringBuilder sb = new StringBuilder();
AtomicInteger count = new AtomicInteger();

Flux.fromIterable(requests)
    .flatMap(req -> {
        return webClientService.send(req); //assume long running response
    }, 8) //send 8 requests in parallel, as response takes up to 10s
    .map(rsp -> {
        //convert response to csv values and add to StringBuilder
        int c = addCsv(sb, rsp);
        if (count.addAndGet(c) > 1000) {
            //TODO how can I assign a new StringBuilder,
            //so that all further finished responses will append the csv to the new builder?
            //same problem with the counter.
            
            databaseWriter.write(sb.build()); //writes the content so far to db, but not threadsafe so far
        }
        return c;
    })
    .blockLast();
    

Perhaps you could try to avoid side-effects entirely instead, eg with something like:

.map(x -> toCsv(x))
.reduce((a, b) -> {
    if (length(a) < 1000) {
        return concat(a, b);
    }
    databaseWriter.write(a);
    return b;
})
.doOnNext(x -> databaseWriter.write(x))

In my opinion, you can use builtin operators to achieve the same result:

      Flux.fromIterable(requests)
            .flatMap(req -> webClientService
                    .send(req)
                    .subscribeOn(Schedulers.boundedElastic()), 8)// subscribeOn to subscribe from different threads
            .map(resp -> converToCsvLine(resp)) //make some transformations on the respnse
            .window(1000) //split incoming data into 1000 lines
            .flatMap(stringFlux -> stringFlux.collect(Collectors.joining("\n")))// collect last 1000
            .flatMap(s -> Mono.fromRunnable(() -> writeToDb(s))) //do some logic on the collected 1000 lines
            .blockLast();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM