users@grizzly.java.net

Re: Project Grizzly Meeting Agenda (September 2, 2009)

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Thu, 03 Sep 2009 14:48:35 +0200

Hi Sendhil,

> As discussed in the call,
>
> - Is it possible to have a separate thread pool for Servlet
> Container. The
> reason being any malformed request or longer running request on the
> servlet
> could potentially block the incoming connections because one common
> thread
> pool is used. I would even prefer to have a separate thread pool per
> servlet
> if possible
Grizzly processes I/O tasks using worker thread pool, which could be
configured following way:

SelectorThread.setThreadPool(ExecutorService);
if you want to provide own thread pool implementation. But note, that
thread pool should construct threads, which are subclasses of
HttpWorkerThread.

If you want to use standard Grizzly threadpool implementation, but
just adjust min/max threads count, you can use following methods:
SelectorThread.setCoreThreads(int);
SelectorThread.setMaxThreads(int);


by default Grizzly autoconfigures thread pool size according to number
of CPUs, but it's mostly useful for development, not for production.

Regarding custom thread pool per Servlet. I think it's what you will
need to create yourself. Like:

     public class HelloServlet extends HttpServlet {

         private ExecutorService customThreadPool;
         @Override
         public void init() throws ServletException {
             customThreadPool = Executors.newCachedThreadPool();
         }

         /**
          * Implements the HTTP GET method. The GET method is the
standard
          * browser method.
          *
          * @param request the request object, containing data from
the browser
          * @param repsonse the response object to send data to the
browser
          */
         public void doGet(HttpServletRequest request,
                 HttpServletResponse response)
                 throws ServletException, IOException {

             ---> Here postpone request processing using either Comet
or ARP

             // And execute long task
             customThreadPool.execute(new LongComplexTask(request,
response));
         }
     }
}


> - I wan to recieve the request on Consumer servlet and dispatch the
> request
> to another thread (separate thread pool) and process the request
> asynchronously without blocking the consumer thread. There are 2
> options
> discussed: ARP & Comet. Could you please provide some examples on
> ARP &Comet
I'd recommend to use Comet here, as its API is much more simple.
Ok, let's continue with servlet written above...

Here is updated servlet's init method, which initializes CometContext,
which we will need in order to control asynchronous request processing:

     private ExecutorService customThreadPool;
     private String contextPath;

     @Override
     public void init(ServletConfig config) throws ServletException {
        // Initialize custom thread pool we will need to execute long complex
tasks
         customThreadPool = Executors.newCachedThreadPool();

         ServletContext context = config.getServletContext();
         contextPath = context.getContextPath() + "/hello";

         CometEngine engine = CometEngine.getEngine();

         // Register CometContext, which is associated with some path
         CometContext cometContext = engine.register(contextPath);

         // Set timeout value for async operations in milliseconds
         cometContext.setExpirationDelay(30 * 1000);
     }

So code above initializes Comet infrastructure, which will be
associated with the current servlet.

Next step is how to postpone the processing of servlet request, using
Comet? Here is example:

     public void doGet(HttpServletRequest request,
             HttpServletResponse response)
             throws ServletException, IOException {

         final CometEngine engine = CometEngine.getEngine();
         // Get CometContext associated with the servlet
         final CometContext context =
engine.getCometContext(contextPath);

         // Create CometHandler, which will be notified about
asynchronous operation progress
         final HelloCometHandler handler = new HelloCometHandler();
         handler.attach(new Context(request, response));

         // Register new CometHandler
         context.addCometHandler(handler);

         // Execute long complex task
         customThreadPool.execute(new LongComplexTask(handler));
     }

by registering CometHandler we postpone the request and then release
current worker thread. To resume the request processing - we will need
to notify CometHandler. Here is the code of LongComplexTask, which
includes CometHandler notificiation.

     public class LongComplexTask implements Runnable {
         final CometHandler handler;

         public LongComplexTask(CometHandler handler) {
             this.handler = handler;
         }

         public void run() {
             // emulate long task
             try {
                 Thread.sleep(5000);
             } catch (Exception e) {}


             final CometEngine engine = CometEngine.getEngine();
             // Get CometContext
             final CometContext context =
engine.getCometContext(contextPath);
             try {
                 // Notify CometHandler about task completion
                 context.notify("DONE", handler);
             } catch (IOException e) {
                 context.removeCometHandler(handler);
             }
         }
     }


After being notified, CometHandler should form a response and send it
back to a client. Here is example snippet from HelloCometHandler:

     public void onEvent(CometEvent event) throws IOException {
         if (CometEvent.NOTIFY == event.getType()) {
             final HttpServletResponse response = context.getResponse();

             PrintWriter writer = response.getWriter();
             writer.write("Long task is completed");
             writer.flush();

             event.getCometContext().resumeCometHandler(this);
         }
     }


That's actually the story :)

Additionally I'm attaching working HelloServlet example, where you can
find complete example's source code and find how you should initialize
and start GrizzlyWebContainer.
If you'll have any other question - please let us know..

WBR,
Alexey.







>
> - Provide some pointers on integrating JAX-WS servlet into Grizzly
>
> Thanks
> Sendhil
>
>
> Oleksiy Stashok wrote:
>>
>> Agenda:
>>
>> + Grizzly 1.9.x updates: 1.9.18 release
>> + Grizzly servlet container
>> + Grizzly 2.0 updates
>> + round table
>>
>>
>> Logistics:
>> Time: 10:00am Pacific Time, Noon Central Time, 13:00 Eastern Time,
>> 19:00 CET
>> Toll free: 866.692.3163
>> International: 1.203.418.3122
>> Access code: 6454223
>>
>> Every one is welcome!
>>
>> WBR,
>> Alexey.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Project-Grizzly-Meeting-Agenda-%28September-2%2C-2009%29-tp25259401p25262287.html
> Sent from the Grizzly - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>