现有三种方式可以实现在tomcat启动时执行某段代码(三种实现方式见1.2.3.)
由于这三种方式的执行时长计算在tomcat的启动时长里,如果tomcat设置了启动超时时间,那么这三种方式执行的操作很可能会让tomcat启动超时。
为了解决自动执行的部分不影响tomcat的正常启动我们可以在三种方式中新建一个线程,将需要操作的部分交给子线程去做。
我们可以取三种方式的任意一种,新建一个线程:
public class GreyStartServlet extends HttpServlet { @Override public void init() throws ServletException { MyThread read = new MyThread(); // 使用另一个线程来执行该方法,会避免占用Tomcat的启动时间 new Thread(thread).start(); } } class MyThread implements Runnable { // Tomcat启动结束后执行 @Override public void run() { // 子线程需要做的事情 } }
1.ServletContextListener
web.xml配置
<listener>
<listener-class>com.yuan.framework.GreyClientInitListener</listener-class> </listener>
1 public class GreyClientInitListener implements ServletContextListener { 2 private static final Logger LOGGER = LoggerFactory.getLogger(GreyClientInitListener.class); 3 public GreyClientInitListener() {} 4 public void contextDestroyed(ServletContextEvent arg0) {} 5 public void contextInitialized(ServletContextEvent arg0) { 6 try { 7 // 需要实现的功能 8 } catch (Exception e) { 9 LOGGER.error("GreyClientInitListener error", e); 10 } 11 } 12 }
2.HttpServlet
web.xml配置
<servlet>
<servlet-name>event-collector</servlet-name> <servlet-class>com.yuan.framework.GreyStartServlet</servlet-class> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>event-collector</servlet-name> <url-pattern>/event-collect</url-pattern> </servlet-mapping>
1 public class GreyStartServlet extends HttpServlet { 2 // Servlet的init方法会在Tomcat启动的时候执行 3 @Override 4 public void init() throws ServletException { 5 // 需要实现的功能 6 } 7 }
3.spring ApplicationListener
1 @Service 2 public class StartGateServiceData implements ApplicationListener<ContextRefreshedEvent> { 3 private static final Log LOGGER = LogFactory.getLog(StartGateServiceData.class); 4 @Override 5 public void onApplicationEvent(ContextRefreshedEvent event) { 6 try { 7 // 在web项目中(spring mvc),系统会存在两个容器,一个是root application context 8 // ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。 9 // 这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免这个问题,我们可以只在root 10 // application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理。 11 if (event.getApplicationContext().getParent() == null) { 12 // 需要实现的功能 13 } 14 } catch (Exception e) { 15 LOGGER.error("StartGateServiceData", e); 16 } 17 } 18 }