我想在不使用块()/blockFirst()/blockLast()的情况下将mono转换为object java。请告诉我转换的方法。
发布于 2019-06-13 03:45:22
官方路径是block()
,它应该作为一个警告,告诉你可能做错了什么,因为阻止非阻塞系统就像是搬起石头砸自己的脚。
事实上,我们最近通过在使用这些Schedulers
时抛出一个异常来禁止在Reactor的一些非阻塞API上这样做:
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
如果你真的想搬起石头砸自己的脚,总有.toFuture().get()
……¯\_(ツ)_/¯
的反模式解决方案。
发布于 2020-10-31 02:15:00
在Spring boot2中使用ReactiveMongoDbRepository和WebFlux在MongoDb文档/模型上生成序列/id时,我遇到了类似的错误。我在非阻塞方法/函数上使用.toFuture().get()
作为承诺来处理它。
@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();
}
https://stackoverflow.com/questions/56521998
复制相似问题