Some nwbie question - how to integrate Netty into WebApp

Frederic Bregier fredbregier at free.fr
Sat Sep 12 03:27:37 EDT 2009


Hi Olek,

I think the handler in the pipeline should be HttpMessageEncoder.
It takes care about HttpMessage and HttpChunk.
Also, Http protocol defines that last chunk must be empty:

DefaultHttpChunk lastchunk = new
DefaultHttpChunk(ChannelBuffers.EMPTY_BUFFER);
e.getChannel().write(lastchunk);

ChunkedWriteHandler is not about HttpChunk but about a facility in Netty to
send data chunk by chunk but not necesseraly HttpChunk (the object must obey
the ChunkedInput interface). It is another option to send HttpChunk as shown
in the File example for http.
Here is a small part of the factory:
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("handler", new HttpStaticFileServerHandler());

and the code does:
long fileLength = raf.length();
HttpResponse response = new DefaultHttpResponse(
   HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setHeader(
   HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(fileLength));
Channel ch = e.getChannel();
// Write the initial line and the header.
ch.write(response);
// Write the content.
ChannelFuture writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength,
8192));

See the org.jboss.netty.example.http.file for the complete example.

HTH,
Frederic


olekg wrote:
> 
> 
> 
> Frederic Bregier wrote:
>> 
>> Hi,
>> 
>> To send a response in Http, you have two modes, chunked (one message
>> split in several pieces) and not chunked (1 piece only). The difference
>> must be set in the first header (Transfer-Encoding to chunked or not
>> present).
>> 
>> So if you want to send by chunk, you have to create the first response
>> with the TransferEncoding set in the header to chunked and with no data
>> in the body (no channelBuffer attached to the first response).
>> You then write this answer to the connected channel.
>> 
>> Then you can send as many chunks as you want by creating each time your
>> own HttpChunk (DefaultHttpChunk) setting the next ChannelBuffer each
>> time. Again, write all HttpChunk to the connected channel.
>> 
>> The last written chunk must be an empty one (ChannelBuffer is an empty
>> one, not null).
>> 
>> Then after the last chunk, you can close the connection if you want.
>> 
>> If possible, the first response should contains a Content-Length header
>> value (if known).
>> 
>> HTH,
>> Frederic
>> 
> 
> 
> Hello Frederic,
> 
> Thank you for you reply. I have already created something you described :
> 
> //only headres in response object
> e.getChannel().write(response);
> 
> //portions of data as chunks every 20 seconds (to simulate Tomcat sample
> that works)
> DefaultHttpChunk chunk = new DefaultHttpChunk( buf);
> 
>         for( int i = 0; i < 11; i++) {
>         	e.getChannel().write(chunk);
>         	try {
> 				Thread.sleep(10000);
> 			} catch (InterruptedException e1) {
> 				// TODO Auto-generated catch block
> 				e1.printStackTrace();
> 			}
>         }
> 
> 
> I also set in HttpSefrverPipelineFactory :
> 
> pipeline.addLast("streamer", new ChunkedWriteHandler());
> 
> Unfortunately it does not work, however the data that are sent are almost
> (I cannot find the difference) the same as Tomcat sample sends. It is
> JavaScript injection into  IFrame. I can see the request/response data
> flow in tcpmon (a tool from Axis project) or with Firebug plugin in
> Firefox :
> 
> It looks like :
> 
> HTTP/1.1 200 OK
> Transfer-Encoding: chunked
> 
> 35
> <script>parent.printTime("Hello its Netty");</script>
> 35
> <script>parent.printTime("Hello its Netty");</script>
> 
> The Tomcat sample works while this one with Netty - not.
> 
> regards Olek
> 


-----
Hardware/Software Architect
-- 
View this message in context: http://n2.nabble.com/Some-nwbie-question-how-to-integrate-Netty-into-WebApp-tp3617235p3630805.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list