我的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)
主站蜘蛛池模板:
亚洲午夜免费视频
|
中文字幕手机在线免费看电影
|
久久99热精品免费观看动漫
|
亚洲日本天堂在线
|
欧美大尺寸SUV免费
|
亚洲最大中文字幕
|
一级女人18毛片免费
|
亚洲导航深夜福利
|
国产成人免费爽爽爽视频
|
亚洲熟妇AV乱码在线观看
|
浮力影院第一页小视频国产在线观看免费
|
亚洲av无码兔费综合
|
日本一道一区二区免费看
|
国产区图片区小说区亚洲区
|
国产一级一片免费播放
|
国产特黄特色的大片观看免费视频
|
国产亚洲精品高清在线
|
三年在线观看免费观看完整版中文
|
亚洲AV日韩AV永久无码久久
|
99久热只有精品视频免费看
|
亚洲入口无毒网址你懂的
|
破了亲妺妺的处免费视频国产
|
免费看又黄又爽又猛的视频软件
|
中文字幕中韩乱码亚洲大片
|
久久免费精品视频
|
亚洲一区二区三区久久久久
|
国产国产人免费人成免费视频
|
国产免费播放一区二区
|
亚洲黄色高清视频
|
日本一线a视频免费观看
|
国产精品免费久久久久电影网
|
亚洲一区二区三区四区在线观看
|
国产精品成人免费视频网站京东
|
亚洲阿v天堂在线
|
黄在线观看www免费看
|
亚洲av永久无码
|
亚洲AV日韩AV永久无码下载
|
岛国片在线免费观看
|
伊人免费在线观看高清版
|
亚洲国产成人精品无码区在线网站
|
免费成人黄色大片
|