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
- @SuppressWarnings ("serial")
- Public class Command implements Serializable {
- private String ActionName;
- Public String Getactionname () {
- return actionname;
- }
- public void Setactionname (String actionname) {
- this.actionname = ActionName;
- }
- }
2. Let's look at the client code first
[Java]View Plaincopy
- Public class Objectclient {
- public static void Main (String args[]) {
- Clientbootstrap bootstrap = new Clientbootstrap (new Nioclientsocketchannelfactory ( Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
- Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
- @Override
- Public channelpipeline Getpipeline () throws Exception {
- return Channels.pipeline (new Objectencoder (), new Objectclienthandler ());
- }
- });
- Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8000));
- }
- }
- Class Objectclienthandler extends Simplechannelhandler {
- /**
- * When the server is bound to trigger, send messages to the server.
- */
- @Override
- public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
- //Send object information to the server
- SendObject (E.getchannel ());
- }
- /**
- * Send Object
- * @param Channel
- */
- private void SendObject (Channel channel) {
- Command command = new command ();
- Command.setactionname ("Hello action.");
- Channel.write (command);
- }
- }
3. Take a look at the service-side code
[Java]View Plaincopy
- Public class Objectserver {
- public static void Main (String args[]) {
- //Server service Initiator
- Serverbootstrap bootstrap = new Serverbootstrap (new Nioserversocketchannelfactory ( Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
- //Set up a class to handle client messages and various message events (Handler)
- Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
- @Override
- Public channelpipeline Getpipeline () throws Exception {
- //Encode--and post-process your own business
- return Channels.pipeline (new Objectdecoder (classresolvers.cachedisabled) (This.getclass (). getClassLoader ())), new Objectserverhandler ());
- }
- });
- Bootstrap.bind (new inetsocketaddress (8000));
- }
- }
- Class Objectserverhandler extends Simplechannelhandler {
- /**
- * Triggered when a message is received
- */
- @Override
- public void messagereceived (Channelhandlercontext ctx, messageevent e) throws Exception {
- command command = (command) e.getmessage ();
- //Print to see if it's the one we just passed over.
- System.out.println (Command.getactionname ());
- }
- }
Run the server first, run the client, and then print the following string in the console of the server
[Java]View Plaincopy
- Hello action.
The delivery of Android Netty-based message push scheme objects (iv)