public interface ContextServicejava.lang.reflect.ProxySE で定義されている)を作成するためのメソッドを提供します。そのようなコンテキストの例は、クラスローディング、名前空間、セキュリティなどです。 プロキシオブジェクトは、java.lang.reflect.ProxySE クラスに対して定義されたものと同じ規則に従いますが、以下の追加があります。
createContextualProxy メソッドで指定されたすべてのインターフェースを実装します。hashCode、equals、toString と ObjectSE で宣言された他のすべてのメソッドを除いて、プロキシインスタンスでのすべてのインターフェースメソッドの呼び出しは、作成者のコンテキストで実行されます。SerializableSE を実装する必要があります。SerializableSE を実装する必要があります。| 修飾子と型 | メソッドと説明 |
|---|---|
ObjectSE | createContextualProxy(ObjectSE instance, ClassSE<?>... interfaces) 入力オブジェクトインスタンスの新しいコンテキストオブジェクトプロキシを作成します。 |
ObjectSE | createContextualProxy(ObjectSE instance, MapSE<StringSE, StringSE> executionProperties, ClassSE<?>... interfaces) 入力オブジェクトインスタンスの新しいコンテキストオブジェクトプロキシを作成します。 |
<T> T | createContextualProxy(T instance, ClassSE<T> intf) 入力オブジェクトインスタンスの新しいコンテキストオブジェクトプロキシを作成します。 |
<T> T | createContextualProxy(T instance, MapSE<StringSE, StringSE> executionProperties, ClassSE<T> intf) 入力オブジェクトインスタンスの新しいコンテキストオブジェクトプロキシを作成します。 |
MapSE<StringSE, StringSE> | getExecutionProperties(ObjectSE contextualProxy) コンテキストプロキシインスタンスの現在の実行プロパティを取得します。 |
<T> T createContextualProxy(T instance,
ClassSE<T> intf)各メソッド呼び出しには、コンテキストオブジェクトプロキシを作成したアプリケーションコンポーネントインスタンスのコンテキストがあります。
コンテキストオブジェクトは、Java™ の開発または使用時に役立ちます。イベントを他のコンポーネントインスタンスに伝播する SE スレッドメカニズム。
プロキシを作成したアプリケーションコンポーネントが起動またはデプロイされていない場合、リフレクトされたインターフェースのすべてのメソッドが IllegalStateExceptionSE をスローする場合があります。
例: Java™ を使用して、作成者のコンテキストでコンテキスト化された Runnable を実行します。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);
instance - プロキシするオブジェクトのインスタンス。intf - プロキシが実装する必要のあるインターフェース。IllegalArgumentExceptionSE - - intf 引数が null であるか、インスタンスが指定されたインターフェースを実装していない場合。ObjectSE createContextualProxy(ObjectSE instance, ClassSE<?>... interfaces)
このメソッドは、<T> T createContextualProxy(T instance, Class<T> intf) に似ていますが、プロキシが複数のインターフェースをサポートする必要がある場合にこのメソッドを使用できる点が異なります。
例:
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);
instance - プロキシするオブジェクトのインスタンス。interfaces - プロキシが実装する必要のあるインターフェース。IllegalArgumentExceptionSE - - interfaces 引数が null であるか、インスタンスが指定されたすべてのインターフェースを実装していない場合。<T> T createContextualProxy(T instance,
MapSE<StringSE,StringSE> executionProperties,
ClassSE<T> intf)コンテキストオブジェクトは、Java™ の開発または使用時に役立ちます。イベントを他のコンポーネントインスタンスに伝播する SE スレッドメカニズム。
プロキシを作成したアプリケーションコンポーネントが起動またはデプロイされていない場合、リフレクトされたインターフェースのすべてのメソッドが IllegalStateExceptionSE をスローする場合があります。
このメソッドは、Map オブジェクトを受け入れます。これにより、コンテキストオブジェクトの作成者は、コンテキストオブジェクトの作成時にキャプチャーするコンテキストまたは動作を定義できます。指定されたプロパティは、コンテキストオブジェクトとともに残ります。
例: MDB のトランザクション内で、送信者のコンテキストを使用してメッセージ駆動型 Bean(MDB)を呼び出すには:
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.
}
}
instance - プロキシするオブジェクトのインスタンス。executionProperties - コンテキストオブジェクトを作成して実行するときに使用するプロパティ。intf - プロキシが実装する必要のあるインターフェース。IllegalArgumentExceptionSE - - intf 引数が null の場合、またはインスタンスが指定されたインターフェースを実装していない場合 ObjectSE createContextualProxy(ObjectSE instance, MapSE<StringSE,StringSE> executionProperties, ClassSE<?>... interfaces)
このメソッドは、<T> T createContextualProxy(T instance, Map<String, String> executionProperties, Class<T> intf) に似ていますが、プロキシが複数のインターフェースをサポートする必要がある場合にこのメソッドを使用できる点が異なります。
instance - プロキシするオブジェクトのインスタンス。executionProperties - コンテキストオブジェクトを作成して実行するときに使用するプロパティ。interfaces - プロキシが実装する必要のあるインターフェース。IllegalArgumentExceptionSE - - interfaces 引数が null であるか、インスタンスが指定されたすべてのインターフェースを実装していない場合。MapSE<StringSE,StringSE> getExecutionProperties(ObjectSE contextualProxy)
contextualProxy - 実行プロパティを取得するためのコンテキストプロキシインスタンス。IllegalArgumentExceptionSE - 入力 contextualProxy が createContextualProxy メソッドで作成された有効なコンテキストオブジェクトプロキシでない場合にスローされます。Copyright © 2019 Eclipse Foundation.
Use is subject to license terms.