作業域を使用するアプリケーションの開発
アプリケーションは UserWorkArea インターフェースを実装することにより、作業域サービスと対話します。このインターフェースは、作業域を作成、操作、および終了するために使用されるすべてのメソッドを定義します。
このタスクについて
package com.ibm.websphere.workarea;
public interface UserWorkArea {
void begin(String name);
void complete() throws NoWorkArea, NotOriginator;
String getName();
String[] retrieveAllKeys();
void set(String key, java.io.Serializable value)
throws NoWorkArea, NotOriginator, PropertyReadOnly;
void set(String key, java.io.Serializable value, PropertyModeType mode)
throws NoWorkArea, NotOriginator, PropertyReadOnly;
java.io.Serializable get(String key);
PropertyModeType getMode(String key);
void remove(String key)
throws NoWorkArea, NotOriginator, PropertyFixed;
}
- NoWorkArea
- 要求で要求された関連作業域が存在していない場合に発生します。
- NotOriginator
- インポートされた作業域の内容を要求が操作しようとすると発生します。
- PropertyReadOnly
- 読み取り専用プロパティーまたは固定読み取り専用プロパティーを要求が 変更しようとすると発生します。
- PropertyFixed
- 指定されたプロパティーがいずれかの固定モードである場合、remove メソッド によって発生します。
手順
例
作業域のオブジェクト型。 以下の例では、クライアントは作業域を作成し、その作業域に サイト ID と優先順位の 2 つのプロパティーを挿入しています。 サイト ID は読み取り専用プロパティーとして設定されます。クライアントは作業域の受信側が サイト ID をオーバーライドすることを禁止しています。このプロパティーは company キーと、SimpleSampleCompany オブジェクトの静的インスタンスから構成されています。 優先順位プロパティーは priority キーと、SimpleSamplePriority オブジェクトの静的インスタンスから構成されています。オブジェクト型は次のコード例で示しているように定義されます。
public static final class SimpleSampleCompany {
public static final SimpleSampleCompany Main;
public static final SimpleSampleCompany NewYork_Sales;
public static final SimpleSampleCompany NewYork_Development;
public static final SimpleSampleCompany London_Sales;
public static final SimpleSampleCompany London_Development;
}
public static final class SimpleSamplePriority {
public static final SimpleSamplePriority Platinum;
public static final SimpleSamplePriority Gold;
public static final SimpleSamplePriority Silver;
public static final SimpleSamplePriority Bronze;
public static final SimpleSamplePriority Tin;
}
クライアントはその後、リモート・オブジェクトに呼び出しを作成します。作業域は自動的に伝搬されます。リモート・オブジェクトのどのメソッドも、 作業域の引数を取りません。 リモート側では、まず SimpleSampleBean によって要求が処理されます。 SimpleSampleBean は最初に、作業域からサイト ID と優先順位プロパティーを読み取ります。 この Bean は次に、インポートした作業域への直接の書き込みと、 読み取り専用サイト ID プロパティーのオーバーライドを行おうとして、それに失敗します。
SimpleSampleBean は、ネストされた作業域を正常に開始して、クライアントの優先順位をオーバーライドし、 次に、もう 1 つの Bean である SimpleSampleBackendBean を呼び出します。 SimpleSampleBackendBean は、作業域からプロパティーを読み込みます。 この作業域は、クライアントに設定されたサイト ID と、SimpleSampleBean に設定された優先順位を含んでいます。 最後に、SimpleSampleBean はそのネストされた作業域を完了し、 サイト ID プロパティーに基づいたメッセージを書き込んで戻ります。
作業域区画マネージャーの使用。 以下のコード例は、作業域区画マネージャーのインターフェースの使用を示しています。 この例は、プログラマチックに作業域区画を作成およびリトリーブする 方法を示しています。ただし、作業域区画のプログラマチックな作成は、Java Platform, Enterprise Edition (Java EE) クライアントでのみ使用可能であることに ご注意ください。サーバー上で作業域区画を作成するには、管理コンソールを 使用する必要があります。区画を構成する上で使用可能な構成パラメーターについては、 『作業域区画サービス』の項を参照してください。
import com.ibm.websphere.workarea.WorkAreaPartitionManager;
import com.ibm.websphere.workarea.UserWorkArea;
import com.ibm.websphere.workarea.PartitionAlreadyExistsException;
import com.ibm.websphere.workarea.NoSuchPartitionException;
import java.lang.IllegalAccessError;
import java.util.Properties;
import javax.naming.InitialContext;
//This sample demonstrates how to retrieve an instance of the
//WorkAreaPartitionManager implementation and how to use that
//instance to create a WorkArea partition and retrieve a partition.
//NOTE: Creating a partition in the way listed is only available
//on a J2EE client. To create a partition on the server use the
//WebSphere administrative console. Retrieving a WorkArea
//partition is performed in the same way on both client and server.
public class Example {
//The name of the partition to create/retrieve
String partitionName = "myPartitionName";
//The name in java naming the WorkAreaPartitionManager instance is bound to
String jndiName = "java:comp/websphere/WorkAreaPartitionManager";
//On a J2EE client a user would create a partition as follows:
public UserWorkArea myCreate(){
//Variable to hold our WorkAreaPartitionManager reference
WorkAreaPartitionManager partitionManager = null;
//Get an instance of the WorkAreaPartitionManager implementation
try {
InitialContext initialContext = new InitialContext();
partitionManager = (WorkAreaPartitionManager) initialContext.lookup(jndiName);
} catch (Exception e) { }
//Set the properties to configure our WorkArea partition
Properties props = new Properties();
props.put("maxSendSize","12345");
props.put("maxReceiveSize","54321");
props.put("Bidirectional","true");
props.put("DeferredAttributeSerialization","true");
//Variable used to hold the newly created WorkArea Partition
UserWorkArea myPartition = null;
try{
//This is the way to create a partition on the J2EE client. Use the
//WebSphere Administrative Console to create a WorkArea Partition
//on the server.
myPartition = partitionManager.createWorkAreaPartition(partitionName,props);
}
catch (PartitionAlreadyExistsException e){ }
catch (IllegalAccessException e){ }
return myPartition;
}
//. . . .
//In order to retrieve a WorkArea partition at some time later or
//from some other class, do the following (from client or server):
public UserWorkArea myGet(){
//Variable to hold our WorkAreaPartitionManager reference
WorkAreaPartitionManager partitionManager = null;
//Get an instance of the WorkAreaPartitionManager implementation
try {
InitialContext initialContext = new InitialContext();
partitionManager = (WorkAreaPartitionManager) initialContext.lookup(jndiName);
} catch (Exception e) { }
//Variable used to hold the retrieved WorkArea partition
UserWorkArea myPartition = null;
try{
myPartition = partitionManager.getWorkAreaPartition(partitionName);
}catch(NoSuchPartitionException e){ }
return myPartition;
}
}
次のタスク
作業域について詳しくは、API の『com.ibm.websphere.workarea パッケージ』を参照してください。 生成済み API 文書は、インフォメーション・センターの目次で、
の順にたどって入手できます。