Grizzly echo server development practices
Chszs, which must be noted for reprinting. Blog homepage: http://blog.csdn.net/chszs
It is difficult to write Scalable Server Applications in Java. Using Java NiO for development, thread management, and Server Extension for thousands of users are all difficulties. The Grizzly NiO framework is designed to help developers make good use of Java NiO APIs to compile highly scalable and powerful servers and improve the extended framework components: web Framework (HTTP/S), websocket, and comet.
Example of grizzly 2.3 echo server/client development
1. Download The grizzly-framework.jar Library
Maven dependency
<dependency> <groupId>org.glassfish.grizzly</groupId> <artifactId>grizzly-framework</artifactId> <version>2.3.16</version></dependency>
Or: http://central.maven.org/maven2/org/glassfish/grizzly/grizzly-framework/2.3.16/grizzly-framework-2.3.16.jar
Version 2.3.17 was just released on January 23,. For more information, see:
Https://maven.java.net/content/repositories/releases/org/glassfish/grizzly/grizzly-framework/2.3.17/grizzly-framework-2.3.17.jar
You can also use this version.
Server:
1) Create an echo Filter
The echo filter returns the received message (regardless of its type) to the grizzly connection.
Import Java. io. ioexception; import Org. glassfish. grizzly. filterchain. basefilter; import Org. glassfish. grizzly. filterchain. filterchaincontext; import Org. glassfish. grizzly. filterchain. nextaction; public class echofilter extends basefilter {/*** only processes read operations, when a message arrives, it processes * @ Param CTX processing context * @ return next action */@ overridepublic nextaction handleread (filterchaincontext CTX) throws ioexception {// peer address is used for connectionless UDP connections. Final object peeraddress = CTX. getaddress (); final object message = CTX. getmessage (); system. out. println ("server received ed:" + message); CTX. write (peeraddress, message, null); Return CTX. getstopaction ();}}
2) server initialization code
All server filter chains are ready to initialize and start the server.
Import Java. io. ioexception; import Java. NIO. charset. charset; import Java. util. logging. logger; import Org. glassfish. grizzly. filterchain. filterchainbuilder; import Org. glassfish. grizzly. filterchain. transportfilter; import Org. glassfish. grizzly. NIO. transport. tcpniotransport; import Org. glassfish. grizzly. NIO. transport. tcpniotransportbuilder; import Org. glassfish. grizzly. utils. stringfilter; public class echoserver {Private Static final logger = logger. getlogger (echoserver. class. getname (); public static final string host = "localhost"; public static final int Port = 7777; public static void main (string [] ARGs) throws ioexception {// use filterchainbuilder to create a filter chain filterchainbuilder = filterchainbuilder. stateless (); // adds the transportfilter, which reads data from the connection and writes data to the connection filterchainbuilder. add (New transportfilter (); // string filter stringfilter is responsible for the conversion between the buffer and the string filterchainbuilder. add (New stringfilter (charset. forname ("UTF-8"); // The filter echofilter is responsible for returning the received message to the connection filterchainbuilder as is. add (New echofilter (); // create a TCP transmission final tcpniotransport transport = tcpniotransportbuilder. newinstance (). build (); transport. setprocessor (filterchainbuilder. build (); try {// bind transmission, start to listen to the host + port transport. BIND (host, Port); // start transmission transport. start (); logger.info ("press any key to stop the echo server... "); system. in. read ();} finally {logger.info ("stopping transport... "); // stop the transport server transport. shutdown (); logger.info ("stopped transport... ");}}}
Run echo server:
java -classpath grizzly-framework.jar EchoServer
Client:
1) create a client Filter
The client filter redirects the server's response to the standard output. Note that the filterchaincontext message is required for the client filter.
Import Java. io. ioexception; import Org. glassfish. grizzly. filterchain. basefilter; import Org. glassfish. grizzly. filterchain. filterchaincontext; import Org. glassfish. grizzly. filterchain. nextaction; public class clientfilter extends basefilter {/*** only processes read operations, when a message arrives, it processes the * @ Param CTX processing context * @ return next action */@ overridepublic nextaction handleread (final filterchaincontext CTX) throws ioexception {// obtain a string message from the context, the filter chain only uses the string filter stringfilterfinal string serverresponse = CTX. getmessage (); system. out. println ("server ECHO:" + serverresponse); Return CTX. getstopaction ();}}
2) client code
A simple client sends messages to the echo server and waits for a response.
Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. NIO. charset. charset; import Java. util. concurrent. executionexception; import Java. util. concurrent. future; import Java. util. concurrent. timeunit; import Java. util. concurrent. timeoutexception; import Java. util. logging. logger; import Org. glassfish. grizzly. connection; import Org. glassfish. grizzly. grizzly; import Org. glassfish. grizzly. filterchain. filterchainbuilder; import Org. glassfish. grizzly. filterchain. transportfilter; import Org. glassfish. grizzly. NIO. transport. tcpniotransport; import Org. glassfish. grizzly. NIO. transport. tcpniotransportbuilder; import Org. glassfish. grizzly. utils. stringfilter; import ch. echo. server. echoserver; public class echoclient {Private Static final logger = grizzly. logger (echocl Ient. class); public static void main (string [] ARGs) throws ioexception, executionexception, timeout, timeoutexception {connection = NULL; // use the filterchainbuilder class to create a filter chain filterchainbuilder = filterchainbuilder. stateless (); // adds a transfer filter, which reads data from the connection and writes data to the connection filterchainbuilder. add (New transportfilter (); // Add a string filter, which is responsible for the conversion between the buffer and the string filterchainbuilder. add (New stringfilter (Cha Rset. forname ("UTF-8"); // Add the client filter, which redirects the server response to the standard output filterchainbuilder. add (New clientfilter (); // create a TCP transmission final tcpniotransport transport = tcpniotransportbuilder. newinstance (). build (); transport. setprocessor (filterchainbuilder. build (); try {// start transmission transport. start (); // asynchronous execution, connected to the server future <connection> future = transport. connect (echoserver. host, echoserver. port); // wait until the connection operation is complete. Connection = Future. get (10, T Imeunit. Seconds); Assert connection! = NULL; system. out. println ("ready... (\ "Q \" to exit "); Final bufferedreader inreader = new bufferedreader (New inputstreamreader (system. in); do {final string userinput = inreader. readline (); If (userinput = NULL | "Q ". equals (userinput) break; connection. write (userinput);} while (true);} finally {// close the client connection if (connection! = NULL) connection. Close (); // stop transmission transport. shutdownnow ();}}}
Run the echo client:
java -classpath grizzly-framework.jar EchoClient
It has passed the test and the program runs perfectly.
Grizzly echo server development practices