Netty Java Call

Source: Internet
Author: User
Tags jboss

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
  1. Package Netty;
  2. Import org.jboss.netty.bootstrap.ServerBootstrap;
  3. Import Org.jboss.netty.channel.ChannelFactory;
  4. Import Org.jboss.netty.channel.ChannelPipeline;
  5. Import Org.jboss.netty.channel.ChannelPipelineFactory;
  6. Import Org.jboss.netty.channel.Channels;
  7. Import Org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
  8. Import Org.jboss.netty.  Handler.codec.string.StringDecoder;
  9. Import Org.jboss.netty.handler.codec.string.StringEncoder;
  10. Import java. Net.  inetsocketaddress;
  11. Import java.util.concurrent.Executors;
  12. /**
  13. * Created by IntelliJ idea.
  14. * User:flychao88
  15. * date:12-6-6
  16. * Time: 10:14
  17. * To change this template use File | Settings | File Templates.
  18. */
  19. Public class Discardserver {
  20. public static void Main (string[] args) throws Exception {
  21. ChannelFactory factory = new Nioserversocketchannelfactory (
  22. Executors.newcachedthreadpool (),
  23. Executors.newcachedthreadpool ());
  24. Serverbootstrap bootstrap = new Serverbootstrap (Factory);
  25. bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  26. Public Channelpipeline Getpipeline () {
  27. Channelpipeline pipeline = Channels.pipeline ();
  28. pipeline.addlast ("encode",new Stringencoder ());
  29. Pipeline.addlast ("Decode",new Stringdecoder ());
  30. Pipeline.addlast ("Handler",new Discardserverhandler ());
  31. return pipeline;
  32. }
  33. });
  34. Bootstrap.  setOption ("Child.tcpnodelay", true);
  35. Bootstrap.setoption ("Child.keepalive", true);
  36. Bootstrap.bind (new inetsocketaddress (8080));
  37. }
  38. }



Java code
  1. Package Netty;
  2. Import Org.jboss.netty.buffer.ChannelBuffer;
  3. Import Org.jboss.netty.buffer.ChannelBuffers;
  4. Import org.jboss.netty.channel.*;
  5. /**
  6. * Created by IntelliJ idea.
  7. * User:flychao88
  8. * date:12-6-6
  9. * Time: 10:10
  10. * To change this template use File | Settings | File Templates.
  11. */
  12. Public class Discardserverhandler extends Simplechannelupstreamhandler {
  13. @Override
  14. public void messagereceived (Channelhandlercontext ctx, messageevent e) {
  15. SYSTEM.OUT.PRINTLN ("server receives 1:" +e.getmessage ());
  16. }
  17. @Override
  18. public void Exceptioncaught (Channelhandlercontext ctx, exceptionevent e) {
  19. E.getcause (). Printstacktrace ();
  20. Channel ch = e.getchannel ();
  21. Ch.close ();
  22. }
  23. }




Java code
  1. Package Netty;
  2. Import Org.jboss.netty.bootstrap.ClientBootstrap;
  3. Import Org.jboss.netty.channel.ChannelFactory;
  4. Import Org.jboss.netty.channel.ChannelPipeline;
  5. Import Org.jboss.netty.channel.ChannelPipelineFactory;
  6. Import Org.jboss.netty.channel.Channels;
  7. Import Org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
  8. Import Org.jboss.netty.handler.codec.string.StringDecoder;
  9. Import Org.jboss.netty.handler.codec.string.StringEncoder;
  10. Import java.net.InetSocketAddress;
  11. Import java.util.concurrent.Executors;
  12. /**
  13. * Created by IntelliJ idea.
  14. * User:flychao88
  15. * date:12-6-6
  16. * Time: 10:21
  17. * To change this template use File | Settings | File Templates.
  18. */
  19. Public class Timeclient {
  20. public static void Main (string[] args) throws Exception {
  21. ChannelFactory factory = new Nioclientsocketchannelfactory (
  22. Executors.newcachedthreadpool (),
  23. Executors.newcachedthreadpool ());
  24. Clientbootstrap bootstrap = new Clientbootstrap (Factory);
  25. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  26. Public Channelpipeline Getpipeline () {
  27. Channelpipeline pipeline = Channels.pipeline ();
  28. Pipeline.addlast ("encode",new Stringencoder ());
  29. Pipeline.addlast ("Decode",new Stringdecoder ());
  30. Pipeline.addlast ("Handler",new Timeclienthandler ());
  31. return pipeline;
  32. }
  33. });
  34. Bootstrap.setoption ("Tcpnodelay", true);
  35. Bootstrap.setoption ("KeepAlive", true);
  36. Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8080));
  37. }
  38. }




Java code
    1. Package Netty;
    2. /**
    3. * Created by IntelliJ idea.
    4. * User:flychao88
    5. * date:12-6-6
    6. * Time: 10:22
    7. * To change this template use File | Settings | File Templates.
    8. */
    9. Import Org.jboss.netty.buffer.ChannelBuffer;
    10. Import Org.jboss.netty.buffer.ChannelBuffers;
    11. Import org.jboss.netty.channel.*;
    12. Import Java.util.Date;
    13. Public class Timeclienthandler extends Simplechannelupstreamhandler {
    14. @Override
    15. public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
    16. E.getchannel (). Write ("ABCD");
    17. }
    18. @Override
    19. public void messagereceived (Channelhandlercontext ctx, messageevent e) {
    20. E.getchannel (). Close ();
    21. }
    22. @Override
    23. public void Exceptioncaught (Channelhandlercontext ctx, exceptionevent e) {
    24. E.getcause (). Printstacktrace ();
    25. E.getchannel (). Close ();
    26. }

Netty Java Call

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.