Prior to using the Mina Framework, the sense of efficiency is very good, using a long connection can support more than 100,000 times of concurrency.
Today's attempt to use the Netty framework, the feeling is also very convenient to use, specific efficiency issues, in the following blog will be explained in detail:
Nioserversocketchannelfactory creates a server-side serversocketchannel with multi-threaded execution of nonblocking io, and Mina design
mode, the reactor mode is used. Where Bossexecutor, Workerexecutor is a two thread pool, Bossexecutor is used to receive client connections, Workerexecutor is used to perform non-blocking IO operations, mainly read,write.
Java Code
- Package Netty;
- Import org.jboss.netty.bootstrap.ServerBootstrap;
- Import Org.jboss.netty.channel.ChannelFactory;
- Import Org.jboss.netty.channel.ChannelPipeline;
- Import Org.jboss.netty.channel.ChannelPipelineFactory;
- Import Org.jboss.netty.channel.Channels;
- Import Org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
- Import Org.jboss.netty. Handler.codec.string.StringDecoder;
- Import Org.jboss.netty.handler.codec.string.StringEncoder;
- Import java. Net. inetsocketaddress;
- Import java.util.concurrent.Executors;
- /**
- * Created by IntelliJ idea.
- * User:flychao88
- * date:12-6-6
- * Time: 10:14
- * To change this template use File | Settings | File Templates.
- */
- Public class Discardserver {
- public static void Main (string[] args) throws Exception {
- ChannelFactory factory = new Nioserversocketchannelfactory (
- Executors.newcachedthreadpool (),
- Executors.newcachedthreadpool ());
- Serverbootstrap bootstrap = new Serverbootstrap (Factory);
- bootstrap.setpipelinefactory (new Channelpipelinefactory () {
- Public Channelpipeline Getpipeline () {
- Channelpipeline pipeline = Channels.pipeline ();
- pipeline.addlast ("encode",new Stringencoder ());
- Pipeline.addlast ("Decode",new Stringdecoder ());
- Pipeline.addlast ("Handler",new Discardserverhandler ());
- return pipeline;
- }
- });
- Bootstrap. setOption ("Child.tcpnodelay", true);
- Bootstrap.setoption ("Child.keepalive", true);
- Bootstrap.bind (new inetsocketaddress (8080));
- }
- }
Java code
- Package Netty;
- Import Org.jboss.netty.buffer.ChannelBuffer;
- Import Org.jboss.netty.buffer.ChannelBuffers;
- Import org.jboss.netty.channel.*;
- /**
- * Created by IntelliJ idea.
- * User:flychao88
- * date:12-6-6
- * Time: 10:10
- * To change this template use File | Settings | File Templates.
- */
- Public class Discardserverhandler extends Simplechannelupstreamhandler {
- @Override
- public void messagereceived (Channelhandlercontext ctx, messageevent e) {
- SYSTEM.OUT.PRINTLN ("server receives 1:" +e.getmessage ());
- }
- @Override
- public void Exceptioncaught (Channelhandlercontext ctx, exceptionevent e) {
- E.getcause (). Printstacktrace ();
- Channel ch = e.getchannel ();
- Ch.close ();
- }
- }
Java code
- Package Netty;
- Import Org.jboss.netty.bootstrap.ClientBootstrap;
- Import Org.jboss.netty.channel.ChannelFactory;
- Import Org.jboss.netty.channel.ChannelPipeline;
- Import Org.jboss.netty.channel.ChannelPipelineFactory;
- Import Org.jboss.netty.channel.Channels;
- Import Org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
- Import Org.jboss.netty.handler.codec.string.StringDecoder;
- Import Org.jboss.netty.handler.codec.string.StringEncoder;
- Import java.net.InetSocketAddress;
- Import java.util.concurrent.Executors;
- /**
- * Created by IntelliJ idea.
- * User:flychao88
- * date:12-6-6
- * Time: 10:21
- * To change this template use File | Settings | File Templates.
- */
- Public class Timeclient {
- public static void Main (string[] args) throws Exception {
- ChannelFactory factory = new Nioclientsocketchannelfactory (
- Executors.newcachedthreadpool (),
- Executors.newcachedthreadpool ());
- Clientbootstrap bootstrap = new Clientbootstrap (Factory);
- Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
- Public Channelpipeline Getpipeline () {
- Channelpipeline pipeline = Channels.pipeline ();
- Pipeline.addlast ("encode",new Stringencoder ());
- Pipeline.addlast ("Decode",new Stringdecoder ());
- Pipeline.addlast ("Handler",new Timeclienthandler ());
- return pipeline;
- }
- });
- Bootstrap.setoption ("Tcpnodelay", true);
- Bootstrap.setoption ("KeepAlive", true);
- Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8080));
- }
- }
Java code
- Package Netty;
- /**
- * Created by IntelliJ idea.
- * User:flychao88
- * date:12-6-6
- * Time: 10:22
- * To change this template use File | Settings | File Templates.
- */
- Import Org.jboss.netty.buffer.ChannelBuffer;
- Import Org.jboss.netty.buffer.ChannelBuffers;
- Import org.jboss.netty.channel.*;
- Import Java.util.Date;
- Public class Timeclienthandler extends Simplechannelupstreamhandler {
- @Override
- public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
- E.getchannel (). Write ("ABCD");
- }
- @Override
- public void messagereceived (Channelhandlercontext ctx, messageevent e) {
- E.getchannel (). Close ();
- }
- @Override
- public void Exceptioncaught (Channelhandlercontext ctx, exceptionevent e) {
- E.getcause (). Printstacktrace ();
- E.getchannel (). Close ();
- }
Netty Java Call