JavaWeb项目启动时,自动执行代码的三种方式(包含不占用tomcat启动时长的方式)
2016-03-15 22:01
671 查看
现有三种方式可以实现在tomcat启动时执行某段代码(三种实现方式见1.2.3.)
由于这三种方式的执行时长计算在tomcat的启动时长里,如果tomcat设置了启动超时时间,那么这三种方式执行的操作很可能会让tomcat启动超时。
为了解决自动执行的部分不影响tomcat的正常启动我们可以在三种方式中新建一个线程,将需要操作的部分交给子线程去做。
我们可以取三种方式的任意一种,新建一个线程:
1.ServletContextListener
2.HttpServlet
3.spring ApplicationListener
由于这三种方式的执行时长计算在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
@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) { // 需要实现的功能 } } catch (Exception e) { LOGGER.error("StartGateServiceData", e); } } }
相关文章推荐
- eclipse中导入web项目变成java项目解决办法and dynamic web module和对应的TOMCAT 版本
- 启动程序端口被占用Address already in use: bind解决方案
- linux下安装tomcat
- Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!
- Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!
- 浅谈WebLogic和Tomcat
- 配置tomcat manager【Tomcat Manager用户配置详解】【Tomcat的Manager显示403 Access Denied】
- Nginx + Tomcat 动静分离实现负载均衡(转)
- Linux 安装jdk、tomcat,maven
- Tomcat源码解析(五):Connector连接器的初始化和启动流程
- mac下安装tomcat
- Linux下启动启动tomcat 服务器报错 The file is absent or does not have execute permission
- MyEclipse下Tomcat启动变慢 解决方法
- Tomcat虚拟目录配置
- Tomcat实现Session对象的持久化原理及配置方法介绍
- maven tomcat7:deploy
- java与tomcat类加载机制
- tomcat log 配置解决catalina.out文件过大问题
- tomcat catalina.out 日志分割
- tomcat虚拟目录配置方法