Top Description Methods
javax.enterprise.concurrent

public Interface ContextService

Imports
java.util.Map

The ContextService provides methods for creating dynamic proxy objects (as defined by java.lang.reflect.Proxy) with the addition of context typically associated with applications executing in a Java™ EE environment. Examples of such context are classloading, namespace, security, etc.

The proxy objects follow the same rules as defined for the java.lang.reflect.Proxy class with the following additions:

Since
1.0

Method Summary

Modifier and TypeMethod and Description
public <T> T

Returns:

a proxy for the input object that implements the specified interface.
createContextualProxy
(T
the instance of the object to proxy.
instance
,
Class<T>
the interface that the proxy should implement.
intf
)

Creates a new contextual object proxy for the input object instance.

public Object

Returns:

a proxy for the input object that implements all of the specified interfaces.
createContextualProxy
(Object
the instance of the object to proxy.
instance
,
Class<?>...
the interfaces that the proxy should implement.
interfaces
)

Creates a new contextual object proxy for the input object instance.

public <T> T

Returns:

a proxy for the input object that implements the specified interface.
createContextualProxy
(T
the instance of the object to proxy.
instance
,
Map<String, String>
the properties to use when creating and running the context object.
executionProperties
,
Class<T>
the interface that the proxy should implement.
intf
)

Creates a new contextual object proxy for the input object instance.

public Object

Returns:

a proxy for the input object that implements all of the specified interfaces.
createContextualProxy
(Object
the instance of the object to proxy.
instance
,
Map<String, String>
the properties to use when creating and running the context object.
executionProperties
,
Class<?>...
the interfaces that the proxy should implement.
interfaces
)

Creates a new contextual object proxy for the input object instance.

public Map<String, String>

Returns:

A copy of the current contextual object execution properties, or null if the contextualProxy is created without specifying any execution properties.
getExecutionProperties
(Object
the contextual proxy instance to retrieve the execution properties.
contextualProxy
)

Gets the current execution properties on the context proxy instance.

Method Detail

createContextualProxyback to summary
public <T> T createContextualProxy(T instance, Class<T> intf)

Creates a new contextual object proxy for the input object instance.

Each method invocation will have the context of the application component instance that created the contextual object proxy.

The contextual object is useful when developing or using Java™ SE threading mechanisms propagating events to other component instances.

If the application component that created the proxy is not started or deployed, all methods on reflected interfaces may throw an java.lang.IllegalStateException.

For example, to execute a Runnable which is contextualized with the creator's context using a Java™ SE ExecutorService:

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

 InitialContext ctx = new InitialContext();
 ThreadFactory threadFactory = (ThreadFactory) ctx
          .lookup("java:comp/env/concurrent/ThreadFactory");

 ContextService ctxService = (ContextService) ctx
          .lookup("java:comp/env/concurrent/ContextService");

 MyRunnable myRunnableInstance = ...;

 Runnable rProxy = ctxService.createContextualProxy(myRunnableInstance, Runnable.class);

 ExecutorService exSvc = Executors.newThreadPool(10, threadFactory);

 Future f = exSvc.submit(rProxy);

Parameters
instance:T

the instance of the object to proxy.

intf:Class<T>

the interface that the proxy should implement.

Returns:T

a proxy for the input object that implements the specified interface.

Exceptions
IllegalArgumentException:
- if the intf argument is null or the instance does not implement the specified interface.
createContextualProxyback to summary
public Object createContextualProxy(Object instance, Class<?>... interfaces)

Creates a new contextual object proxy for the input object instance.

This method is similar to <T> T createContextualProxy(T instance, Class<T> intf) except that this method can be used if the proxy has to support multiple interfaces.

Example:

 public class MyRunnableWork implements Runnable, SomeWorkInterface {
     public void run() {
         System.out.println("MyRunnableWork.run with Java EE Context available.");
     }
     public void someWorkInterfaceMethod() {
         ...
     }
 }

 ThreadFactory threadFactory = ...;

 ContextService ctxService = ...;

 MyRunnableWork myRunnableWorkInstance = ...;

 Object proxy = ctxService.createContextualProxy(myRunnableWorkInstance, 
                                  Runnable.class, SomeWorkInterface.class);

 // call SomeWorkInterface method on the proxy
 ((SomeWorkInterface) proxy).someWorkInterfaceMethod();

 ExecutorService exSvc = Executors.newThreadPool(10, threadFactory);

 // submit the proxy as a Runnable to the ExecutorService 
 Future f = exSvc.submit( (Runnable)proxy);

