users@grizzly.java.net

Re: Question on how to design statless filters

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Wed, 21 May 2014 13:41:43 -0700

Oh, jusr realized it's HTTP server example, but still it might be useful
sample for you, on client side the code will look similarly, except that
you don't need to send response :)

WBR,
Alexey.

On 21.05.14 13:35, Oleksiy Stashok wrote:
> Hey Johan,
>
> On 21.05.14 05:28, Johan Maasing wrote:
>> I'm going through the tutorials (for 2.3.12) making a simple HTTP
>> client. I followed this
>> https://grizzly.java.net/httpframework.html#/Samples which works fine.
>>
>> However I get a bit stuck on how to modify the sample to make the
>> filter stateless.
>> My use case is that I will have many threads making the same type of
>> HTTP-requests to the same end-point. So I guess that I should set up
>> the filter chain and start the transport once as a singleton. Then I
>> use transport.connect and transport.write in each "client" thread.
>> Will there ever be the case that my filter will be called several
>> time for the same response - I'm thinking about how/where to
>> accumulate in memory the entire response body if that is the case?
> I think here is the Filter sample, which may help you:
>
> /new BaseFilter() {//
> // @Override//
> // public NextAction handleRead(FilterChainContext ctx)
> throws IOException {//
> // final HttpContent requestContent = ctx.getMessage();//
> // if (!requestContent.isLast()) { // if it's not the
> last content chunk - keep accumulating the response content//
> // return ctx.getStopAction(requestContent);//
> // }//
> ////
> // HttpRequestPacket request = (HttpRequestPacket)
> requestContent.getHttpHeader();//
> // HttpResponsePacket response = request.getResponse();//
> //
> // final Buffer payload =
> requestContent.getContent();// // retrieve the entire response payload
> ////
> // HttpContent responseContent =
> response.httpContentBuilder().//
> //content(payload).last(requestContent.isLast()).build(); // build the
> response//
> // ctx.write(responseContent); // send the response
> back on the same connection//
> // return ctx.getStopAction();//
> // }/
>
>
> Please let me know if you have more questions.
>
> WBR,
> Alexey.
>
>
>>
>> How do I return the response to the calling thread? In the sample the
>> filter gets a future from the caller in the constructor to return the
>> result to the caller. But this won't work if I set up the chain once
>> and it is shared between threads.
>>
>> Any pointers are appreciated.
>>
>> Cheers,
>> JM
>