zoukankan      html  css  js  c++  java
  • 1.Spring项目启动时,加载相关初始化配置

    Spring项目启动时,会加载一些常用的配置:

    1、加载spring上下文

    SpringApplicationContextUtils.initApplicationContext(event.getServletContext());

    2、加载属性文件

    EsbCommsUtils.initComms(event.getServletContext());
     1 public class EsbCommsUtils {
     2     
     3     private static Log logger = LogFactory.getLog(EsbCommsUtils.class);
     4     
     5     public static final Properties properties = new Properties();
     6         
     7     public static void initComms(ServletContext sct){
     8         try{
     9             properties.load(sct.getResourceAsStream("/WEB-INF/conf/comms.properties"));
    10         }catch(Exception e){
    11             logger.error("加载comms.properties文件异常,cause:"+e.getMessage());
    12         }
    13     }
    14     
    15     public static String getCommsValue(String key){
    16         return properties.getProperty(key, null);
    17     }
    18     
    19 }

    3、加载本地緩存,定时轮询刷新(定义定时线程池,1个线程)

    cacheManager = (CacheManager)SpringApplicationContextUtils.getBean(EsbUtils.CACHEMANAGER);
    cacheManager.loadAllCache();
     1 package com.zat.mesb.base;
     2 
     3 import com.zat.mesb.util.EsbCommsUtils;
     4 import com.zat.sproxy.thread.NamedThreadFactory;
     5 import org.springframework.stereotype.Controller;
     6 
     7 import java.util.List;
     8 import java.util.concurrent.Executors;
     9 import java.util.concurrent.ScheduledExecutorService;
    10 import java.util.concurrent.TimeUnit;
    11 
    12 
    13 @Controller
    14 public class CacheManager {
    15     
    16     private List<AbstractCache> listCaches;
    17     
    // 定义定时线程池,1个线程 18 private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1, new NamedThreadFactory("reloadcache", true)); 19 20 public List<AbstractCache> getListCaches() { 21 return listCaches; 22 } 23 24 public void setListCaches(List<AbstractCache> listCaches) { 25 this.listCaches = listCaches; 26 } 27
        // 定时查询参数 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 } 38 39 private void loadCache() { 40 if(this.listCaches != null){ 41 for(AbstractCache cache : listCaches) { 42 cache.loadCache(); 43 } 44 } 45 } 46 47 public Object getCacheBySimpleClassName(String className){ 48 if(this.listCaches != null){ 49 for(AbstractCache cache : listCaches){ 50 if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){ 51 return cache.getCacheList(); 52 } 53 } 54 } 55 return null; 56 } 57 58 public Object getCacheValueByKey(String className, String key){ 59 if(this.listCaches != null){ 60 for(AbstractCache cache : listCaches){ 61 if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){ 62 return cache.cacheMaps.get(key); 63 } 64 } 65 } 66 return null; 67 } 68 69 public Object getCacheValueByKey(String className, String key, String type){ 70 if(this.listCaches != null){ 71 for(AbstractCache cache : listCaches){ 72 if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){ 73 return cache.getCacheMaps().get(key); 74 } 75 } 76 } 77 return null; 78 } 79 80 public void clearCache(){ 81 if(this.listCaches != null){ 82 for(AbstractCache cache : listCaches){ 83 cache.clearCache(); 84 } 85 } 86 } 87 88 }

    完整示例代码:

     1 package com.zat.mesb.listener;
     2 
     3 
     4 import com.zat.mesb.base.CacheManager;
     5 import com.zat.mesb.stage.processor.StageProcessorManager;
     6 import com.zat.mesb.util.*;
     7 import org.apache.commons.logging.Log;
     8 import org.apache.commons.logging.LogFactory;
     9 import org.springframework.web.context.ContextLoaderListener;
    10 
    11 import javax.servlet.ServletContextEvent;
    12 
    13 public class EsbListener extends ContextLoaderListener {
    14     
    15     private static Log logger = LogFactory.getLog(EsbListener.class);
    16     private CacheManager cacheManager;
    17 
    18     @Override
    19     public void contextDestroyed(ServletContextEvent sce) {
    20         super.contextDestroyed(sce);
    21     }
    22 
    23     @Override
    24     public void contextInitialized(ServletContextEvent event) {
    25         super.contextInitialized(event);
    26         logger.info("1.开始加载spring上下文...");
    27         SpringApplicationContextUtils.initApplicationContext(event.getServletContext());
    28         EsbCommsUtils.initComms(event.getServletContext());
    29         logger.info("1.加载spring上下文完成...");    
    30 
    31         logger.info("2.开始加载本地緩存...");
    32         cacheManager = (CacheManager)SpringApplicationContextUtils.getBean(EsbUtils.CACHEMANAGER);
    33         cacheManager.loadAllCache();
    34         logger.info("2.加载本地緩存完成...");
    35         
    36         logger.info("3.开始加載BusHandlers配置信息...");
    37         BusHandlerUtils.initBusHandlers(event.getServletContext());
    38         logger.info("3.加載BusHandlers配置信息完成...");
    39 
    40         logger.info("4.开始加載ApiHandlers配置信息...");
    41         ApiHandlerUtils.initApiHandlers(event.getServletContext());
    42         logger.info("4.加載ApiHandlers配置信息完成...");
    43 
    44         logger.info("5.开始加載ApiAlipayHandlers配置信息...");
    45         ApiAlipayHandlerUtils.initApiAlipayHandlers(event.getServletContext());
    46         logger.info("5.加載ApiAlipayHandlers配置信息完成...");
    47 
    48         logger.info("6.開始初始化業務階段流程...");
    49         Thread thread = StageProcessorManager.getInstance();
    50         thread.setPriority(Thread.MAX_PRIORITY);
    51         thread.start();
    52         if(thread != null && thread.isAlive()){
    53             try {
    54                 thread.join();
    55             } catch (InterruptedException e) {
    56                 logger.error("Init stage process error,cause:"+e.getMessage());
    57             }
    58         }
    59         logger.info("6.初始化業務階段流程完成...");
    60     }
    61 
    62 }
  • 相关阅读:
    Windows2008R2安装DNS和SQLServer200r2服务 (9.18第七天)
    Windows2008R2安装iis和iis下搭建web服务器(9.18 第七天)
    Ubuntu 安装phpmyadmin (9.17第六天)
    Ubuntu Navicat链接mysql (9.17第六天)
    Spring之AOP由浅入深
    oracle并行模式(Parallel)
    转:Java后端面试自我学习
    Spring Security 简介
    spring boot入门
    git--分布式版本管理系统
  • 原文地址:https://www.cnblogs.com/caoweixiong/p/10886333.html
Copyright © 2011-2022 走看看