java监控上传任务_java自动监听任务 PropertiesTask

packageorg.stephencat.listener;importjava.io.IOException;importjava.io.*;importjava.util.*;importjavax.servlet.*;/*** 自动监听任务

*@authorstephen

**/publicclassPropertiesTaskextendsTimerTask {privateServletContext context=null;/*** 配置文件的最后更新时间*/privatelonglastModified=0;/*** 构造一个自动更新任务

*@paramcontext*/publicPropertiesTask(ServletContext context){this.context=context;

System.out.println("A task instance is created now.");//任务在整个 application 周期内只创建一次。}/*** 每次执行任务时显示一个随机数。*/publicvoidtodoTestRandom(){

System.out.println("Task running");

context.setAttribute("random", String.valueOf(Math.random()));

System.out.println((String)context.getAttribute("random"));

}/*** 监听配置文件是否被更新。*/publicvoidtodoTestFileStatus(){

System.out.println("Getting file status");

System.out.println(this.isFileUpdated("WEB-INF/platforms/test.properties"));

}/*** 监听配置文件是否被更新,自动更新文件中的配置项存储到 application 变量中。*/publicvoidtodo(){

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"));

}publicvoidrun() {

todoTestRandom();

todo();//todo();}/*** 判断物理文件是否已被更新

*@paramfilename 物理文件名

*@return是 true 否 false*/privatebooleanisFileUpdated(String filename){

File file=newFile(context.getRealPath(filename));if(file.isFile()){longlastUpdateTime=file.lastModified();if(lastUpdateTime>this.lastModified){

System.out.println("The properties file was modified.");this.lastModified=lastUpdateTime;returntrue;

}else{

System.out.println("The properties file was not modified.");returnfalse;

}

}else{

System.out.println("The path does not point to a file.");returnfalse;

}

}/*** 获取配置文件

*@paramkey

*@paramfilename

*@return*/publicvoidloadProperties(String key, String filename)throwsIOException{

Properties prop=newProperties();

InputStream stream=context.getResourceAsStream(filename);

prop.load(stream);if(stream!=null){

stream.close();

}

context.setAttribute(key, prop);

}/*** 从 application 取配置项的值

*@paramkey 配置项的键名

*@return配置项的值*/publicString getTestProperty(String key){

Properties prop=(Properties)context.getAttribute("test");if(prop==null){returnnull;

}else{return(String)prop.get(key);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值