小编zou*_*oto的帖子

spring-data-mongo 使用 MappingMongoConverter 更新文档中的空值

使用 spring 数据 mongo,我需要更新 mongo 中的文档。

我的实体是这样定义的:

@Document(collection = "Orders")
public class Order{

    @Id
    private Long id;
    private String clientContainerReference;
    private String status;
    private Bigdecimal amount;
    private BigDecimal remainingQuantity;
    ...
}
Run Code Online (Sandbox Code Playgroud)

首先,这个文档被插入到 mongo 中,剩余数量为 100。接下来,订单被更新为空剩余数量。更新(upsert)后,residualQuantity 始终设置为 100。

这是由于类:

org.springframework.data.mongodb.core.convert.MappingMongoConverter
Run Code Online (Sandbox Code Playgroud)

在 writeInternal 方法中,对文档的每个属性都进行了空检查。如果该属性为空,则从生成的 DBObject 中排除它。

entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            if (prop.equals(idProperty)) {
                return;
            }

            Object propertyObj = wrapper.getProperty(prop);

            if (null != propertyObj) {

                if (!conversions.isSimpleType(propertyObj.getClass())) {
                    writePropertyInternal(propertyObj, dbo, prop);
                } else {
                    writeSimpleInternal(propertyObj, dbo, prop);
                }
            } …
Run Code Online (Sandbox Code Playgroud)

spring-data spring-data-mongodb

5
推荐指数
1
解决办法
2629
查看次数

SpringBoot Undertow:如何调度到工作线程

我现在看看一个springboot下载,并不是很清楚(对我来说)如何将传入的http请求分派给工作线程以阻止操作处理.

看看UndertowEmbeddedServletContainer.class类,看起来没有办法让这种行为,因为唯一的HttpHandler是一个ServletHandler,允许@Controller配置

private Undertow createUndertowServer() {
    try {
        HttpHandler servletHandler = this.manager.start();
        this.builder.setHandler(getContextHandler(servletHandler));
        return this.builder.build();
    }
    catch (ServletException ex) {
        throw new EmbeddedServletContainerException(
                "Unable to start embdedded Undertow", ex);
    }
}

private HttpHandler getContextHandler(HttpHandler servletHandler) {
    if (StringUtils.isEmpty(this.contextPath)) {
        return servletHandler;
    }
    return Handlers.path().addPrefixPath(this.contextPath, servletHandler);

}
Run Code Online (Sandbox Code Playgroud)

默认情况下,在进程中,所有请求都由IO-Thread处理,用于非阻塞操作.这是否意味着每个@Controller执行都将由非阻塞线程处理?或者是否有从IO-THREAD或WORKER-THREAD中选择的解决方案?

我尝试编写一个解决方法,但这段代码非常流畅,也许有人有更好的解决方案:

BlockingHandler.class

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BlockingHandler {

    String contextPath() default "/";

}
Run Code Online (Sandbox Code Playgroud)

UndertowInitializer.class

public class UndertowInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        configurableApplicationContext.addBeanFactoryPostProcessor(new UndertowHandlerPostProcessor());
    }

} …
Run Code Online (Sandbox Code Playgroud)

spring-mvc worker spring-boot undertow

1
推荐指数
1
解决办法
2844
查看次数