コンテキスト・プロキシーの開発

ContextService などの作業マネージャーを使用して、インターフェースの呼び出しをコンテキストに対応して行います。

このタスクについて

オブジェクトのメソッドを呼び出す時点でスレッド・コンテキストが存在していなければならないアプリケーション・コンポーネントでは、javax.enterprise.concurrent.ContextService を実装する作業マネージャーを使用して、オブジェクトのコンテキスト・プロキシーを構成できます。コンテキスト・プロキシーの作成時に、作業マネージャーの設定に従ってスレッド・コンテキストが取り込まれます。コンテキスト・プロキシー上でインターフェース・メソッドが呼び出される際は、事前に取り込まれたスレッド・コンテキストが呼び出しの前に適用され、後で復元されます。

手順

  1. スレッド・コンテキストを要求するメソッドを使用して、インターフェースを定義します (または、適切な既存のインターフェースを選択します)。
    public interface AddressFinder {
        Address getAddress(Person p) throws Exception;
    }
  2. 次のようにして、インターフェースの実装を提供します。
    public class DatabaseAddressFinder implements AddressFinder {
        private final String resRefLookupName;
        public DatabaseAddressFinder(String resRefLookupName) {
            this.resRefLookupName = resRefLookupName;
        }
        public Address getAddress(Person p) throws Exception {
            // Resource reference lookup requires the thread context of the
            // application component that defines the resource reference.
            DataSource ds = (DataSource) new InitialContext().lookup(
                resRefLookupName);
            Connection con = ds.getConnection();
            try {
                ResultSet r = con.createStatement().executeQuery(
                    "SELECT STREETADDR, CITY, STATE, ZIP " +
                    "FROM ADDRESSES WHERE PERSONID=" + p.id);
                if (r.next())
                    return new Address(r.getString(1), r.getString(2),
                        r.getString(3), r.getInt(4));
                else
                    return null;
            } finally {
                con.close();
            }
        }
    }
  3. コンテキスト・サービスを使用して現行スレッドからコンテキストを取り込み、そのコンテキストでインスタンスをラップします。
    ContextService contextService = (ContextService) new InitialContext().lookup(
        "java:comp/DefaultContextService");
    AddressFinder addressFinder = contextService.createContextualProxy(
        new DatabaseAddressFinder("java:comp/env/jdbc/MyDataSourceRef"),
        AddressFinder.class);
  4. 別のスレッドから、コンテキスト・プロキシーを使用して、元のスレッドのコンテキストの下でメソッドを呼び出します。
    Address myAddress = addressFinder.getAddress(me);

トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tasb_cproxies
ファイル名:tasb_cproxies.html