编译和运行 Echo 服务器和客户端

编译

本例涉及到多模块 Maven 项目的组织

在例子 chapter2 目录下,执行

  1. mvn clean package

输出如下

Listing 2.6 Build Output

  1. chapter2>mvn clean package
  2. [INFO] Scanning for projects...
  3. [INFO] --------------------------------------------------------------------
  4. [INFO] Reactor Build Order:
  5. [INFO]
  6. [INFO] Echo Client and Server
  7. [INFO] Echo Client
  8. [INFO] Echo Server
  9. [INFO]
  10. [INFO] --------------------------------------------------------------------
  11. [INFO] Building Echo Client and Server 1.0-SNAPSHOT
  12. [INFO] --------------------------------------------------------------------
  13. [INFO]
  14. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ echo-parent ---
  15. [INFO]
  16. [INFO] --------------------------------------------------------------------
  17. [INFO] Building Echo Client 1.0-SNAPSHOT
  18. [INFO] --------------------------------------------------------------------
  19. [INFO]
  20. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ echo-client ---
  21. [INFO]
  22. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile)
  23. @ echo-client ---
  24. [INFO] Changes detected - recompiling the module!
  25. [INFO] --------------------------------------------------------------------
  26. [INFO] Reactor Summary:
  27. [INFO]
  28. [INFO] Echo Client and Server ......................... SUCCESS [ 0.118 s]
  29. [INFO] Echo Client .................................... SUCCESS [ 1.219 s]
  30. [INFO] Echo Server .................................... SUCCESS [ 0.110 s]
  31. [INFO] --------------------------------------------------------------------
  32. [INFO] BUILD SUCCESS
  33. [INFO] --------------------------------------------------------------------
  34. [INFO] Total time: 1.561 s
  35. [INFO] Finished at: 2014-06-08T17:39:15-05:00
  36. [INFO] Final Memory: 14M/245M
  37. [INFO] --------------------------------------------------------------------

注意事项:

  • Maven Reactor 构建顺序:先是 父 POM,然后是子项目
  • Netty artifact 没在用户的本地存储库中找到,所以 Maven 就会从互联网上下载
  • clean 和 compile 在构建生命周期的运行。事后 mavensurefire-plugin
    插件运行,但不会有测试类存在。最后 mavenjar-plugin 执行

这段说明了项目已经成功编译。

运行 Echo 服务器 和 客户端

我们使用 exec-maven-plugin 来运行项目。

在 chapter2/Server 目录,执行

  1. mvn exec:java

输出如下:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] --------------------------------------------------------------------
  4. [INFO] Building Echo Server 1.0-SNAPSHOT
  5. [INFO] --------------------------------------------------------------------
  6. [INFO]
  7. [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ echo-server >>>
  8. [INFO]
  9. [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ echo-server <<<
  10. [INFO]
  11. [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ echo-server ---
  12. nettyinaction.echo.EchoServer started and listening for connections on
  13. /0:0:0:0:0:0:0:0:9999

在 chapter2/Client 目录,执行

  1. mvn exec:java

输出如下:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] --------------------------------------------------------------------
  4. [INFO] Building Echo Client 1.0-SNAPSHOT
  5. [INFO] --------------------------------------------------------------------
  6. [INFO]
  7. [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ echo-client >>>
  8. [INFO]
  9. [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ echo-client <<<
  10. [INFO]
  11. [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ echo-client ---
  12. Client received: Netty rocks!
  13. [INFO] --------------------------------------------------------------------
  14. [INFO] BUILD SUCCESS
  15. [INFO] --------------------------------------------------------------------
  16. [INFO] Total time: 3.907 s
  17. [INFO] Finished at: 2014-06-08T18:26:14-05:00
  18. [INFO] Final Memory: 8M/245M
  19. [INFO] --------------------------------------------------------------------

在服务器的控制台输出:

  1. Server received: Netty rocks!

发生了什么事:

  • 客户端连接后,它发送消息:“Netty rocks!”
  • 服务器输出接收到消息并将其返回给客户端
  • 客户输出接收到的消息并退出。

每次运行客户端,你会看到在服务器的控制台输出:

  1. Server received: Netty rocks!

现在,我们看下错误的情况。在控制台 输入 Ctrl-C 来关闭服务器。而后运行客户端,此时输出如下:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] --------------------------------------------------------------------
  4. [INFO] Building Echo Client 1.0-SNAPSHOT
  5. [INFO] --------------------------------------------------------------------
  6. [INFO]
  7. [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ echo-client >>>
  8. [INFO]
  9. [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ echo-client <<<
  10. [INFO]
  11. [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ echo-client ---
  12. [WARNING]
  13. java.lang.reflect.InvocationTargetException
  14. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  15. at sun.reflect.NativeMethodAccessorImpl.invoke
  16. (NativeMethodAccessorImpl.java:57)
  17. at sun.reflect.DelegatingMethodAccessorImpl.invoke
  18. (DelegatingMethodAccessorImpl.java:43)
  19. at java.lang.reflect.Method.invoke(Method.java:606)
  20. at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
  21. at java.lang.Thread.run(Thread.java:744)
  22. Caused by: java.net.ConnectException: Connection refused:
  23. no further information: localhost/127.0.0.1:9999
  24. at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
  25. at sun.nio.ch.SocketChannelImpl.finishConnect
  26. (SocketChannelImpl.java:739)
  27. at io.netty.channel.socket.nio.NioSocketChannel
  28. .doFinishConnect(NioSocketChannel.java:191)
  29. at io.netty.channel.nio.
  30. AbstractNioChannel$AbstractNioUnsafe.finishConnect(
  31. AbstractNioChannel.java:279)
  32. at io.netty.channel.nio.NioEventLoop
  33. .processSelectedKey(NioEventLoop.java:511)
  34. at io.netty.channel.nio.NioEventLoop
  35. .processSelectedKeysOptimized(NioEventLoop.java:461)
  36. at io.netty.channel.nio.NioEventLoop
  37. .processSelectedKeys(NioEventLoop.java:378)
  38. at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
  39. at io.netty.util.concurrent
  40. .SingleThreadEventExecutor$2.run
  41. (SingleThreadEventExecutor.java:101)
  42. ... 1 more
  43. [INFO] --------------------------------------------------------------------
  44. [INFO] BUILD FAILURE
  45. [INFO] --------------------------------------------------------------------
  46. [INFO] Total time: 3.728 s
  47. [INFO] Finished at: 2014-06-08T18:49:13-05:00
  48. [INFO] Final Memory: 8M/245M
  49. [INFO] --------------------------------------------------------------------
  50. [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java
  51. (default-cli) on project echo-client: An exception occured while executing the
  52. Java class. null: InvocationTargetException: Connection refused: no further
  53. information:
  54. localhost/127.0.0.1:9999 -> [Help 1]

发生了啥?客户端尝试连接服务器,但服务器是关闭的,所以引发了一个 java.net.ConnectException ,这个异常被 EchoClientHandler 的 exceptionCaught() 触发,打印出异常信息,并关闭 channel