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

三种方式实现在tomcat启动时执行某段代码

由于这三种方式的执行时长计算在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>

 

  public class GreyClientInitListener implements ServletContextListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(GreyClientInitListener.class);
    public GreyClientInitListener() {}
   public void contextDestroyed(ServletContextEvent arg0) {}
    public void contextInitialized(ServletContextEvent arg0) {
      try {
        // 需要实现的功能
      } catch (Exception e) {
       LOGGER.error("GreyClientInitListener error", e);
     }
   }
 }

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>
 public class GreyStartServlet extends HttpServlet {
   // Servlet的init方法会在Tomcat启动的时候执行
   @Override
   public void init() throws ServletException {
   // 需要实现的功能
   }
 }    

3.spring ApplicationListener

 


package com.light.ac.service;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

@Service
public class StartGateServiceData implements ApplicationListener<ContextRefreshedEvent> {
    private static final Log LOGGER = LogFactory.getLog(StartGateServiceData.class);

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            // 在web项目中(spring mvc),系统会存在两个容器,一个是root application context
            // ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。
            // 这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免这个问题,我们可以只在root
            // application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理。
            if (event.getApplicationContext().getParent() == null) {
                // 需要实现的功能
                System.out.println("初始化");
            }
        } catch (Exception e) {
            LOGGER.error("StartGateServiceData", e);
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值