2.5 编译和运行回声客户端和服务端

TODO

In this section, Ill go through the various steps needed to run your echo client and server. In 
large, complex projects, you often need to use a build tool, and as mentioned previously, all 
the examples in this book use Maven. You can compile the echo client and server with any 
other build tool (or even with the javac command). The most important thing is that the Netty 
jar needs to be on the class path because the code depends on it. 
To compile the example, you have to do some preparation first, as the maven has its own 
dependencies. The next section guides you through that process. 
2.5.1 Compiling the server and client 
Once everything is in place, its easy to compile your application execute the mvn command 
from the root directory of the project (where pom.xml exists). The following listing shows the 
command output. 
Listing 2.7 Compile the source 
Normans-MacBook-Pro:netty-in-action norman$ mvn clean package 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ----------------------------------------------------------------------
-- 
[INFO] Building netty-in-action 0.1-SNAPSHOT 
[INFO] ----------------------------------------------------------------------
-- 
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ netty-in-action -
-- 
[INFO] Deleting /Users/norman/Documents/workspace/netty-in-action/target 
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ 
netty-in-action --- 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered 
resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory 
/Users/norman/Documents/workspace/netty-in-action/src/main/resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ netty-in-
©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and 
other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. 
http://www.manning-sandbox.com/forum.jspa?forumID=857
33
action --- 
[INFO] Compiling 4 source files to /Users/norman/Documents/workspace/netty￾in-action/target/classes 
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) 
@ netty-in-action --- 
[INFO] skip non existing resourceDirectory 
/Users/norman/Documents/workspace/netty-in-action/src/test/resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ 
netty-in-action --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ netty-in-action 
--- 
[INFO] No tests to run. 
... 
------------------------------------------------------- 
 T E S T S 
------------------------------------------------------- 
There are no tests to run. 
Results : 
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 #1 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ netty-in-action --- 
[INFO] Building jar: /Users/norman/Documents/workspace/netty-in￾action/target/netty-in-action-0.1-SNAPSHOT.jar #2 
[INFO] ----------------------------------------------------------------------
-- 
[INFO] BUILD SUCCESS #3 
[INFO] ----------------------------------------------------------------------
-- 
[INFO] Total time: 4.344s 
[INFO] Finished at: Mon Oct 22 09:49:28 CEST 2012 
[INFO] Final Memory: 13M/118M 
[INFO] ----------------------------------------------------------------------
-- 
Normans-MacBook-Pro:netty-in-action norman$ 
#1 Unit tests report ran, failed, errors, or skipped 
#2 Java jar file created from echo client and server code and placed in location listed 
#3 Compilation successful; errors would report "build failed" 
Maven downloads all the libraries your code needs. In this case, only Netty is required, but for 
bigger projects there may be more dependencies. 
After the process completes youll have your final JAR file, which youll run in the next 
section. 
©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and 
other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. 
http://www.manning-sandbox.com/forum.jspa?forumID=857
2.5.2 Running the server and client 
So everything is compiled now and ready to be used. This is done via the java command. 
Youll need at least two console windows. The first console runs the server; the second runs the 
client. 
The following listing shows how the server is started. Note that I include the Netty jar and 
the generated jar (which holds your compiled code) with the classpath. This is needed or youd 
get a ClassNotFoundException. 
Listing 2.8 Starting the server 
Normans-MacBook-Pro:netty-in-action norman$ java -cp 
~/.m2/repository/io/netty/netty-all/4.0.0.Final/netty-all-4.0.0. 
Final.jar:target/netty-in-action-0.1-SNAPSHOT.jar 
com.manning.nettyinaction.chapter2.EchoServer 8080 
com.manning.nettyinaction.chapter2.EchoServer started and listen on 
/0:0:0:0:0:0:0:0:8080 
To stop the server, press Ctrl-C. 
Now the server is started and ready to accept data, which it processes and echoes back to 
the client. To test this, Ill start up the client, which does the following: 
 Connects to the server. 
 Writes data to the server. 
 Waits to receive data. 
 Shuts down the server . 
