Jetserver Open Source Project Learning (eight)--Decoding encoder usage

Source: Internet
Author: User

I. Eventencoder and Eventdecoder

The event encoder receives an event object and transforms it into a Channelbuffer object. The encoder first reads the event type and acts as the opcode (the first byte of the buffer), then reads the event body and transforms it into a Channelbuffer object as the body of the message.

/*** A Simple Event encoder would receive an incoming event, and convert it to A * {@linkChannelbuffer}.  It would read the event type and put it as the * opcode (i.e first byte of the buffer), then it would read the event body and * Put convert to channelbuffer if necessary and put it as the body of the * message. *  * @authorAbraham Menacherry **/@Sharable Public classEventencoderextendsonetooneencoder{Private Static FinalLogger LOG =loggerfactory. GetLogger (Eventdecoder.class); @OverrideprotectedObject Encode (Channelhandlercontext CTX, channel Channel, Object msg)throwsException {if(NULL==msg) {Log.error ("Received null message in Eventencoder"); returnmsg; } Event Event=(Event) msg; Channelbuffer opcode= Channelbuffers.buffer (1);        Opcode.writebyte (Event.gettype ()); Channelbuffer Buffer=NULL; if(NULL!=Event.getsource ()) {Channelbuffer Data=(Channelbuffer) Event.getsource (); Buffer=channelbuffers.wrappedbuffer (opcode, data); }        Else{buffer=opcode; }        returnbuffer; }}

Jetserver Open Source Project Learning (eight)--Decoding encoder usage

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.