Applications interact with the work area service by implementing the UserWorkArea interface. This interface defines all of the methods used to create, manipulate, and terminate work areas.
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;
}
Work area object types. In the following example, the client creates a work area and inserts two properties into the work area: a site identifier and a priority. The site-identifier is set as a read-only property; the client does not allow recipients of the work area to override the site identifier. This property consists of the key company and a static instance of a SimpleSampleCompany object. The priority property consists of the key priority and a static instance of a SimpleSamplePriority object. The object types are defined as shown in the following code example:
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;
}
The client then makes an invocation on a remote object. The work area is automatically propagated; none of the methods on the remote object take a work area argument. On the remote side, the request is first handled by the SimpleSampleBean; the bean first reads the site identifier and priority properties from the work area. The bean then intentionally attempts, and fails, both to write directly into the imported work area and to override the read-only site-identifier property.
The SimpleSampleBean successfully begins a nested work area, in which it overrides the client's priority, then calls another bean, the SimpleSampleBackendBean. The SimpleSampleBackendBean reads the properties from the work area, which contains the site identifier set in the client and priority set in the SimpleSampleBean. Finally, the SimpleSampleBean completes its nested work area, writes out a message based on the site-identifier property, and returns.
Using the work area partition manager. The following code example illustrates the use of the work area partition manager interface. The sample illustrates how to create and retrieve a work area partition programmatically`. Please note that programmatically creating a work area partition is only available on the Java Platform, Enterprise Edition (Java EE) client. To create a work area partition on the server one must use the administrative console. Refer to the Work area partition service article for configuration parameters available to configure a partition.
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 below 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;
}
}
For additional information about work area, see the com.ibm.websphere.workarea package in the API. The generated API documentation is available in the information center table of contents from the path
.