Transmission of objects in the Netty-based message push solution of Android (IV)

Source: Internet
Author: User

Transmission of objects in the Netty-based message push solution of Android (IV)

In the previous article, "receiving and sending strings of the Netty-based message push solution for Android (3)", we introduced the string transmission of Netty, we know that Netty's message transmission is based on the stream and passed through ChannelBuffer. Naturally, the Object also needs to be converted to ChannelBuffer for transmission. Fortunately, Netty has already written such a conversion tool for us. ObjectEncoder and ObjectDecoder. The following is a case study.

1. We construct an object for transmission (JavaBean)

@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 take a look at the Client code first.

Public class ObjectClient {public static void main (String args []) {ClientBootstrap bootstrap = new ClientBootstrap (new NioClientSocketChannelFactory (Executors. newCachedThreadPool (), Executors. newCachedThreadPool (); bootstrap. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {return Channels. pipeline (new ObjectEncoder (), new ObjectClient Handler () ;}}); bootstrap. connect (new InetSocketAddress ("127.0.0.1", 8000);} class ObjectClientHandler extends SimpleChannelHandler {/*** send a message to the server when it is bound to the server. * // @ Overridepublic void channelConnected (ChannelHandlerContext ctx, ChannelStateEvent e) {// send the Object information sendObject (e. getChannel ();}/*** send Object * @ param channel */private void sendObject (Channel channel) {Command command = new Command (); command. setActionName ("Hello action. "); channel. write (command );}}
3. Check the server code again.

Public class ObjectServer {public static void main (String args []) {// Server service initiator ServerBootstrap bootstrap = new ServerBootstrap (new NioServerSocketChannelFactory (Executors. newCachedThreadPool (), Executors. newCachedThreadPool (); // sets a Handler bootstrap class for processing client messages and various message events. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {// encode it first --> process your 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 */@ Overridepublic void messageReceived (ChannelHandlerContext ctx, MessageEvent e) throws Exception {Command command = (Command) e. getMessage (); // print it to see if it is the System we just passed. out. println (command. getActionName ());}}
Run the server first, then the client, and then print the following string in the server console

Hello action.






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.