users@concurrency-ee-spec.java.net

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

From: David M. Lloyd <david.lloyd_at_redhat.com>
Date: Mon, 11 Feb 2013 19:30:38 -0600

On 01/22/2013 01:06 PM, Anthony Lai wrote:
> 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.

I tend to disagree with this API. Why not just create a new Runnable
like this:

mes.submit(new Runnable() {
     void run() {
         addWhateverContextIsNeeded();
         myRunnableInstance.run();
     }
});

We're adding a clunky API facility for something that can seemingly be
accomplished by basic Java. Unless there's a compelling use case, I
think this API should be removed.
-- 
- DML