tech.mhuang.pacebox.springboot.autoconfiguration.web.UndertowServerFactoryCustomizer Maven / Gradle / Ivy
package tech.mhuang.pacebox.springboot.autoconfiguration.web;
import io.undertow.connector.ByteBufferPool;
import io.undertow.server.DefaultByteBufferPool;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.Xnio;
import org.xnio.XnioWorker;
import java.io.IOException;
/**
* 设置Undertow服务器 XnioWorker Buffers
*
* 解决启动时警告: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
*
* ...
*
* @author mhuang
* @since 2021.0.5.0
*/
public class UndertowServerFactoryCustomizer implements WebServerFactoryCustomizer {
@Override
public void customize(UndertowServletWebServerFactory factory) {
UndertowDeploymentInfoCustomizer undertowDeploymentInfoCustomizer = deploymentInfo -> {
WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
XnioWorker worker = getXnioWorker();
ByteBufferPool buffers = new DefaultByteBufferPool(Boolean.getBoolean("io.undertow.websockets.direct-buffers"), 1024, 100, 12);
info.setWorker(worker);
info.setBuffers(buffers);
};
factory.addDeploymentInfoCustomizers(undertowDeploymentInfoCustomizer);
}
private XnioWorker getXnioWorker() {
XnioWorker worker = null;
try {
worker = Xnio.getInstance().createWorker(OptionMap.create(Options.THREAD_DAEMON, true));
} catch (IOException ignored) {
}
return worker;
}
}