users@concurrency-ee-spec.java.net

[jsr236-spec users] [jsr236-experts] New ContextService API proposal from JSR 359 spec lead

From: Anthony Lai <anthony.lai_at_oracle.com>
Date: Tue, 22 Jan 2013 11:06:52 -0800

Dear Experts,

Binod, the spec lead for JSR 359 - SIP Servlet 2.0, is proposing a new
API in ContextService to pass in application-provided information that
can be retrieved in the thread that executes the task.

Forwarding Binod's suggestion:

<quote>
It is possible for some containers to build contextual object based
on application provided information. For example, a container could support
thread safe execution of the tasks based on a specific contextual
information.

| <T> T| |*createContextualProxy
<imap://binod%2Epg%40oracle%2Ecom_at_stbeehive.oracle.com:993/fetch%3EUID%3E/INBOX%3E44873?header=quotebody&part=1.2.2&filename=ContextService.html>*(T instance,
Properties
<http://download.oracle.com/javase/6/docs/api/java/util/Properties.html?is-external=true>
executionProperties, Class
<http://download.oracle.com/javase/6/docs/api/java/lang/Class.html?is-external=true><T> intf,
ContextHint<C> hint)
||*Object getContextHint
<imap://binod%2Epg%40oracle%2Ecom_at_stbeehive.oracle.com:993/fetch%3EUID%3E/INBOX%3E44873?header=quotebody&part=1.2.3&filename=ContextService.html>*(Object
<http://download.oracle.com/javase/6/docs/api/java/lang/Object.html?is-external=true> contextObject)|


       /** Context hint information that contains the container specific object. **/
       interface ContextHint<C> {
           void setHint(C hint);
           C getHint();
       }


Example code:

  public class MyRunnable implements Runnable {
       public void run() {
           System.out.println("MyRunnable.run with Container Context available.");

            ContextService ctxService = (ContextService) ctx
            .lookup("java:comp/env/concurrent/ContainerSpecificContextService");
            ContainerSpecificContextObject object = ctxService.getContextHint(this);

       }
   }
   
   InitialContext ctx = new InitialContext();
  * *ManagedExecutorService mes = (ManagedExecutorService)
            ctx.lookup("java:comp/env/concurrent/ContainerThreadPool");
            
   ContextService ctxService = (ContextService) ctx
            .lookup("java:comp/env/concurrent/ContainerSpecificContextService");

   MyRunnable myRunnableInstance = ...;
  
   ContextHint<ContainerSpecificContextObject> hint = new MyContextHint<ContainerSpecificContextObject>(...);
   Runnable rProxy = ctxService.createContextualProxy(myRunnableInstance, Runnable.class, hint);

   Future f = mes.submit(rProxy);

</unquote>

Any comments are welcome.

Regards
Anthony