开发使用工作区的应用程序
应用程序通过实现 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 方法抛出。
过程
示例
工作区对象类型。在下面的示例中,客户机创建了一个工作区并将两个属性插入其中:站点标识和优先级。站点标识被设置为只读属性;客户机不允许工作区的接收方覆盖站点标识。该属性由主公司和 SimpleSampleCompany 对象的一个静态实例组成。优先级属性由主优先级和 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 处理;bean 首先从工作区读取站点标识和优先级属性。bean 有意尝试直接写入导入的工作区和覆盖只读的站点标识属性并且失败。
SimpleSampleBean 成功地启动一个嵌套工作区,它覆盖了其中的客户机优先级,然后调用另一个 Bean (SimpleSampleBackendBean)。SimpleSampleBackendBean 从工作区读取属性,此工作区包含客户机中设置的站点标识和 SimpleSampleBean 中设置的优先级。最后,SimpleSampleBean 完成它的嵌套工作区,根据站点标识属性写出并返回一条消息。
使用工作区分区管理器。以下代码示例说明了如何使用工作区分区管理器接口。它说明了如何通过程序创建和检索工作区分区。请注意,只有在 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.workare 包。在信息中心目录中,生成的 API 文档的路径是:
。