W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
如上所述,我們將使用 Maven 來運(yùn)行應(yīng)用程序。這一次你需要打開兩個(gè)控制臺(tái)窗口給每個(gè)項(xiàng)目。用 Ctrl-C 可以停止它。
首先我們將啟動(dòng) LogEventBroadcaster 如清單13.4所示,除了已經(jīng)構(gòu)建項(xiàng)目以下命令即可(使用默認(rèn)值):
$ mvn exec:exec -Pchapter13-LogEventBroadcaster
和之前一樣,這將通過 UDP 廣播日志消息。
現(xiàn)在,在一個(gè)新窗口,構(gòu)建和啟動(dòng) LogEventMonitor 接收和顯示廣播消息。
Listing 13.9 Compile and start the LogEventBroadcaster
$ mvn clean package exec:exec -Pchapter13-LogEventMonitor
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building netty-in-action 0.1-SNAPSHOT
[INFO] --------------------------------------------------------------------
...
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ netty-in-action ---
[INFO] Building jar: /Users/norman/Documents/workspace-intellij/netty-in-actionprivate/
target/netty-in-action-0.1-SNAPSHOT.jar
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ netty-in-action ---
LogEventMonitor running
當(dāng)看到 “LogEventMonitor running” 說明程序運(yùn)行成功了。
控制臺(tái)將顯示任何事件被添加到日志文件中,如下所示。消息的格式是由LogEventHandler 創(chuàng)建。
Listing 13.10 LogEventMonitor output
1364217299382 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 13:55:08 dev-linux
dhclient: DHCPREQUEST of 192.168.0.50 on eth2 to 192.168.0.254 port 67
1364217299382 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 13:55:08 dev-linux
dhclient: DHCPACK of 192.168.0.50 from 192.168.0.254
1364217299382 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 13:55:08 dev-linux
dhclient: bound to 192.168.0.50 -- renewal in 270 seconds.
1364217299382 [/192.168.0.38:63182] [[/var/log/messages] : Mar 25 13:59:38 dev-linux
dhclient: DHCPREQUEST of 192.168.0.50 on eth2 to 192.168.0.254 port 67
1364217299382 [/192.168.0.38:63182] [/[/var/log/messages] : Mar 25 13:59:38 dev-linux
dhclient: DHCPACK of 192.168.0.50 from 192.168.0.254
1364217299382 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 13:59:38 dev-linux
dhclient: bound to 192.168.0.50 -- renewal in 259 seconds.
1364217299383 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 14:03:57 dev-linux
dhclient: DHCPREQUEST of 192.168.0.50 on eth2 to 192.168.0.254 port 67
1364217299383 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 14:03:57 dev-linux
dhclient: DHCPACK of 192.168.0.50 from 192.168.0.254
1364217299383 [/192.168.0.38:63182] [/var/log/messages] : Mar 25 14:03:57 dev-linux
dhclient: bound to 192.168.0.50 -- renewal in 285 seconds.
若你沒有訪問 UNIX syslog 的權(quán)限,可以創(chuàng)建 自定義的文件,手動(dòng)填入內(nèi)容。下面是 UNIX 命令用 touch 創(chuàng)建一個(gè)空文件
$ touch ~/mylog.log
再次啟動(dòng) LogEventBroadcaster,設(shè)置系統(tǒng)屬性
$ mvn exec:exec -Pchapter13-LogEventBroadcaster -Dlogfile=~/mylog.log
當(dāng) LogEventBroadcaster 運(yùn)行時(shí),你可以手動(dòng)的添加消息到文件來查看廣播到 LogEventMonitor 控制臺(tái)的內(nèi)容。使用 echo 和輸出的文件
$ echo ’Test log entry’ >> ~/mylog.log
你可以啟動(dòng)任意個(gè)監(jiān)視器實(shí)例,他們都會(huì)收到相同的消息。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: