The delivery of Android Netty-based message push scheme objects (iv)

Source: Internet
Author: User

In the previous article, "Android Netty-based message push scheme string receive and send (iii)" We introduced the Netty string transfer, we know that Netty message delivery is based on the flow, passed through Channelbuffer, then natural, object also needs to be converted into Channelbuffer to pass. Fortunately, Netty itself has written such a conversion tool for us. Objectencoder and Objectdecoder, let's introduce a case.

1. We construct an object for transmission (JavaBean)

[Java]View Plaincopy
  1. @SuppressWarnings ("serial")
  2. Public class Command implements Serializable {
  3. private String ActionName;
  4. Public String Getactionname () {
  5. return actionname;
  6. }
  7. public void Setactionname (String actionname) {
  8. this.actionname = ActionName;
  9. }
  10. }

2. Let's look at the client code first

[Java]View Plaincopy
  1. Public class Objectclient {
  2. public static void Main (String args[]) {
  3. Clientbootstrap bootstrap = new Clientbootstrap (new Nioclientsocketchannelfactory (  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
  4. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  5. @Override
  6. Public channelpipeline Getpipeline () throws Exception {
  7. return Channels.pipeline (new Objectencoder (), new Objectclienthandler ());
  8. }
  9. });
  10. Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8000));
  11. }
  12. }
  13. Class Objectclienthandler extends Simplechannelhandler {
  14. /** 
  15. * When the server is bound to trigger, send messages to the server.
  16. */
  17. @Override
  18. public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
  19. //Send object information to the server
  20. SendObject (E.getchannel ());
  21. }
  22. /** 
  23. * Send Object
  24. * @param Channel
  25. */
  26. private void SendObject (Channel channel) {
  27. Command command = new command ();
  28. Command.setactionname ("Hello action.");
  29. Channel.write (command);
  30. }
  31. }

3. Take a look at the service-side code

[Java]View Plaincopy
  1. Public class Objectserver {
  2. public static void Main (String args[]) {
  3. //Server service Initiator
  4. Serverbootstrap bootstrap = new Serverbootstrap (new Nioserversocketchannelfactory (  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
  5. //Set up a class to handle client messages and various message events (Handler)
  6. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  7. @Override
  8. Public channelpipeline Getpipeline () throws Exception {
  9. //Encode--and post-process your own business
  10. return Channels.pipeline (new Objectdecoder (classresolvers.cachedisabled) (This.getclass ().  getClassLoader ())), new Objectserverhandler ());
  11. }
  12. });
  13. Bootstrap.bind (new inetsocketaddress (8000));
  14. }
  15. }
  16. Class Objectserverhandler extends Simplechannelhandler {
  17. /** 
  18. * Triggered when a message is received
  19. */
  20. @Override
  21. public void messagereceived (Channelhandlercontext ctx, messageevent e) throws Exception {
  22. command command = (command) e.getmessage ();
  23. //Print to see if it's the one we just passed over.
  24. System.out.println (Command.getactionname ());
  25. }
  26. }

Run the server first, run the client, and then print the following string in the console of the server

[Java]View Plaincopy
    1. Hello action.

The delivery of Android Netty-based message push scheme objects (iv)

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.