使用quartz实现不重启服务器修改自定义配置
为了方便维护系统,开发中通常会设置一些自定义参数,写在单独的配置文件里,需要调整时可直接登录服务器修复配置文件,而不需要修改程序。但尴尬的是,web服务器并不会自动重新加载配置文件,重启服务器又会中断服务。本文是一个使用quartz解决此问题的示例。
项目结构:
其中,system.properties是自定义配置文件,内容只有一行:
- debug=false
这个定义了一个debug参数,切换true/false可以控制一些运行状态。
然后通过编写类来读取属性
- package com.autfish;
- import org.apache.commons.lang3.BooleanUtils;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import org.springframework.util.ResourceUtils;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.util.Properties;
- /**
- * Created by autfish on 2017/1/17.
- */
- public class SystemConfig {
- private static Logger logger = LogManager.getLogger("system");
- private static Properties props = new Properties();
- private File configFile = null;
- public SystemConfig(String filePath) {
- try {
- this.configFile = ResourceUtils.getFile(filePath);
- this.refresh();
- } catch (FileNotFoundException e) {
- logger.error(e);
- logger.warn("file [" + filePath + "] not found!");
- this.configFile = null;
- }
- }
- public void refresh() {
- if (this.configFile != null)
- SystemConfig.loadConfig(this.configFile);
- else
- logger.warn("load configuration file failed!");
- }
- private static void loadConfig(File configFile) {
- try (FileInputStream fis = new FileInputStream(configFile) {
- @Override
- public void close() { }
- }) {
- if (props.isEmpty()) {
- props.load(fis);
- } else {
- Properties temp = new Properties();
- temp.load(fis);
- for (Object key : temp.keySet()) {
- String value = temp.getProperty(key.toString());
- if (props.getProperty(key.toString()) != null && !props.getProperty(key.toString()).equals(value)) {
- props.setProperty(key.toString(), value);
- logger.info("property changed: " + key.toString() + "=" + value);
- }
- }
- }
- } catch (Exception e) {
- logger.error(e);
- }
- }
- public static boolean isDebug() {
- return BooleanUtils.toBoolean(props.getProperty("debug"));
- }
- }
在项目中可通过SystemConfig.isDebug()方法读取当前配置。
下面设置quartz,定时调用refresh()方法重新加载修改的配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <!-- 线程执行器配置,用于任务注册 -->
- <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
- <property name="corePoolSize" value="10"/>
- <property name="maxPoolSize" value="100"/>
- <property name="queueCapacity" value="500"/>
- </bean>
- <!-- 设置调度 -->
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
- <property name="triggers">
- <list>
- <ref bean="refreshConfigCronTrigger"/>
- </list>
- </property>
- <property name="taskExecutor" ref="executor"/>
- </bean>
- <bean id="systemConfig" class="com.autfish.SystemConfig" >
- <constructor-arg value="classpath:system.properties"/>
- </bean>
- <bean id="refreshConfigJobDetail"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="systemConfig"/>
- <property name="targetMethod" value="refresh"/>
- <property name="concurrent" value="false"/>
- </bean>
- <bean id="refreshConfigCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
- <property name="jobDetail" ref="refreshConfigJobDetail"/>
- <property name="cronExpression" value="0 0/5 * * * ?"/>
- </bean>
- </beans>
这里设置了每5分钟重新读取一下,如果需要更即时的读取变化可通过修改cron表达式实现。
使用quartz实现不重启服务器修改自定义配置的更多相关文章
- 【Eclipse】Eclipse中tomcat的Server配置(解决修改代码不断的重启服务器)以及设置tomcat文件发布位置与JSP编译位置查看
Eclipse有时候修改一点JS或者JSP都会自动重启,有时候修改完JS或者JSP之后必须重启服务器才生效,下面研究了server的一些选项之后彻底解决了这些问题,下面做记录: 我的 Eclipse ...
- Linux下IP修改后重启服务器 oralce 出错(监听无法启动)
针对linux下修改IP导致的Oracle不能启动问题的解决 主要修改/etc/hosts配置文件.修改前配置: # Do not remove the following line, or vari ...
- scoket模块 粘包问题 tcp协议特点 重启服务器会遇到地址被占用问题
scoket()模块函数用法 import socket socket.socket(socket_family,socket_type,protocal=0) 获取tcp/ip套接字 tcpsock ...
- post提交/文件上传服务器修改
第一步:修改在php5下POST文件大小的限制 1.编修php.ini 找到:max_execution_time = 30 ,这个是每个脚本运行的最长时间,单位秒,修改为: max_exec ...
- 超实用,你get了吗?再也不怕本地文件更新到环境用Linux命令重启服务器了。。。
来公司这么久,写过不少代码,可是一碰见关于Linux命令操作的马上绕过,每次都是嚷嚷同事过来帮我替换文件,重启服务器,一直害怕接触命令的我一次一次不嫌麻烦,哈哈.有没有醉了?其实我一直都知道操作不难, ...
- Rabbitmq重启服务器用户丢失解决办法
参考:https://blog.csdn.net/yiluoAK_47/article/details/78173563?utm_source=blogxgwz2 Rabbitmq创建的用户在服务器重 ...
- 关于阿里云ECS服务器修改远程端口的一点总结
般修改公司的远程服务器的登录端口号分为两大步: 一.修改注册表中的两个地方的端口号:(注册表打开命令:regedit) [HKEY_LOCAL_MACHINE\SYSTEM\CurrentContro ...
- 【转载】Windows服务器修改远程桌面默认端口
因为 windows服务器远程桌面端口默认是 3389端口,使用系统默认端口的风险很大,很容易被攻击软件扫描以及攻击,同时频繁的扫描和攻击会导致服务器的 CPU 及带宽资源耗尽,因此为了保证服务器的安 ...
- 阿里云ECS服务器购买流程 (自定义配置购买、按月、按量购买)教程
阿里ECS云服务器自定义购买流程 本文提供全图文流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
随机推荐
- 自学Zabbix9.3 zabbix客户端自动注册
自学Zabbix9.3 zabbix客户端自动注册 1. 概述 网络自动发现配置,只要就是zabbix server去扫描一个网段,把在线的主机添加到Host列表中.但是Active agent是主动 ...
- git 设置和取消代理
# 设置ss git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'soc ...
- Python3.5:装饰器的使用
在Python里面函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数,简单来说函数也是变量也可以作文函数的参数 >>> def funA(): ... pr ...
- 496. Next Greater Element I
You are given two arrays(without duplicates)nums1andnums2wherenums1's elements are subset ofnums2. F ...
- WPF单位真的与分辨率无关吗?
转载自http://www.cnblogs.com/helloj2ee/archive/2009/04/21/1440709.htm WPF从发布之日起,一直将"分辨率无关(resoluti ...
- IDE 、SATA、SCSI 的区别
http://chuanwang66.iteye.com/blog/1134784 IDE IDE的英文全称为“Integrated Drive Electronics”,即“电子集成驱动器”,它的 ...
- 随手记一下,VS2015卡顿问题解决。
不知道什么开始,vs2015卡顿的很,启动时加载项目很慢,调试是启动慢,停止调试时直接卡死半分钟.其他都还能忍受,最不能忍受的是点击停止调试按钮后十几秒没反应! 网上有解决方案如下几个,我试了,都不行 ...
- java之自动过滤提交文本中的html代码script代码
public class test { public static String Html2Text(String inputString) { String htmlStr = inputStrin ...
- Appsacn 定期自动化扫描
appscan提供了计划扫描的选项,配合windows的计划任务,可以按需设定. 操作流程如下: 1.打开Appsacn--工具---扫描调度程序---新建 2.新建后显示如下窗口 3.填写好相应的设 ...
- JS 中 cookie 的使用
1.cookie 是什么? ①.cookie 是存储于访问者计算机中的变量.每当一台计算机通过浏览器来访问某个页面时,那么就可以通过 JavaScript 来创建和读取 cookie. ②.实际上 c ...