下面列出了怎么用org.springframework.boot.context.embedded.EmbeddedServletContainerException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void customize(ConfigurableApplicationContext context) {
if (context instanceof EmbeddedWebApplicationContext
&& !AdminEndpointApplicationRunListener.isEmbeddedServletServer(context.getEnvironment())) {
MetaDataProvider metaDataProvider = context.getBean(MetaDataProvider.class);
EmbeddedServletContainer embeddedServletContainer = new EmbeddedServletContainer() {
@Override
public void start() throws EmbeddedServletContainerException {
}
@Override
public void stop() throws EmbeddedServletContainerException {
}
@Override
public int getPort() {
return metaDataProvider.getServerPort();
}
};
context.publishEvent(new EmbeddedServletContainerInitializedEvent((EmbeddedWebApplicationContext) context, embeddedServletContainer));
}
}
@Override
public void stop() throws EmbeddedServletContainerException {
try {
if (null != bossGroup) {
bossGroup.shutdownGracefully().await();
}
if (null != workerGroup) {
workerGroup.shutdownGracefully().await();
}
if (null != servletExecutor) {
servletExecutor.shutdownGracefully().await();
}
} catch (InterruptedException e) {
throw new EmbeddedServletContainerException("Container stop interrupted", e);
}
}
/**
* 优雅地关闭各种资源
* @throws EmbeddedServletContainerException
*/
@Override
public void stop() throws EmbeddedServletContainerException {
log.info("Embedded Netty Servlet Container(by Leibniz.Hu) is now shuting down.");
try {
if (null != bossGroup) {
bossGroup.shutdownGracefully().await();
}
if (null != workerGroup) {
workerGroup.shutdownGracefully().await();
}
if (null != servletExecutor) {
servletExecutor.shutdownGracefully().await();
}
} catch (InterruptedException e) {
throw new EmbeddedServletContainerException("Container stop interrupted", e);
}
}
@Override
public void start() throws EmbeddedServletContainerException {
ServerBootstrap b = new ServerBootstrap();
groups(b);
servletExecutor = new DefaultEventExecutorGroup(50);
b.childHandler(new NettyEmbeddedServletInitializer(servletExecutor, context));
// Don't yet need the complexity of lifecycle state, listeners etc, so tell the context it's initialised here
context.setInitialised(true);
ChannelFuture future = b.bind(address).awaitUninterruptibly();
//noinspection ThrowableResultOfMethodCallIgnored
Throwable cause = future.cause();
if (null != cause) {
throw new EmbeddedServletContainerException("Could not start Netty server", cause);
}
logger.info(context.getServerInfo() + " started on port: " + getPort());
}
@Override
public void stop() throws EmbeddedServletContainerException {
try {
if (null != bossGroup) {
bossGroup.shutdownGracefully().await();
}
if (null != workerGroup) {
workerGroup.shutdownGracefully().await();
}
if (null != servletExecutor) {
servletExecutor.shutdownGracefully().await();
}
} catch (InterruptedException e) {
throw new EmbeddedServletContainerException("Container stop interrupted", e);
}
}
@Override
public void start() throws EmbeddedServletContainerException {
for (ServletContextInitializer i : initializers) {
try {
if (handler.getServletContext() == null) {
throw new EmbeddedServletContainerException("Attempting to initialize ServletEmbeddedWebServer without ServletContext in Handler", null);
}
i.onStartup(handler.getServletContext());
} catch (ServletException e) {
throw new EmbeddedServletContainerException("Could not initialize Servlets", e);
}
}
}
@Test
public void start_throwsException() {
ServerlessServletEmbeddedServerFactory factory = new ServerlessServletEmbeddedServerFactory();
TestServlet initializer = new TestServlet(true);
EmbeddedServletContainer container = factory.getEmbeddedServletContainer(initializer);
try {
container.start();
} catch (EmbeddedServletContainerException e) {
assertTrue(ServletException.class.isAssignableFrom(e.getCause().getClass()));
assertEquals(TestServlet.EXCEPTION_MESSAGE, e.getCause().getMessage());
return;
}
fail("Did not throw the expected exception");
}
@Override
public void start() throws EmbeddedServletContainerException {
}
@Override
public void stop() throws EmbeddedServletContainerException {
}