JavaWeb项目启动时,自动执行代码的三种方式

来源:互联网 发布:医疗软件搜索下载 编辑:程序博客网 时间:2024/05/31 18:31

JavaWeb项目启动时,自动执行代码的三种方式(包含不占用tomcat启动时长的方式)

现有三种方式可以实现在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   @Override4   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方法被执行两次。为了避免这个问题,我们可以只在root10       // application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理。11       if (event.getApplicationContext().getParent() == null) {12         // 需要实现的功能13       }14     } catch (Exception e) {15       LOGGER.error("StartGateServiceData", e);16     }17   }18 }
复制代码
阅读全文
0 0