One, the HTTP decoder may parse an HTTP request into multiple message objects.
Ch.pipeline (). AddLast (new Httpservercodec ()); Ch.pipeline (). AddLast (New Parserequesthandler ());
After Httpservercodec decoding, an HTTP request causes: Parserequesthandler's Channelread () method call multiple times (test-time "received Message "Output two times)
@Override publicvoid channelread (channelhandlercontext ctx, Object msg) throws Exception { System.out.println ("received message");
You can use Httpobjectaggregator to convert multiple messages to a single fullhttprequest, as follows:
Ch.pipeline (). AddLast (new Httpservercodec ()); Ch.pipeline (). AddLast (new Httpobjectaggregator(65536)); Ch.pipeline (). AddLast (new Parserequesthandler ());
At this point, an HTTP message (Object msg) is the following.
Content:compositebytebuf (ridx:0, Widx:17, Cap:17, Components=1)) post/http/1.1host:127.0.0.1:8888user-agent:mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) gecko/20100101 firefox/46.0accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q= 0.8accept-language:nullaccept-encoding:gzip, deflatecontent-type:application/x-www-form-urlencoded Content-length:17Cookie: _ga=ga1.1.457486782.1446782739connection:keep-alive
As can be seen from the above, the Entity header field Content-length is 17, indicating that the entity body has 17 bytes.
And the message I sent is this:
HTTP POST request, the request body is JSON-formatted data. The JSON string parsed by Json-lib is used here. The code is as follows:
// parse Job type 0,1 private String Getjobtype (fullhttprequest request) throws ioexception{bytebuf jsonbuf = Span style= "color: #000000;" > Request.content (); String jsonstr = jsonbuf.tostring (charsetutil.utf_8); Jsonobject jsonobj = Jsonobject.fromobject (JSONSTR); String JobType = jsonobj.getstring ("JobType" ); return JobType; }
It is important to note that when parsing a JSON string using Json-lib, additional dependency packages are required:
After parsing is complete, the processed results need to be sent to the next channelhandler for the next step.
@Override Public void Channelread (Channelhandlercontext ctx, Object msg) throws Exception { //dosome process ..... ctx.firechannelread (Job);}
Note that the Firechannelread () method is used instead of the Ctx.writeandflush (...). Because, Writeandflush/write is outbound, it sends the message to the previous handler, which is then sent to the remote peer, and here is the inbound. Specific reference:
Here, through ctx.firechannelread (Job), the processed results are sent to the next channel processing.
Ch.pipeline (). AddLast (new Httpservercodec ()); Ch.pipeline (). AddLast (New Httpobjectaggregator (2048)); Ch.pipeline (). AddLast (new Parserequesthandler ()); Ch.pipeline (). AddLast (new Oozierequesthandler ());
The next Handler is Oozierequesthandler, which submits the job to Oozie server and then returns Jobid to the client (Httpservercodec Handler is responsible for the underlying transfer details).
Netty constructs an HTTP response in the following way:
String jobId =new defaultfullhttpresponse ( httpversion.http_1_1, Httpresponsestatus.ok, "Application/xml"); Response.headers (). Setint (Content_length, response.content (). Readablebytes ()); Ctx.write (response). AddListener (channelfuturelistener.close);
Netty One of the learning notes (Netty parsing a simple HTTP Post Json request)