Again, use the java executable to start up the client, with the classpath set up correctly as 
shown in the following listing. 
Listing 2.9 Starting the client 
Normans-MacBook-Pro:netty-in-action norman$ java -cp 
~/.m2/repository/io/netty/netty-all/4.0.0.Final/netty-all-
4.0.0.Final.jar:target/netty-in-action-0.1-SNAPSHOT.jar 
com.manning.nettyinaction.chapter2.EchoClient 127.0.0.1 8080 
Client received: 4e6574747920726f636b7321 
Note that the client starts up and prints out a single log statement to STDOUT. This shows the 
HEX of the received data. After the HEX prints, it closes the connection and terminates itself 
without any further interaction. 
Every time you start the client, youll see one log statement in the console that the server is 
running in: 
Server received: 4e6574747920726f636b7321 
©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and 
other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. 
http://www.manning-sandbox.com/forum.jspa?forumID=857
34
As you can see, the logged HEX matches what was logged on the client, which verifies that 
the application works as expected; the server writes exactly the same data to the client as it 
received. 
The last scenario to test is what happens if the server isnt started but a client tries to 
connect to the server. First, stop the server with the Ctrl-C shortcut, as described previously. 
Once youre sure its not running anymore, start the client again. The following listing shows 
the output of the client when the server is no longer running. 
Listing 2.10 Exception in client 
Normans-MacBook-Pro:netty-in-action norman$ java -cp 
~/.m2/repository/io/netty/netty-all/4.0.0.Final/netty-all-
4.0.0.Final.jar:target/netty-in-action-0.1-SNAPSHOT.jar 
com.manning.nettyinaction.chapter2.EchoClient 127.0.0.1 8080 
java.net.ConnectException: Connection refused 
 at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) 
 at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:692) 
 at 
io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel
.java:177) #1
 at 
io.netty.channel.socket.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnec
t(AbstractNioChannel.java:201) 
 at 
io.netty.channel.socket.nio.NioEventLoop.processSelectedKeys(NioEventLoop.jav
a:284) 
 at io.netty.channel.socket.nio.NioEventLoop.run(NioEventLoop.java:211) 
 at 
io.netty.channel.SingleThreadEventExecutor$1.run(SingleThreadEventExecutor.ja
va:80) 
 at java.lang.Thread.run(Thread.java:722) 
Exception in thread "main" io.netty.channel.ChannelException: 
java.net.ConnectException: Connection refused #2
 at 
io.netty.channel.DefaultChannelFuture.rethrowIfFailed(DefaultChannelFuture.ja
va:226) 
 at 
io.netty.channel.DefaultChannelFuture.sync(DefaultChannelFuture.java:175) 
 at 
com.manning.nettyinaction.chapter2.EchoClient.start(EchoClient.java:37) 
 at #3
com.manning.nettyinaction.chapter2.EchoClient.main(EchoClient.java:56) 
Caused by: java.net.ConnectException: Connection refused 
 at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) 
 at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:692) 
 at 
io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel
.java:177) 
 at 
io.netty.channel.socket.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnec
t(AbstractNioChannel.java:201) 
 at 
io.netty.channel.socket.nio.NioEventLoop.processSelectedKeys(NioEventLoop.jav
a:284) 
©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and 
other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. 
http://www.manning-sandbox.com/forum.jspa?forumID=857
35
36
 at io.netty.channel.socket.nio.NioEventLoop.run(NioEventLoop.java:211) 
 at 
io.netty.channel.SingleThreadEventExecutor$1.run(SingleThreadEventExecutor.ja
va:80) 
 at java.lang.Thread.run(Thread.java:722) 
#1 Client connect fails; even on different thread Netty propagates exception 
#2 Exception traceable back to client example 
#3 Specific exception propagated and not wrapped. ConnectException could be handled differently in 
client handler to support features such as automatic reconnection 
What happened? The client tried to connect to the server, which it expected to run on 
127.0.0.1:8080. This failed (as expected) because the server was stopped previously, 
triggering a java.net.ConnectException on the client side. The exception then triggered 
the exceptionCaught(Ö) method of the EchoClientHandler. This prints out the stack 
trace and closes the channel, which is exactly how it was implemented in listing 2.3. 
Youve now essentially built a client and server which can handle concurrency in a simpler 
way. This setup is capable of scaling to several thousand concurrent users and handles far 
more messages per second than a vanilla Java can handle. In later chapters, youll see and 
appreciate why and how Nettys approach makes scaling and threading easier. Youll also see 
that by allowing for a separation of concerns and hooking into the data lifecycle, Netty creates 
an extensible environment in which its easy to implement improvements and changes as the 
business requirements change.

最后更新于

这有帮助吗?