Netty 編譯和運行 Echo 服務(wù)器和客戶端

2018-08-02 14:28 更新

當完成了echo客戶端和服務(wù)器的編寫之后,我們就要來掌握編譯及運行echo服務(wù)器和客戶端,下面一起來看看。

編譯 Echo 服務(wù)器 和 客戶端

本例涉及到多模塊 Maven 項目的組織

在例子 chapter2 目錄下,執(zhí)行

mvn clean package

輸出如下

Listing 2.6 Build Output

chapter2>mvn clean package
[INFO] Scanning for projects...
[INFO] --------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] Echo Client and Server
[INFO] Echo Client
[INFO] Echo Server
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building Echo Client and Server 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ echo-parent ---
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building Echo Client 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ echo-client ---
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile)
@ echo-client ---
[INFO] Changes detected - recompiling the module!
[INFO] --------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Echo Client and Server ......................... SUCCESS [ 0.118 s]
[INFO] Echo Client .................................... SUCCESS [ 1.219 s]
[INFO] Echo Server .................................... SUCCESS [ 0.110 s]
[INFO] --------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------
[INFO] Total time: 1.561 s
[INFO] Finished at: 2014-06-08T17:39:15-05:00
[INFO] Final Memory: 14M/245M
[INFO] --------------------------------------------------------------------

注意事項:

  • Maven Reactor 構(gòu)建順序:先是 父 POM,然后是子項目
  • Netty artifact 沒在用戶的本地存儲庫中找到,所以 Maven 就會從互聯(lián)網(wǎng)上下載
  • clean 和 compile 在構(gòu)建生命周期的運行。事后 mavensurefire-plugin 插件運行,但不會有測試類存在。最后 mavenjar-plugin 執(zhí)行

這段說明了項目已經(jīng)成功編譯。

運行 Echo 服務(wù)器 和 客戶端

我們使用 exec-maven-plugin 來運行項目。

在 chapter2/Server 目錄,執(zhí)行

mvn exec:java

輸出如下:

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building Echo Server 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ echo-server >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ echo-server <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ echo-server ---
nettyinaction.echo.EchoServer started and listening for connections on
/0:0:0:0:0:0:0:0:9999

在 chapter2/Client 目錄,執(zhí)行

mvn exec:java

輸出如下:

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building Echo Client 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ echo-client >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ echo-client <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ echo-client ---
Client received: Netty rocks!
[INFO] --------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------
[INFO] Total time: 3.907 s
[INFO] Finished at: 2014-06-08T18:26:14-05:00
[INFO] Final Memory: 8M/245M
[INFO] --------------------------------------------------------------------

在服務(wù)器的控制臺輸出:

Server received: Netty rocks!

發(fā)生了什么事:

  • 客戶端連接后,它發(fā)送消息:“Netty rocks!”
  • 服務(wù)器輸出接收到消息并將其返回給客戶端
  • 客戶輸出接收到的消息并退出。

每次運行客戶端,你會看到在服務(wù)器的控制臺輸出:

Server received: Netty rocks!

現(xiàn)在,我們看下錯誤的情況。在控制臺 輸入 Ctrl-C 來關(guān)閉服務(wù)器。而后運行客戶端,此時輸出如下:

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building Echo Client 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ echo-client >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ echo-client <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ echo-client ---
[WARNING]
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke
    (NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke
    (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:744)
    Caused by: java.net.ConnectException: Connection refused:
    no further information: localhost/127.0.0.1:9999
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect
    (SocketChannelImpl.java:739)
    at io.netty.channel.socket.nio.NioSocketChannel
    .doFinishConnect(NioSocketChannel.java:191)
    at io.netty.channel.nio.
    AbstractNioChannel$AbstractNioUnsafe.finishConnect(
    AbstractNioChannel.java:279)
    at io.netty.channel.nio.NioEventLoop
    .processSelectedKey(NioEventLoop.java:511)
    at io.netty.channel.nio.NioEventLoop
    .processSelectedKeysOptimized(NioEventLoop.java:461)
    at io.netty.channel.nio.NioEventLoop
    .processSelectedKeys(NioEventLoop.java:378)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
    at io.netty.util.concurrent
    .SingleThreadEventExecutor$2.run
    (SingleThreadEventExecutor.java:101)
    ... 1 more
[INFO] --------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] --------------------------------------------------------------------
[INFO] Total time: 3.728 s
[INFO] Finished at: 2014-06-08T18:49:13-05:00
[INFO] Final Memory: 8M/245M
[INFO] --------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java
    (default-cli) on project echo-client: An exception occured while executing the
    Java class. null: InvocationTargetException: Connection refused: no further
    information:
    localhost/127.0.0.1:9999 -> [Help 1]

看到了嗎?客戶端嘗試連接服務(wù)器,但服務(wù)器處于關(guān)閉狀態(tài),所以引發(fā)了一個 java.net.ConnectException ,這個異常被 EchoClientHandler 的 exceptionCaught() 觸發(fā),打印出異常信息,并關(guān)閉 channel.


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號