All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.hmtools.EHCMContext Maven / Gradle / Ivy

There is a newer version: 0.0.2.20180312
Show newest version
package top.hmtools;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import top.hmtools.autoConfiguration.EHCMAutoConfiguration;
import top.hmtools.manager.EhCacheManagerDefault;
import top.hmtools.manager.IEhCacheManager;

/**
 * HTTP Client Manager 上下文
 * @author HyboJ
 *
 */
@Component
public class EHCMContext implements ApplicationContextAware,CommandLineRunner {
	
	private final Logger logger = LoggerFactory.getLogger(EHCMContext.class);
	
	public static ApplicationContext APPLICATIONCONTEXT;
	
	@Autowired
	public  EHCMAutoConfiguration ehcmAutoConfiguration;
	
	/**
	 * 获取管理者
	 */
	public IEhCacheManager getLocalFileMonitorManager(){
		IEhCacheManager result = APPLICATIONCONTEXT.getBean(EhCacheManagerDefault.class);
		try {
			String className = ehcmAutoConfiguration.getEhCacheManagerClassName();
			Class clazz = Class.forName(className);
			result = (IEhCacheManager)APPLICATIONCONTEXT.getBean(clazz);
		} catch (Exception e) {
			this.logger.error("获取IEhCacheManager实例发生异常:"+e.getMessage(),e);
		}
		this.logger.info("当前的IEhCacheManager对象实例是:"+result);
		return result;
	}
	
	@Override
	public void run(String... args) throws Exception {
		this.logger.info("启动。。。");
		this.logger.info("启动完毕");
	}
	

	@PostConstruct
	public void doPostConstruct(){
		this.logger.info("初始化。。。");
		this.logger.info("初始化完毕");
	}
	
	@PreDestroy
	public void doPreDestroy(){
		this.logger.info("销毁开始。。。");
		this.logger.info("销毁完毕");
	}
	
	/**
	 * 执行刷新连接操作
	 */
	public void doRefresh(){
		this.logger.info("刷新开始。。。");
		String[] beanNamesForType = APPLICATIONCONTEXT.getBeanNamesForType(IEhCacheManager.class);
		for(String beanName:beanNamesForType){
			IEhCacheManager ehCacheManager = APPLICATIONCONTEXT.getBean(beanName, IEhCacheManager.class);
			try {
				ehCacheManager.refresh();
			} catch (Exception e) {
				this.logger.info(beanName+"刷新执行失败……");
			}
			this.logger.info(beanName+"刷新执行成功……");
		}
		this.logger.info("刷新完毕");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		EHCMContext.APPLICATIONCONTEXT=applicationContext;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy