Commit 149ffdd9 authored by ag's avatar ag

feat : jetty support

parent e652aa0b
......@@ -58,10 +58,16 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<artifactId>spring-boot-starter-jetty</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<!--<optional>true</optional>-->
</dependency>
</dependencies>
<!-- 部署时往公司私服上发包, 可以在网址上找到 其他项目引入该包的需要的 XML -->
<distributionManagement>
......
......@@ -8,8 +8,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.Lifecycle;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
@Slf4j
@Component
public class BaseDestroyHandler {
......@@ -17,18 +15,22 @@ public class BaseDestroyHandler {
@Autowired
private ApplicationContext applicationContext;
@PreDestroy
//todo test
// @PreDestroy
private void stopRedisSub() {
// context.getBean(RedisMessageListenerContainer.)
log.info("redis stopped");
}
@PreDestroy
//todo test
// @PreDestroy
private void stopRabbitMQ() {
RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry = applicationContext.getBean(
RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME,
RabbitListenerEndpointRegistry.class);
rabbitListenerEndpointRegistry.getListenerContainers().forEach(Lifecycle::stop);
try {
RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry = applicationContext.getBean(
RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME,
RabbitListenerEndpointRegistry.class);
rabbitListenerEndpointRegistry.getListenerContainers().forEach(Lifecycle::stop);
} catch (Exception e) {
}
log.info("MQ listener stopped");
}
}
package cn.quantgroup.tech.shutdown.configuration;
import cn.quantgroup.tech.shutdown.properties.GracefulShutdownProperties;
import cn.quantgroup.tech.shutdown.service.JettyShutdown;
import cn.quantgroup.tech.shutdown.service.TomcatShutdown;
import cn.quantgroup.tech.shutdown.service.UndertowShutdown;
import cn.quantgroup.tech.shutdown.wrapper.UndertowShutdownHandlerWrapper;
import io.undertow.Undertow;
import org.apache.catalina.startup.Tomcat;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
......@@ -20,6 +28,7 @@ import org.springframework.context.annotation.Import;
import javax.servlet.Servlet;
/**
* This configuration class will be picked up by Spring Boot's auto configuration capabilities as soon as it's
* on the classpath.
......@@ -35,6 +44,7 @@ public class GracefulShutdownAutoConfiguration {
*/
@Configuration
@ConditionalOnClass({Servlet.class, Tomcat.class})
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {
@Bean
......@@ -57,11 +67,41 @@ public class GracefulShutdownAutoConfiguration {
}
}
@Configuration
@ConditionalOnClass({Servlet.class, Server.class, Loader.class,
WebAppContext.class})
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedJetty {
@Bean
public JettyShutdown jettyShutdown() {
return new JettyShutdown();
}
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
return new JettyEmbeddedServletContainerFactory();
}
@Bean
public EmbeddedServletContainerCustomizer jettyCustomizer() {
return container -> {
if (container instanceof JettyEmbeddedServletContainerFactory) {
((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(jettyShutdown());
}
};
}
}
/**
* Configuration for Undertow.
*/
@Configuration
@ConditionalOnClass({Servlet.class, Undertow.class})
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedUndertow {
@Bean
......
package cn.quantgroup.tech.shutdown.service;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
/**
* @author ag
*/
@Slf4j
public class JettyShutdown implements Shutdown, JettyServerCustomizer {
private volatile Server server;
@Override
public void shutdown(Integer delay) throws InterruptedException {
StatisticsHandler handler = new StatisticsHandler();
handler.setHandler(server.getHandler());
server.setHandler(handler);
server.setStopTimeout(delay);
//todo stop at shutdown
server.setStopAtShutdown(true);
}
@Override
public void customize(Server server) {
this.server = server;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment