我的Java路上那些事兒
快樂(lè)成長(zhǎng)
posts - 110, comments - 101, trackbacks - 0, articles - 7
BlogJava
::
首頁(yè)
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
服務(wù)器監(jiān)控狗 log4j FileWatchdog 臨控環(huán)境變化 方便多了
Posted on 2012-02-17 14:03
云云
閱讀(3553)
評(píng)論(0)
編輯
收藏
用java
寫服務(wù)
程序
時(shí)經(jīng)常會(huì)涉及到監(jiān)控某些配置
文件
,當(dāng)配置文件發(fā)生變化時(shí)實(shí)時(shí)重新加載該文件的內(nèi)容到內(nèi)存.
實(shí)際上log4j里有現(xiàn)成的類FileWatchdog做了類似的工作.我們只需繼承它,然后重寫它的一些方法就可以了.
/** */
/**
使用log4j的監(jiān)控狗
*/
public
class
IPAccessFileWatchdog
extends
FileWatchdog
{
public
IPAccessFileWatchdog(String filename)
{
super
(filename);
}
public
void
doOnChange()
{
List
<
String
>
list
=
IPAccessController.
this
.loadIPRule(
new
File(
this
.filename));
if
(list
!=
null
)
{
IPAccessController.
this
.ipRule
=
list.toArray(
new
String[list.size()]);
}
else
{
IPAccessController.
this
.ipRule
=
null
;
}
LogLog.warn(
"
ip access config load completed from file:
"
+
filename);
}
}
}
FileWatchdog的代碼也很簡(jiǎn)單,其實(shí)就是起一個(gè)線程,每隔固定的時(shí)間掃描一次監(jiān)控的文件.我把代碼也貼出來(lái):
'
//
Contributors: Mathias Bogaert
package
org.apache.log4j.helpers;
import
java.io.File;
import
org.apache.log4j.helpers.LogLog;
public
abstract
class
FileWatchdog
extends
Thread
{
static
final
public
long
DEFAULT_DELAY
=
60000
;
protected
String filename;
protected
long
delay
=
DEFAULT_DELAY;
File file;
long
lastModif
=
0
;
boolean
warnedAlready
=
false
;
boolean
interrupted
=
false
;
protected
FileWatchdog(String filename)
{
this
.filename
=
filename;
file
=
new
File(filename);
setDaemon(
true
);
checkAndConfigure();
}
public
void
setDelay(
long
delay)
{
this
.delay
=
delay;
}
abstract
protected
void
doOnChange();
protected
void
checkAndConfigure()
{
boolean
fileExists;
try
{
fileExists
=
file.exists();
}
catch
(SecurityException e)
{
LogLog.warn(
"
Was not allowed to read check file existance, file:[
"
+
filename
+
"
].
"
);
interrupted
=
true
;
//
there is no point in continuing
return
;
}
if
(fileExists)
{
long
l
=
file.lastModified();
//
this can also throw a SecurityException
if
(l
>
lastModif)
{
//
however, if we reached this point this
lastModif
=
l;
//
is very unlikely.
doOnChange();
warnedAlready
=
false
;
}
}
else
{
if
(
!
warnedAlready)
{
LogLog.debug(
"
[
"
+
filename
+
"
] does not exist.
"
);
warnedAlready
=
true
;
}
}
}
public
void
run()
{
while
(
!
interrupted)
{
try
{
Thread.sleep(delay);
}
catch
(InterruptedException e)
{
//
no interruption expected
}
checkAndConfigure();
}
}
}
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
Powered by:
BlogJava
Copyright © 云云
日歷
<
2012年2月
>
日
一
二
三
四
五
六
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(9)
給我留言
查看公開(kāi)留言
查看私人留言
隨筆檔案
2015年7月 (1)
2014年9月 (3)
2014年1月 (3)
2013年12月 (1)
2013年11月 (4)
2013年10月 (2)
2013年7月 (2)
2013年6月 (3)
2013年4月 (2)
2013年1月 (2)
2012年12月 (4)
2012年11月 (3)
2012年10月 (3)
2012年9月 (2)
2012年8月 (1)
2012年7月 (9)
2012年6月 (2)
2012年5月 (6)
2012年4月 (7)
2012年3月 (2)
2012年2月 (1)
2012年1月 (1)
2011年12月 (2)
2011年11月 (16)
2011年10月 (7)
2011年8月 (1)
2011年6月 (2)
2011年5月 (5)
2011年4月 (9)
2011年3月 (10)
搜索
最新評(píng)論
1.?re: CAP原理與最終一致性 強(qiáng)一致性 透析
學(xué)習(xí)。
--NewSea
2.?re: 一致性哈希算法與Java實(shí)現(xiàn)
有一個(gè)問(wèn)題,如果使用虛擬節(jié)點(diǎn),某臺(tái)機(jī)器每次宕機(jī)再恢復(fù)后都需要遷移數(shù)據(jù)。這樣是否反而更麻煩了。
--三單聯(lián)咖啡色
3.?re: java static塊和static 方法 的使用區(qū)別
sss
--zhangsan
4.?re: struts2 jsp頁(yè)面使用s:if 標(biāo)簽
你是基佬 哦耶耶
--基佬
5.?re: android開(kāi)發(fā)過(guò)程中 R文件消失 clean 和 build project都無(wú)效 已解決
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--llll
閱讀排行榜
1.?linux 新建用戶、用戶組 以及為新用戶分配權(quán)限(127887)
2.?Oracle內(nèi)連接、外連接、右外連接、全外連接小總結(jié)(93205)
3.?zookeeper 集群安裝(單點(diǎn)與分布式成功安裝)摘錄(79153)
4.?android開(kāi)發(fā)過(guò)程中 R文件消失 clean 和 build project都無(wú)效 已解決(76972)
5.?一致性哈希算法與Java實(shí)現(xiàn) (48855)
評(píng)論排行榜
1.?Oracle內(nèi)連接、外連接、右外連接、全外連接小總結(jié)(12)
2.?zookeeper 集群安裝(單點(diǎn)與分布式成功安裝)摘錄(11)
3.?android開(kāi)發(fā)過(guò)程中 R文件消失 clean 和 build project都無(wú)效 已解決(6)
4.?struts2 jsp表單提交后保留表單中輸入框中的值 下拉框select與input(6)
5.?jquery 自動(dòng)過(guò)濾表單輸入框前后空格(5)
主站蜘蛛池模板:
亚洲午夜精品久久久久久浪潮
|
成年女人毛片免费观看97
|
国产精品亚洲玖玖玖在线观看
|
久久久久久精品免费看SSS
|
亚洲成年人免费网站
|
久久久久久一品道精品免费看
|
亚洲AV永久无码精品成人
|
国产成人免费ā片在线观看老同学
|
亚洲国产另类久久久精品小说
|
99久久精品日本一区二区免费
|
亚洲日产2021三区
|
成年女人18级毛片毛片免费观看
|
亚洲一区二区三区乱码在线欧洲
|
老司机永久免费网站在线观看
|
国产亚洲精品美女久久久久久下载
|
国产一级一片免费播放i
|
eeuss影院免费直达入口
|
亚洲gv猛男gv无码男同短文
|
2019中文字幕在线电影免费
|
7777久久亚洲中文字幕
|
日韩午夜免费视频
|
久久国产精品免费一区
|
亚洲精品在线不卡
|
国产大片91精品免费看3
|
一级一级一级毛片免费毛片
|
亚洲成av人片在线观看无码不卡
|
日本黄网站动漫视频免费
|
亚洲国产午夜精品理论片在线播放
|
色在线亚洲视频www
|
免费大香伊蕉在人线国产
|
中文字幕免费在线播放
|
噜噜噜亚洲色成人网站∨
|
国产精品极品美女免费观看
|
aa级女人大片喷水视频免费
|
久久亚洲AV成人无码
|
国产一区二区免费在线
|
青青青国产手机频在线免费观看
|
亚洲AV综合色区无码二区爱AV
|
亚洲а∨天堂久久精品
|
日韩精品人妻系列无码专区免费
|
亚洲日韩精品无码专区
|