undertow 怎么创建线程_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);

}

默认情况下,在undertow中,所有请求均由IO-

Thread处理以进行非阻塞操作。这是否意味着每个@Controller执行将由非阻塞线程处理?还是有从IO-THREAD或WORKER-

THREAD中选择的解决方案?

我尝试编写一种解决方法,但是此代码非常笨拙,也许有人有更好的解决方案:

BlockingHandler.class

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface BlockingHandler {

String contextPath() default "/";

}

UndertowInitializer.class

public class UndertowInitializer implements ApplicationContextInitializer {

@Override

public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

configurableApplicationContext.addBeanFactoryPostProcessor(new UndertowHandlerPostProcessor());

}

}

UndertowHandlerPostProcessor.class

public class UndertowHandlerPostProcessor implements BeanDefinitionRegistryPostProcessor {

@Override

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);

scanner.addIncludeFilter(new AnnotationTypeFilter(BlockingHandler.class));

for (BeanDefinition beanDefinition : scanner.findCandidateComponents("org.me.lah")){

try{

Class clazz = Class.forName(beanDefinition.getBeanClassName());

beanDefinitionRegistry.registerBeanDefinition(clazz.getSimpleName(), beanDefinition);

} catch (ClassNotFoundException e) {

throw new BeanCreationException(format("Unable to create bean %s", beanDefinition.getBeanClassName()), e);

}

}

}

@Override

public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

//no need to post process defined bean

}

}

覆盖UndertowEmbeddedServletContainerFactory.class

public class UndertowEmbeddedServletContainerFactory extends AbstractEmbeddedServletContainerFactory implements ResourceLoaderAware, ApplicationContextAware {

private ApplicationContext applicationContext;

@Override

public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {

DeploymentManager manager = createDeploymentManager(initializers);

int port = getPort();

if (port == 0) {

port = SocketUtils.findAvailableTcpPort(40000);

}

Undertow.Builder builder = createBuilder(port);

Map handlers = applicationContext.getBeansWithAnnotation(BlockingHandler.class);

return new UndertowEmbeddedServletContainer(builder, manager, getContextPath(),

port, port >= 0, handlers);

}

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

}

...

覆盖UndertowEmbeddedServletContainer.class

public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager,

String contextPath, int port, boolean autoStart, Map handlers) {

this.builder = builder;

this.manager = manager;

this.contextPath = contextPath;

this.port = port;

this.autoStart = autoStart;

this.handlers = handlers;

}

private Undertow createUndertowServer() {

try {

HttpHandler servletHandler = this.manager.start();

String path = this.contextPath.isEmpty() ? "/" : this.contextPath;

PathHandler pathHandler = Handlers.path().addPrefixPath(path, servletHandler);

for(Entry entry : handlers.entrySet()){

Annotation annotation = entry.getValue().getClass().getDeclaredAnnotation(BlockingHandler.class);

System.out.println(((BlockingHandler) annotation).contextPath());

pathHandler.addPrefixPath(((BlockingHandler) annotation).contextPath(), (HttpHandler) entry.getValue());

}

this.builder.setHandler(pathHandler);

return this.builder.build();

}

catch (ServletException ex) {

throw new EmbeddedServletContainerException(

"Unable to start embdedded Undertow", ex);

}

}

将初始化程序设置为应用程序上下文

public static void main(String[] args) {

new SpringApplicationBuilder(Application.class).initializers(new UndertowInitializer()).run(args);

}

最后创建一个分派给工作线程的HttpHandler

@BlockingHandler(contextPath = "/blocking/test")

public class DatabaseHandler implements HttpHandler {

@Autowired

private EchoService echoService;

@Override

public void handleRequest(HttpServerExchange httpServerExchange) throws Exception {

if(httpServerExchange.isInIoThread()){

httpServerExchange.dispatch();

}

echoService.getMessage("my message");

}

}

如您所见,我的“解决方案”实在太繁重了,我非常感谢您提供的许多简化方面的帮助。

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值