java 监听的配置文件_Java 实现自动监听并更新配置文件内容 (转自高手)

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Web开发中,监听器(Listener)用于监听Web应用程序中的事件(如请求、会话、上下文等),并在发生这些事件时执行相应的操作。在web.xml文件中配置监听器,可以让Web应用程序在启动时自动注册和启用监听器。 以下是Java Web监听器的web.xml配置详细信息: 1. 在web.xml文件中添加如下代码: ``` <listener> <listener-class>com.example.MyListener</listener-class> </listener> ``` 其中,`com.example.MyListener`是自定义的监听器类。 2. 实现监听器接口 在自定义的监听器类中,需要实现对应的监听器接口,例如: ``` public class MyListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { // 在Web应用程序启动时执行的操作 } public void contextDestroyed(ServletContextEvent event) { // 在Web应用程序关闭时执行的操作 } } ``` 其中,`ServletContextListener`是Web应用程序上下文监听器接口,用于监听Web应用程序的启动和关闭事件。在`contextInitialized`方法中,可以执行Web应用程序启动时需要初始化的操作;在`contextDestroyed`方法中,可以执行Web应用程序关闭时需要清理资源的操作。 3. 配置多个监听器 同一个Web应用程序中可以配置多个监听器,例如: ``` <listener> <listener-class>com.example.MyListener1</listener-class> </listener> <listener> <listener-class>com.example.MyListener2</listener-class> </listener> ``` 其中,`com.example.MyListener1`和`com.example.MyListener2`是两个不同的监听器类。它们可以分别监听不同的事件,实现不同的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值