Java 实现自动监听并更新配置文件内容
来源:互联网 发布:mac如何创建快捷方式 编辑:程序博客网 时间:2024/05/26 19:15
自动监听器 PropertiesListener
自动监听时钟 PropertiesTimer
自动监听任务 PropertiesTask
web.xml 配置
package org.stephencat.listener;
import javax.servlet.*;
/**
* 自动监听器
* @author stephen
*
*/
public class PropertiesListener implements ServletContextListener {
/**
* 自动监听时钟
*/
private PropertiesTimer rt = null;
public void contextInitialized(ServletContextEvent event) {
String status = "Properties listener start .";
event.getServletContext().log(status);
System.out.println(status);
// 激活自动监听时钟
rt = new PropertiesTimer(15, event.getServletContext());
rt.start();
}
public void contextDestroyed(ServletContextEvent event) {
String status = "Properties listener stop .";
event.getServletContext().log(status);
System.out.println(status);
// 停止自动监听时钟
if (rt != null) {
rt.stop();
}
}
}
import javax.servlet.*;
/**
* 自动监听器
* @author stephen
*
*/
public class PropertiesListener implements ServletContextListener {
/**
* 自动监听时钟
*/
private PropertiesTimer rt = null;
public void contextInitialized(ServletContextEvent event) {
String status = "Properties listener start .";
event.getServletContext().log(status);
System.out.println(status);
// 激活自动监听时钟
rt = new PropertiesTimer(15, event.getServletContext());
rt.start();
}
public void contextDestroyed(ServletContextEvent event) {
String status = "Properties listener stop .";
event.getServletContext().log(status);
System.out.println(status);
// 停止自动监听时钟
if (rt != null) {
rt.stop();
}
}
}
自动监听时钟 PropertiesTimer
package org.stephencat.listener;
import java.util.*;
import javax.servlet.*;
/**
* 自动监听时钟
* @author stephen
*
*/
public class PropertiesTimer {
private final Timer timer = new Timer();
private final int sec;
private ServletContext context = null;
public PropertiesTimer(int seconds, ServletContext context) {
sec = seconds;
this.context = context;
}
/**
* 启动自动监听任务
*/
public void start() {
// 取得当前日期时间
Date date = new Date();
// 执行自动监听计划
timer.schedule(new PropertiesTask(this.context), date, sec * 1000);
}
/**
* 停止自动监听任务
*/
public void stop() {
timer.cancel();
}
}
import java.util.*;
import javax.servlet.*;
/**
* 自动监听时钟
* @author stephen
*
*/
public class PropertiesTimer {
private final Timer timer = new Timer();
private final int sec;
private ServletContext context = null;
public PropertiesTimer(int seconds, ServletContext context) {
sec = seconds;
this.context = context;
}
/**
* 启动自动监听任务
*/
public void start() {
// 取得当前日期时间
Date date = new Date();
// 执行自动监听计划
timer.schedule(new PropertiesTask(this.context), date, sec * 1000);
}
/**
* 停止自动监听任务
*/
public void stop() {
timer.cancel();
}
}
自动监听任务 PropertiesTask
package org.stephencat.listener;
import java.io.IOException;
import java.io.*;
import java.util.*;
import javax.servlet.*;
/**
* 自动监听任务
* @author stephen
*
*/
public class PropertiesTask extends TimerTask {
private ServletContext context = null;
/**
* 配置文件的最后更新时间
*/
private long lastModified = 0;
/**
* 构造一个自动更新任务
* @param context
*/
public PropertiesTask(ServletContext context){
this.context = context;
System.out.println("A task instance is created now."); // 任务在整个 application 周期内只创建一次。
}
/**
* 每次执行任务时显示一个随机数。
*/
public void todoTestRandom(){
System.out.println("Task running");
context.setAttribute("random", String.valueOf(Math.random()));
System.out.println((String)context.getAttribute("random"));
}
/**
* 监听配置文件是否被更新。
*/
public void todoTestFileStatus(){
System.out.println("Getting file status");
System.out.println(this.isFileUpdated("WEB-INF/platforms/test.properties"));
}
/**
* 监听配置文件是否被更新,自动更新文件中的配置项存储到 application 变量中。
*/
public void todo(){
String filename = "WEB-INF/platforms/test.properties";
if(this.isFileUpdated(filename)){
System.out.println("Getting properties");
try{
this.loadProperties("test", filename);
}catch(IOException ioe){
System.err.println(ioe.getMessage());
}
}
System.out.println("Test value is: " + this.getTestProperty("name"));
}
public void run() {
todoTestRandom();
todo();
//todo();
}
/**
* 判断物理文件是否已被更新
* @param filename 物理文件名
* @return 是 true 否 false
*/
private boolean isFileUpdated(String filename){
File file = new File(context.getRealPath(filename));
if(file.isFile()){
long lastUpdateTime = file.lastModified();
if(lastUpdateTime > this.lastModified){
System.out.println("The properties file was modified.");
this.lastModified = lastUpdateTime;
return true;
}else{
System.out.println("The properties file was not modified.");
return false;
}
}else{
System.out.println("The path does not point to a file.");
return false;
}
}
/**
* 获取配置文件
* @param key
* @param filename
* @return
*/
public void loadProperties(String key, String filename) throws IOException{
Properties prop = new Properties();
InputStream stream = context.getResourceAsStream(filename);
prop.load(stream);
if(stream!=null){
stream.close();
}
context.setAttribute(key, prop);
}
/**
* 从 application 取配置项的值
* @param key 配置项的键名
* @return 配置项的值
*/
public String getTestProperty(String key){
Properties prop = (Properties)context.getAttribute("test");
if(prop==null){
return null;
}else{
return (String)prop.get(key);
}
}
}
import java.io.IOException;
import java.io.*;
import java.util.*;
import javax.servlet.*;
/**
* 自动监听任务
* @author stephen
*
*/
public class PropertiesTask extends TimerTask {
private ServletContext context = null;
/**
* 配置文件的最后更新时间
*/
private long lastModified = 0;
/**
* 构造一个自动更新任务
* @param context
*/
public PropertiesTask(ServletContext context){
this.context = context;
System.out.println("A task instance is created now."); // 任务在整个 application 周期内只创建一次。
}
/**
* 每次执行任务时显示一个随机数。
*/
public void todoTestRandom(){
System.out.println("Task running");
context.setAttribute("random", String.valueOf(Math.random()));
System.out.println((String)context.getAttribute("random"));
}
/**
* 监听配置文件是否被更新。
*/
public void todoTestFileStatus(){
System.out.println("Getting file status");
System.out.println(this.isFileUpdated("WEB-INF/platforms/test.properties"));
}
/**
* 监听配置文件是否被更新,自动更新文件中的配置项存储到 application 变量中。
*/
public void todo(){
String filename = "WEB-INF/platforms/test.properties";
if(this.isFileUpdated(filename)){
System.out.println("Getting properties");
try{
this.loadProperties("test", filename);
}catch(IOException ioe){
System.err.println(ioe.getMessage());
}
}
System.out.println("Test value is: " + this.getTestProperty("name"));
}
public void run() {
todoTestRandom();
todo();
//todo();
}
/**
* 判断物理文件是否已被更新
* @param filename 物理文件名
* @return 是 true 否 false
*/
private boolean isFileUpdated(String filename){
File file = new File(context.getRealPath(filename));
if(file.isFile()){
long lastUpdateTime = file.lastModified();
if(lastUpdateTime > this.lastModified){
System.out.println("The properties file was modified.");
this.lastModified = lastUpdateTime;
return true;
}else{
System.out.println("The properties file was not modified.");
return false;
}
}else{
System.out.println("The path does not point to a file.");
return false;
}
}
/**
* 获取配置文件
* @param key
* @param filename
* @return
*/
public void loadProperties(String key, String filename) throws IOException{
Properties prop = new Properties();
InputStream stream = context.getResourceAsStream(filename);
prop.load(stream);
if(stream!=null){
stream.close();
}
context.setAttribute(key, prop);
}
/**
* 从 application 取配置项的值
* @param key 配置项的键名
* @return 配置项的值
*/
public String getTestProperty(String key){
Properties prop = (Properties)context.getAttribute("test");
if(prop==null){
return null;
}else{
return (String)prop.get(key);
}
}
}
web.xml 配置
<?xml version="1.0" encoding="GB18030"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.stephencat.listener.PropertiesListener</listener-class>
</listener>
</web-app>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.stephencat.listener.PropertiesListener</listener-class>
</listener>
</web-app>
- JAVA 实现自动监听并更新配置文件内容
- Java 实现自动监听并更新配置文件内容
- Java 实现自动监听并更新配置文件内容 (转自高手)
- 求助,怎么实现新闻自动采集并更新内容
- JAVA自动监听文件内容
- java实现文本框JTextField的实施监听输入内容,并实时显示出来
- Java实现系统目录实时监听更新。
- 【android】EditText监听,根据输入内容查询数据并动态更新ListView
- 用js实现对话框修改内容,并更新主页面
- css实现内容超长自动截取并显示省略号
- 两行代码实现标题栏软件更新并自动安装
- android实现静默更新安装并自动启动
- JAVA自动监听程序
- java获取配置文件内容
- java读取配置文件内容
- java web配置文件修改监听
- Android 实现监听开机启动开启后台服务,并实现自动重启。
- android广播监听短信并显示内容
- VC++学习笔记3--------------设置static text背景为透明的办法
- git reset
- git小技巧
- 在QtCreator中使用自定义控件及在vs2010中集成qt
- HTML5 中input的新属性
- Java 实现自动监听并更新配置文件内容
- 难过了,不要告诉别人,自己知道就好
- Java复习总结:Java基础程序设计
- 写于大四,工作一周
- 字符串模式匹配(所谓的kmp)算法
- jQuery图片连续滚动 li 标签实现
- Ext Js 3.2 EditorGridPanel 加入一行后进行保存并进行数据验证
- 同步的前提和同步函数
- 情书