@Override public void contextDestroyed(ServletContextEvent sce) { logger.info("destroyed DefaultContextListener."); try { // 关闭线程池 AppExecutors.getScheduledExecutor().shutdown(); AppExecutors.getTaskPoolExecutor().shutdown(); if (AppExecutors.getScheduledExecutor().awaitTermination(5, TimeUnit.SECONDS)) { logger.info("success shutdown scheduled pool excutor."); } else { logger.error("failed shutdown scheduled pool excutor."); AppExecutors.getScheduledExecutor().shutdownNow(); } if (AppExecutors.getTaskPoolExecutor().awaitTermination(5, TimeUnit.SECONDS)) { logger.info("success shutdown task pool excutor."); } else { logger.error("failed shutdown task pool excutor."); AppExecutors.getTaskPoolExecutor().shutdownNow(); } } catch (Exception e) { if (!AppExecutors.getTaskPoolExecutor().isShutdown()) { AppExecutors.getTaskPoolExecutor().shutdownNow(); } if (!AppExecutors.getScheduledExecutor().isShutdown()) { AppExecutors.getScheduledExecutor().shutdownNow(); } logger.error("catch exception when destroyed DefaultContextListener."); e.printStackTrace(); } finally { // 关闭阿里云服务客户端 SystemConfig.getOtsTagClient().shutdown(); SystemConfig.getOtsSharedClient().shutdown(); SystemConfig.shutDownOSSClient(); SystemConfig.closeMnsClient(); RedisPool.closeAll(); RedisLogAppender.closeAll(); WechatBasicHelper.destroy(); JerseyClientManager.destroy(); } }
/** 获取freemarker模板加载路径 */ private MultiTemplateLoader getTemplateLoader(ServletContextEvent sce) throws IOException { RemoteTemplateLoader remoteLoader = new RemoteTemplateLoader(SystemConfig.getTagQueryViewTemplate()); String rootPath = sce.getServletContext().getRealPath("/"); FileTemplateLoader localLoader = new FileTemplateLoader(new File(rootPath + "/tpl/")); TemplateLoader[] loaders = new TemplateLoader[] {remoteLoader, localLoader}; return new MultiTemplateLoader(loaders); }
@Override public void contextInitialized(ServletContextEvent sce) { try { SystemConfig.initParam(); TagOperationHelper.setOtsClient(SystemConfig.getOtsTagClient()); OtsUserHelper.setOtsClient(SystemConfig.getOtsSharedClient()); UserOperationHelper.setOtsClient(SystemConfig.getOtsSharedClient()); // 初始化Redis连接池 RedisPool.initRedisPool(); RedisLogAppender.init(); WechatConfig.initParam(); PublicAccountFactory.initConfig(); TemplateFactory.initConfig(); // 初始化freemarker initFreeMarker(sce); GeoLocationHelper.init(); // 同步微信access token 定时任务 AppExecutors.getScheduledExecutor() .scheduleAtFixedRate( new RefreshAccessTokenTask(), getRefreshWechatTokenScheduledInitDelay(), WechatConstants.refreshTokenTaskPeriod, TimeUnit.MILLISECONDS); // 同步活动定时任务 AppExecutors.getScheduledExecutor() .scheduleAtFixedRate(new SyncMessageThread(), 5, 60, TimeUnit.SECONDS); // 处理标签和用户锁线程 AppExecutors.getScheduledExecutor() .scheduleAtFixedRate(new EtraceSelfMessageThread(), 10, 60, TimeUnit.SECONDS); } catch (Throwable e) { e.printStackTrace(); } }