Netty 運(yùn)行LogEventBroadcaster和LogEventMonitor

2018-08-07 14:31 更新

如上所述,我們將使用 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ì)收到相同的消息。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號