简体   繁体   中英

convert mono to object java without using block()/blockFirst()/blockLast()

I want to convert mono to object java without using block()/blockFirst()/blockLast(). Please tell me the way to convert it.

The official path is block() , which should serve as a warning that you may be doing something wrong, because blocking a non-blocking system is like shooting yourself in the foot.

In fact so much so that we recently forbid to do so on some of the non-blocking Schedulers of Reactor by throwing an exception when using these APIs:

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1

If you really like to shoot yourself in the foot, there is always the antipattern solution of .toFuture().get() ... ¯\\_(ツ)_/¯

I was facing similar error while generating sequence/id on MongoDb document/model using ReactiveMongoDbRepository and WebFlux in the Spring boot 2. I have handle it using the .toFuture().get() as promise on non-blocking method/function.

@Override
public long generateSequence(final String sequenceName) throws InterruptedException, ExecutionException {
        return mongoOperations.findAndModify(new Query(Criteria.where("_id").is(sequenceName)),
                new Update().inc("sequence", 1), DatabaseSequence.class).doOnSuccess(object -> {
                    logger.debug("databaseSequence is evaluated: {}", object);
                }).toFuture().get().getSequence();
    }

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