1.Spring项目启动时,加载相关初始化配置
Spring项目启动时,会加载一些常用的配置:
1、加载spring上下文
SpringApplicationContextUtils.initApplicationContext(event.getServletContext());
2、加载属性文件
EsbCommsUtils.initComms(event.getServletContext());
public class EsbCommsUtils { private static Log logger = LogFactory.getLog(EsbCommsUtils.class); public static final Properties properties = new Properties(); public static void initComms(ServletContext sct){
try{
properties.load(sct.getResourceAsStream("/WEB-INF/conf/comms.properties"));
}catch(Exception e){
logger.error("加载comms.properties文件异常,cause:"+e.getMessage());
}
} public static String getCommsValue(String key){
return properties.getProperty(key, null);
} }
3、加载本地緩存,定时轮询刷新(定义定时线程池,1个线程)
cacheManager = (CacheManager)SpringApplicationContextUtils.getBean(EsbUtils.CACHEMANAGER);
cacheManager.loadAllCache();
package com.zat.mesb.base; import com.zat.mesb.util.EsbCommsUtils;
import com.zat.sproxy.thread.NamedThreadFactory;
import org.springframework.stereotype.Controller; import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; @Controller
public class CacheManager { private List<AbstractCache> listCaches;
// 定义定时线程池,1个线程
18 private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1, new NamedThreadFactory("reloadcache", true)); public List<AbstractCache> getListCaches() {
return listCaches;
} public void setListCaches(List<AbstractCache> listCaches) {
this.listCaches = listCaches;
}
// 定时查询参数
28 public void loadAllCache() {
29 //loadCache();
30 this.scheduled.scheduleWithFixedDelay(new Runnable() {
31
32 @Override
33 public void run() {
34 loadCache();
35 }
36 }, 1L, Long.valueOf(EsbCommsUtils.getCommsValue("flush.cache.data")), TimeUnit.SECONDS);
37 } private void loadCache() {
if(this.listCaches != null){
for(AbstractCache cache : listCaches) {
cache.loadCache();
}
}
} public Object getCacheBySimpleClassName(String className){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){
return cache.getCacheList();
}
}
}
return null;
} public Object getCacheValueByKey(String className, String key){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){
return cache.cacheMaps.get(key);
}
}
}
return null;
} public Object getCacheValueByKey(String className, String key, String type){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){
return cache.getCacheMaps().get(key);
}
}
}
return null;
} public void clearCache(){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
cache.clearCache();
}
}
} }
完整示例代码:
package com.zat.mesb.listener; import com.zat.mesb.base.CacheManager;
import com.zat.mesb.stage.processor.StageProcessorManager;
import com.zat.mesb.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.ContextLoaderListener; import javax.servlet.ServletContextEvent; public class EsbListener extends ContextLoaderListener { private static Log logger = LogFactory.getLog(EsbListener.class);
private CacheManager cacheManager; @Override
public void contextDestroyed(ServletContextEvent sce) {
super.contextDestroyed(sce);
} @Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
logger.info("1.开始加载spring上下文...");
SpringApplicationContextUtils.initApplicationContext(event.getServletContext());
EsbCommsUtils.initComms(event.getServletContext());
logger.info("1.加载spring上下文完成..."); logger.info("2.开始加载本地緩存...");
cacheManager = (CacheManager)SpringApplicationContextUtils.getBean(EsbUtils.CACHEMANAGER);
cacheManager.loadAllCache();
logger.info("2.加载本地緩存完成..."); logger.info("3.开始加載BusHandlers配置信息...");
BusHandlerUtils.initBusHandlers(event.getServletContext());
logger.info("3.加載BusHandlers配置信息完成..."); logger.info("4.开始加載ApiHandlers配置信息...");
ApiHandlerUtils.initApiHandlers(event.getServletContext());
logger.info("4.加載ApiHandlers配置信息完成..."); logger.info("5.开始加載ApiAlipayHandlers配置信息...");
ApiAlipayHandlerUtils.initApiAlipayHandlers(event.getServletContext());
logger.info("5.加載ApiAlipayHandlers配置信息完成..."); logger.info("6.開始初始化業務階段流程...");
Thread thread = StageProcessorManager.getInstance();
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
if(thread != null && thread.isAlive()){
try {
thread.join();
} catch (InterruptedException e) {
logger.error("Init stage process error,cause:"+e.getMessage());
}
}
logger.info("6.初始化業務階段流程完成...");
} }
1.Spring项目启动时,加载相关初始化配置的更多相关文章
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息. 因为它实现了ServletContextListener这个接口,在web.xml配置这个监听 ...
- ElasticSearch 启动时加载 Analyzer 源码分析
ElasticSearch 启动时加载 Analyzer 源码分析 本文介绍 ElasticSearch启动时如何创建.加载Analyzer,主要的参考资料是Lucene中关于Analyzer官方文档 ...
- 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...
- Servlet在启动时加载的tomcat源码(原创)
tomcat 8.0.36 知识点: 通过配置loadOnStartup可以设置Servlet是否在Tomcat启动时加载,以及按值大小进行有序加载,其最小有效值为0,最大有效值为Integer.MA ...
- web.xml中配置启动时加载的servlet,load-on-starup
web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...
- 依赖Spring的情况下,Java Web项目如何在启动时加载数据库中的数据?
原文:https://blog.csdn.net/u012345283/article/details/39558537 原文:https://blog.csdn.net/wandrong/artic ...
- stark组件前戏之项目启动前加载指定文件
1. django项目启动时, 自定制执行某个py文件 dajngo 启动时.会将所有 路由加载到内存中. 我的目的就是在 路由加载之前,执行某个py文件. 每个app中都有一个 apps.py fr ...
- 原生servlet项目启动自动加载一个方法
web.xml里的配置: 配置好要加载的类,其中1这一句是项目启动时自动加载该类的必要条件. <servlet> <servlet-name>SharePltfCLServle ...
- stark组件前戏(1)之项目启动前加载指定文件
django项目启动时,可以自定义执行某个py文件,这需要在任意app的apps.py中的Config类定义ready方法,并调用. from django.apps import AppConf ...
随机推荐
- z-index无效失效的解决
解决办法: 父级元素加上position:relative;并设置z-index父级元素的z-index优先,其中包含的元素的z-index是相对于父级元素的index <div style=& ...
- Ubuntu18.04安装UHD+GNU Radio后找不到USRP B210解决办法
一.在终端中输入uhd_usrp_probe,提示USB错误,没有权限. 解决办法: 输入 : sudo uhd_usrp_probe 二.GNU Radio中出现找不到设备,地址为空的错误: 错误原 ...
- 小程序+tgit
1.微信公众平台-设置-开发者工具 开通 腾讯云和tgit权限管理 如果遇到问题 ..用户二次验证什么的 直接去 腾讯云-安全设置 将“敏感操作”和“异地登陆” 中的保护去掉 2.微信公众平台- ...
- swoole table
swoole_table #在内存中建立一张表,用来存放进程交互过程中使用的数据,与memocache似有异曲同工之妙#用法 <?php$table = new swoole_table(204 ...
- qt一些函数
sleep(n); 休息ns msleep(1000); 休息1000毫秒 usleep(1000000); //休息n微妙
- MessagePack Java Jackson Dataformat - 安装
中文标题[MessagePack 的 Jackson 数据格式] 本页面中的所有示例程序,请到代码库 https://github.com/cwiki-us-demo/serialize-deseri ...
- POJ - 3162 Walking Race 树形dp 单调队列
POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...
- 深度理解链式前向星——转载自ACdreamer
转载自ACdreamer [转载]深度理解链式前向星 我们首先来看一下什么是前向星. 前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序 ...
- MySQL索引(二)B+树在磁盘中的存储
MySQL索引(二)B+树在磁盘中的存储 回顾  上一篇文章<MySQL索引为什么要用B+树>讲了MySQL为什么选择用B+树来作为底层存储结构,提了两个知识点: B+树索引并不能直接找 ...
- 使用wait/notify实现生产消费模型
public class A { private Deque<Integer> list = new LinkedList<>(); private int max = 10; ...