java.lang.reflect.Proxy).
ContextService also creates proxy objects for functional interfaces
(such as java.util.function.Function) that can be used as
completion stage actions. Proxy objects run with
the addition of context typically associated with applications executing in a
Jakarta™ 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:
createContextualProxy methods.
hashCode,
equals, toString and all other methods declared in
java.lang.Object.
java.io.Serializable
if the proxied object instance is serializable.
java.io.Serializable if the proxy instance is serialized.
For example, to contextualize a single completion stage action such that it is able to access the namespace of the application component,
contextSvc = InitialContext.doLookup("java:comp/DefaultContextService");
stage2 = stage1.thenApply(contextSvc.contextualFunction(i -> {
DataSource ds = InitialContext.doLookup("java:comp/env/dsRef");
try (Connection con = ds.getConnection()) {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, i);
ResultSet result = stmt.executeQuery();
return result.next() ? result.getInt(1) : 0;
}
}));
| Modifier and Type | Method and Description |
|---|---|
| public < callable result type. R> Callable | Returns: contextualized proxy instance that wraps execution of thecall method with context.instance to contextualize. callable)Wraps a |
| public < type of first parameter to consumer. T, type of second parameter to consumer. U> BiConsumer | Returns: contextualized proxy instance that wraps execution of theaccept method with context.instance to contextualize. consumer)Wraps a |
| public < type of parameter to consumer. T> Consumer | Returns: contextualized proxy instance that wraps execution of theaccept method with context.instance to contextualize. consumer)Wraps a |
| public < type of first parameter to function. T, type of second parameter to function. U, function result type. R> BiFunction | Returns: contextualized proxy instance that wraps execution of theapply method with context.instance to contextualize. function)Wraps a |
| public < type of parameter to function. T, function result type. R> Function | Returns: contextualized proxy instance that wraps execution of theapply method with context.instance to contextualize. function)Wraps a |
| public Runnable | Returns: contextualized proxy instance that wraps execution of therun method with context.instance to contextualize. runnable)Wraps a |
| public < supplier result type. R> Supplier | Returns: contextualized proxy instance that wraps execution of thesupply method with context.instance to contextualize. supplier)Wraps a |
| public < the type of the instance to proxy T> T | Returns: a proxy for the input object that implements the specified interface.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.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 < the type of the interface T> T | Returns: a proxy for the input object that implements the specified interface.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.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 Executor | Returns: an executor that wraps theexecute method with context.Captures thread context as an |
| public Map | Returns: A copy of the current contextual object execution properties, or null if the contextualProxy is created without specifying any execution properties.the contextual proxy instance to retrieve the execution properties. contextualProxy)Gets the current execution properties on the context proxy instance. |
| public < completable future result type. T> CompletableFuture | Returns: the new completable future.a completable future whose completion triggers completion of the new completable
future that is created by this method. stage)Returns a new |
| public < completion stage result type. T> CompletionStage | Returns: the new completion stage.a completion stage whose completion triggers completion of the new stage
that is created by this method. stage)Returns a new |
| contextualCallable | back to summary |
|---|---|
| public <R> Callable Wraps a When
| |
| contextualConsumer | back to summary |
|---|---|
| public <T, U> BiConsumer Wraps a When
| |
| contextualConsumer | back to summary |
|---|---|
| public <T> Consumer Wraps a When
| |
| contextualFunction | back to summary |
|---|---|
| public <T, U, R> BiFunction Wraps a When
| |
| contextualFunction | back to summary |
|---|---|
| public <T, R> Function Wraps a When
| |
| contextualRunnable | back to summary |
|---|---|
| public Runnable contextualRunnable(Runnable runnable) Wraps a When
| |
| contextualSupplier | back to summary |
|---|---|
| public <R> Supplier Wraps a When
| |
| createContextualProxy | back 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
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 Jakarta 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);
| |
| createContextualProxy | back 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
public class MyRunnableWork implements Runnable, SomeWorkInterface {
public void run() {
System.out.println("MyRunnableWork.run with Jakarta 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);
| |
| createContextualProxy | back 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
This method accepts a 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.
}
}
| |
| createContextualProxy | back 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
| |
| currentContextExecutor | back to summary |
|---|---|
| public Executor currentContextExecutor() Captures thread context as an Example usage:
The returned
| |
| getExecutionProperties | back to summary |
|---|---|
| public Map Gets the current execution properties on the context proxy instance.
| |
| withContextCapture | back to summary |
|---|---|
| public <T> CompletableFuture Returns a new The new completable future gets its default asynchronous execution facility
from this When dependent stages are created from the new completable future,
and from the dependent stages of those stages, and so on, thread context is captured
and/or cleared by the Invocation of this method does not impact thread context propagation for the originally supplied completable future or any other dependent stages directly created from it (not using this method).
| |
| withContextCapture | back to summary |
|---|---|
| public <T> CompletionStage Returns a new The new completion stage gets its default asynchronous execution facility from this When dependent stages are created from the new completion stage,
and from the dependent stages of those stages, and so on, thread context is captured
and/or cleared by the Invocation of this method does not impact thread context propagation for the originally supplied stage or any other dependent stages directly created from it (not using this method).
| |