SpringBoot新增监听器Listener
什么是web监听器?
web监听器是一种Servlet中的特殊的类,它们能帮助开发者监听web中的特定事件,比如ServletContext,HttpSession,ServletRequest的创建和销毁;变量的创建、销毁和修改等。可以在某些动作前后增加处理,实现监控。
监听器常用的用途
通常使用Web监听器做以下的内容:
统计在线人数,利用HttpSessionLisener
加载初始化信息:利用ServletContextListener
统计网站访问量
实现访问监控
1.编写自定义监听器BackendConfigListener继承自GenericApplicationListener
package com.shitou.deposit.listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.logging.LoggingApplicationListener; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.GenericApplicationListener; import org.springframework.core.ResolvableType; import com.alibaba.fastjson.JSONObject; import com.shitou.deposit.domain.entity.TransactionTradingChannelConfig; import com.shitou.deposit.domain.impl.TransactionTradingChannelConfigBizFacadeImpl; /** * 应用配置Spring容器监听器 * 当容器启动时,当环境准备好时,加载远程的配置信息 * @author zhouky * @since 2018年1月11日 */ public class BackendConfigListener implements GenericApplicationListener { /** * 日志 */ private static Logger logger = LoggerFactory.getLogger(BackendConfigListener.class); //指定要监听的时间 private static Class<?>[] EVENT_TYPES = { ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class, ApplicationReadyEvent.class }; private static Class<?>[] SOURCE_TYPES = { SpringApplication.class, ApplicationContext.class }; private TransactionTradingChannelConfigBizFacadeImpl transactionTradingChannelConfigBizFacadeImpl = null; /** * 程序启动时加载应用远程配置 * * @param applicationEvent */ @Override public void onApplicationEvent(ApplicationEvent applicationEvent) { if (applicationEvent instanceof ApplicationReadyEvent) { //获取ApplicationReadyEvent中applicationContext ConfigurableApplicationContext applicationContext = ((ApplicationReadyEvent) applicationEvent).getApplicationContext(); //applicationContext获取Bean transactionTradingChannelConfigBizFacadeImpl = (TransactionTradingChannelConfigBizFacadeImpl) applicationContext.getBean("transactionTradingChannelConfigBizFacadeImpl"); TransactionTradingChannelConfig transactionTradingChannelConfig = transactionTradingChannelConfigBizFacadeImpl.selectByPrimaryKey(1); logger.info(JSONObject.toJSONString(transactionTradingChannelConfig)); logger.info("-------------------- ApplicationReadyEvent ----------------------"); } } @Override public int getOrder() { // 设置加载优先级,在日志之前加载 return LoggingApplicationListener.DEFAULT_ORDER - 1; } @Override public boolean supportsEventType(ResolvableType resolvableType) { return isAssignableFrom(resolvableType.getRawClass(), EVENT_TYPES); } @Override public boolean supportsSourceType(Class<?> sourceType) { return isAssignableFrom(sourceType, SOURCE_TYPES); } private boolean isAssignableFrom(Class<?> type, Class<?>... supportedTypes) { if (type != null) { for (Class<?> supportedType : supportedTypes) { if (supportedType.isAssignableFrom(type)) { return true; } } } return false; } }
2.Application启动类添加自定义监听器
package com.shitou; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.properties.ConfigurationProperties; import com.shitou.common.utilities.ShutDownBlockHook; import com.shitou.common.utilities.ShutDownHookLevel; import com.shitou.config.ConfigApplicationListener; import com.shitou.deposit.listener.BackendConfigListener; @ConfigurationProperties("server") @SpringBootApplication public class Application implements EmbeddedServletContainerCustomizer { private static Logger logger = LoggerFactory.getLogger(Application.class); private int port = 8080; public void setPort(int port) { this.port = port; } public static void main(String[] args) throws InterruptedException { SpringApplication app = new SpringApplication(Application.class); // 加载remote config app.addListeners(new ConfigApplicationListener()); // 加载database config app.addListeners(new BackendConfigListener()); app.run(args); ShutDownBlockHook.registerHook(new ShutDownBlockHook.ShutDownRunning(){ @Override public void run() { logger.info("Application ShutDownBlockHook ............."); } }, ShutDownHookLevel.LoggingHookLevel); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(this.port); } }