如何在非阻塞线程中同步获取单声道对象?
Posted
技术标签:
【中文标题】如何在非阻塞线程中同步获取单声道对象?【英文标题】:How to synchronization get mono object in nonblockthread? 【发布时间】:2020-08-23 19:38:38 【问题描述】:-
总结问题
我想用orika来映射java bean,我创建了一个orika过滤器,代码是
@Slf4j
@Configuration
public class OrikaMapperConfig implements ApplicationContextAware
@Bean
public MapperFactory init(ApplicationContext applicationContext)
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.registerFilter(new CustomFilter<Object, Object>()
@Override
public boolean filtersSource()
return true;
@Override
public boolean filtersDestination()
return true;
@Override
public <S, D> boolean shouldMap(Type<S> sourceType, String sourceName, S source, Type<D> destType, String destName, D dest, MappingContext mappingContext)
return true;
@Override
public <D> D filterDestination(D destinationValue, Type<?> sourceType, String sourceName, Type<D> destType, String destName, MappingContext mappingContext)
//how to get login User here with springfluxsecurity?
//I want to modify the destinationValue dynamic with login user role
return destinationValue;
@Override
public <S> S filterSource(S sourceValue, Type<S> sourceType, String sourceName, Type<?> destType, String destName, MappingContext mappingContext)
return sourceValue;
);
Map<String, IMapper> mappers = applicationContext.getBeansOfType(IMapper.class);
mappers.forEach((key, iMapper) -> iMapper.register(mapperFactory));
return mapperFactory;
@Bean
public MapperFacade mapperFacade(MapperFactory mapperFactory)
return mapperFactory.getMapperFacade();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
init(applicationContext);
-
描述您的尝试
在filterDestination
的方法中我尝试使用
ReactiveSecurityContextHolder.getContext().map(context -> (User) context.getAuthentication().getPrincipal()).block()
获取登录用户,但出现错误
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-tcp-nio-4
问题: 我想知道如何解决它,谢谢您的回复。
【问题讨论】:
错误很明显不要使用block
,向我们展示您的整个过滤器目标,并解释您真正想要做什么,为什么需要该值。
【参考方案1】:
.block()
调用已在不久前被弃用。至于您的.map
方法需要与User
本身而不是Mono<User>
一起使用,您应该应用的运算符是.flatMap
。这将从 Mono 中解开您的用户,并在解决用户“承诺”时触发。
【讨论】:
以上是关于如何在非阻塞线程中同步获取单声道对象?的主要内容,如果未能解决你的问题,请参考以下文章