Parameters
instance:Object

the instance of the object to proxy.

interfaces:Class<?>[]

the interfaces that the proxy should implement.

Returns:Object

a proxy for the input object that implements all of the specified interfaces.

Exceptions
IllegalArgumentException:
- if the interfaces argument is null or the instance does not implement all the specified interfaces.
createContextualProxyback to summary
public <T> T createContextualProxy(T instance, Map<String, String> executionProperties, Class<T> intf)

Creates a new contextual object proxy for the input object instance.

The contextual object is useful when developing or using Java™ SE threading mechanisms propagating events to other component instances.

If the application component that created the proxy is not started or deployed, all methods on reflected interfaces may throw an java.lang.IllegalStateException.

This method accepts a Map object which allows the contextual object creator to define what contexts or behaviors to capture when creating the contextual object. The specified properties will remain with the contextual object.

For example, to call a Message Driven Bean (MDB) with the sender's context, but within the MDB's transaction:

 public class MyServlet ... {
     public void doPost() throws NamingException, JMSException {
        InitialContext ctx = new InitialContext();

        // Get the ContextService that only propagates
        // security context.
        ContextService ctxSvc = (ContextService)
            ctx.lookup("java:comp/env/SecurityContext");

        // Set any custom context data through execution properties
        Map<String, String> execProps = new HashMap<>();
        execProps.put("vendor_a.security.tokenexpiration", "15000");
        // Specify that contextual object should run inside the current 
        // transaction.  If we have a failure, we don't want to consume
        // the message.
        execProps.put(ManagedTask.TRANSACTION, "USE_TRANSACTION_OF_EXECUTION_THREAD");

        ProcessMessage msgProcessor =
            ctxSvc.createContextualProxy(new MessageProcessor(), execProps,
            ProcessMessage.class);

        ConnectionFactory cf = (ConnectionFactory)
             ctx.lookup("java:comp/env/MyTopicConnectionFactory");
        Destination dest = (Destination) ctx.lookup("java:comp/env/MyTopic");
        Connection con = cf.createConnection();

        Session session = con.createSession(true, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(dest);

        Message msg = session.createObjectMessage((Serializable)msgProcessor);
        producer.send(dest, msg);
        ...

    }

  public class MyMDB ... {
    public void onMessage(Message msg) {
        // Get the ProcessMessage contextual object from the message.
        ObjectMessage omsg = (ObjectMessage)msg;
        ProcessMessage msgProcessor = (ProcessMessage)omsg.getObject();

        // Process the message in the specified context.
        msgProcessor.processMessage(msg);
    }
  }

  public interface  ProcessMessage {
      public void processMessage(Message msg);
  }

  public class MessageProcessor implements ProcessMessage, Serializable {
      public void processMessage(Message msg) {
          // Process the message with the application container
          // context that sent the message.

      }
  }

Parameters
instance:T

the instance of the object to proxy.

executionProperties:Map<String, String>

the properties to use when creating and running the context object.

intf:Class<T>

the interface that the proxy should implement.

Returns:T

a proxy for the input object that implements the specified interface.

Exceptions
IllegalArgumentException:
- if the intf argument null or the instance does not implement the specified interface.
createContextualProxyback to summary
public Object createContextualProxy(Object instance, Map<String, String> executionProperties, Class<?>... interfaces)

Creates a new contextual object proxy for the input object instance.

This method is similar to <T> T createContextualProxy(T instance, Map<String, String> executionProperties, Class<T> intf) except that this method can be used if the proxy has to support multiple interfaces.

Parameters
instance:Object

the instance of the object to proxy.

executionProperties:Map<String, String>

the properties to use when creating and running the context object.

interfaces:Class<?>[]

the interfaces that the proxy should implement.

Returns:Object

a proxy for the input object that implements all of the specified interfaces.

Exceptions
IllegalArgumentException:
- if the interfaces argument is null or the instance does not implement all the specified interfaces.
getExecutionPropertiesback to summary
public Map<String, String> getExecutionProperties(Object contextualProxy)

Gets the current execution properties on the context proxy instance.

Parameters
contextualProxy:Object

the contextual proxy instance to retrieve the execution properties.

Returns:Map<String, String>

A copy of the current contextual object execution properties, or null if the contextualProxy is created without specifying any execution properties.

Exceptions
IllegalArgumentException:
thrown if the input contextualProxy is not a valid contextual object proxy created with the createContextualProxy method.