2

在我之前的问题(感谢@Andy Wilkinson)中,我发现所有传入的请求undertowEmbeddedServletContainer都由工作线程处理(阻塞操作)。

根据安迪的说法,我尝试添加一个UndertowBuilderCustomizer以覆盖ServletInitializerHandler以使用非阻塞处理程序处理传入请求。

@Bean
public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory(){
    UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory = new UndertowEmbeddedServletContainerFactory();
    undertowEmbeddedServletContainerFactory.addBuilderCustomizers(new UndertowBuilderCustomizer() {

        @Override
        public void customize(Undertow.Builder builder) {
            builder.setHandler(new HttpHandler() {
                @Override
                public void handleRequest(HttpServerExchange exchange) throws Exception {
                    exchange.getResponseSender().send("test");
                }
            });
        }
    });
    return undertowEmbeddedServletContainerFactory;
}

在这个定制器中,我rootHandler为 NIO 处理程序设置了构建器。UndertowEmbeddedServletContainer但它在启动阶段被覆盖ServletInitializerHandler

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);
    }
}

正如这个问题的标题所说:我试图同时拥有阻塞和非阻塞处理程序,其中阻塞处理程序通过@Controller注释管理,而 NIO 处理程序由 Sp​​ring 管理。

我找到了一个解决方案,但是作为一个初学者,我不知道它是否是一个好的解决方案。

HandlerPath注解

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

    public String path() default "";

}

创建实现 HttpHandler 的 bean

@Component
@HandlerPath(path = "/hello-nio")
public class HelloHandler implements HttpHandler{

    @Autowired
   HelloService helloService;

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        exchange.getResponseSender().send(helloService.sayHello("Josselin"));
    }  
}

创建一个简单的控制器

@Controller
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(){
        return "hello";
    }
}

创建一个实现 ServletExtension 的类

public class NonBlockingHandlerExtension implements ServletExtension{

    @Override
    public void handleDeployment(DeploymentInfo deploymentInfo, final ServletContext servletContext) {
        deploymentInfo.addInitialHandlerChainWrapper(new HandlerWrapper() {
            @Override
            public HttpHandler wrap(final HttpHandler handler) {

                WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
                Map<String, Object> handlers = ctx.getBeansWithAnnotation(HandlerPath.class);

                PathHandler rootHandler = new PathHandler();
                rootHandler.addPrefixPath("/", handler);
                for(Map.Entry<String, Object> handlerEntry : handlers.entrySet()){
                    if(handlerEntry.getValue() instanceof HttpHandler){
                        HttpHandler httpHandler = (HttpHandler) handlerEntry.getValue();
                        String path = httpHandler.getClass().getAnnotation(HandlerPath.class).path();
                        rootHandler.addPrefixPath(path, httpHandler);
                    }
                }
                return rootHandler;
            }
        });
    }
}

在这个方法中,默认ServletInitializerhandler绑定到“/”上下文,由spring管理,所以所有阻塞请求都可以由@Controller(s)来处理。然后我尝试发现所有用 注释的 bean ,然后在based on属性中@HandlerPath添加一个新的。prefixPathrootHandler@HandlerPath.path

最后

创建目录 META-INF.services

创建一个文件 io.undertow.servlet.ServletExtension 并添加行:

org.me.undertow.NonBlockingHandlerExtension

结果

一切都像魅力一样工作,当绑定 URL 被命中时调用 NIO 处理程序,阻塞处理程序也是如此。

谁能告诉我这个解决方案是否可以以任何方式改进?此外,由于 NIO 处理程序 URL 不是由 Spring 管理的,我想我必须使用globaleMethodSecurity并设置@PreAuthorize来保护 NIO 处理程序?

4

1 回答 1

0

我最近遇到了类似的问题,我发现UndertowEmbeddedServletContainerFactory可以addDeploymentInfoCustomizers()用来将自定义HttpHandler放在处理程序链开头的提供。

例子

@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory(RootHandler rootHandler) {
    UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();

    factory.addDeploymentInfoCustomizers(deploymentInfo ->
            deploymentInfo.addInitialHandlerChainWrapper(rootHandler::setNext));

    return factory;
}

样本RootHandler

@Component
public class RootHandler implements HttpHandler {

    private HttpHandler next;

    @Autowired
    public RootHandler(...) {
    }

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        if (exchange.getRelativePath().startsWith("/service")) {
            handleServiceRequest(exchange);
        } else {
            next.handleRequest(exchange);
        }
    }

    private void handleServiceRequest(HttpServerExchange exchange) {

        // ...

        exchange.getResponseSender().send("OK");
    }

    public HttpHandler setNext(HttpHandler next) {
        this.next = next;
        return this;
    }
}
于 2016-04-27T21:35:32.863 回答