开发使用工作区的应用程序

应用程序通过实现 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;
}
注意: Enterprise JavaBeans (EJB) 应用程序只能在远程接口和/或本地接口中的方法实现中使用 UserWorkArea 接口;同样,servlet 只能在 HTTPServlet 类的服务方法中使用此接口。在 servlet 或企业 Bean 的任何生命周期方法中使用工作区都被视作违背工作区编程模型,并且不受支持。
工作区服务定义了以下异常并将它们用于 UserWorkArea 接口:
NoWorkArea
当请求需要一个关联的工作区,但不存在此类工作区时抛出。
NotOriginator
当请求尝试操作已导入工作区的内容时抛出。
PropertyReadOnly
当请求尝试修改一个只读属性或固定只读属性时抛出。
PropertyFixed
当指定的属性具有一种固定方式时,由 remove 方法抛出。

过程

  1. 通过以下两种方法之一访问分区:
    • 访问 UserWorkArea 分区,以访问 UserWorkArea 分区。
    • 访问用户定义的工作区分区,以访问用户定义的工作区。
    以下步骤将 UserWorkArea 分区作为示例,但您可以用同样的方式来使用用户定义的分区。
  2. 开始新的工作区。

    使用 begin 方法以创建一个新的工作区并将它与调用线程关联起来。工作区的范围为启动此工作区的线程,不能由多个线程访问。begin 方法将字符串作为参数;此字符串用于命名工作区。参数不能为 null,它会导致抛出 java.lang.NullPointer 异常。在以下代码示例中,应用程序以名称 SimpleSampleServlet 启动一个新的工作区:

    public class SimpleSampleServlet {
    ...
       try {
          ...
          userWorkArea = (UserWorkArea)jndi.lookup(
             "java:comp/websphere/UserWorkArea");
       }
       ...
    
       userWorkArea.begin("SimpleSampleServlet");
       ...
    }

    begin 方法还可用于创建嵌套工作区;当调用 begin 方法时,如果工作区与线程相关联,该方法将创建一个嵌套在现有工作区中的新工作区。

    工作区服务不会使用与工作区关联的名称;您可以选择任何方式命名工作区。名称不必是唯一的,但是,如果名称在应用程序中是独特的并且具有一定的意义,它对于调试的作用可以得到增强。应用程序可以使用 getName 方法返回由 begin 方法与工作区关联在一起的名称。

  3. 设置工作区中的属性。

    当前工作区的应用程序可以将属性插入工作区并从工作区检索属性。UserWorkArea 接口为设置属性提供了两个 set 方法,为检索属性提供了一个 get 方法。具有两个参数的 set 方法以常规属性方式插入属性。具有三个参数的 set 方法将属性方式作为第三个参数。两种 set 方法都将键和值作为参数。键是一个字符串;而值是类型 java.io.Serializable 的一个对象。这两个参数都不能为 null,否则将导致抛出 java.lang.NullPointer 异常。

    以下 SimpleSample 应用程序使用两个类(SimpleSampleCompany 类和 SimpleSampleProperty 类)的对象作为属性的值。SimpleSampleCompany 类用于站点标识,而 SimpleSamplePriority 类则用于优先级。

    public class SimpleSampleServlet {
       ...
       userWorkArea.begin("SimpleSampleServlet");
    
       try {
          // Set the site-identifier (default is Main).
          userWorkArea.set("company",
             SimpleSampleCompany.Main, PropertyModeType.read_only);
    
          // Set the priority.
          userWorkArea.set("priority", SimpleSamplePriority.Silver);
       }
    
       catch (PropertyReadOnly e) {
         // The company was previously set with the read-only or
         // fixed read-only mode.
         ...
       }
    
        catch (NotOriginator e) {
         // The work area originated in another process,
         // so it can't be modified here.
         ...
       }
    
       catch (NoWorkArea e) {
          // There is no work area begun on this thread.
          ...
       }
    
       // Do application work.
       ...
    }

    get 方法将键作为参数并返回一个 Java™ 可序列化对象作为键的关联值。例如,要从工作区检索公司键的值,以上代码示例对工作区使用 get 方法来检索该值。

    设置属性方式。UserWorkArea 接口上具有两个参数的 set 方法将键和值作为参数并以常规缺省属性方式插入属性。要使用不同的方式设置属性,应用程序必须使用具有三个参数的 set 方法,该方法将属性方式作为第三个参数。用于请求属性方式的值如下:
    • 常规:PropertyModeType.normal
    • 固定常规:PropertyModeType.fixed_normal
    • 只读:PropertyModeType.read_only
    • 固定只读:PropertyModeType.fixed_readonly
  4. 使用工作区管理本地工作。
  5. 完成工作区。

    当应用程序用完工作区时,它必须通过调用 UserWorkArea 接口中的 complete 方法来完成工作区。此操作将终止与调用线程的关联并销毁工作区。如果在嵌套工作区中调用 complete 方法,那么嵌套工作区将终止并且父工作区将成为当前工作区。如果没有任何工作区与调用线程关联,将抛出 NoWorkArea 异常。 每个工作区都必须完成,并且只能由发起进程完成。例如,如果服务器尝试对客户机发起的工作区调用 complete 方法,将抛出 NotOriginator 异常。服务器进程中创建的工作区始终不会反作用于调用客户机进程。

    注意: 工作区服务要求本地远程之间完全透明。即便两个 Bean 恰巧部署于同一服务器中,并且 JVM 和进程也因此相同,那么任何远程调用之后,另一个工作区的调用启动的工作区将完成,而发起请求所在的 Bean 则始终保持同一状态。

    以下代码示例显示了客户机应用程序中创建的工作区完成。

    public class SimpleSampleServlet {
       ...
       userWorkArea.begin("SimpleSampleServlet");
       userWorkArea.set("company",
           SimpleSampleCompany.Main, PropertyModeType.read_only);
       userWorkArea.set("priority", SimpleSamplePriority.Silver);
       ...
    
       // Do application work.
       ...
    
       // Terminate the work area.
       try {
          userWorkArea.complete();
       }
    
       catch (NoWorkArea e) {
          // There is no work area associated with this thread.
          ...
       }
    
       catch (NotOriginator e) {
          // The work area was imported into this process.
          ...
       }
      ...
    }

    以下代码示例显示了样本应用程序如何完成它先前在远程调用创建的嵌套工作区。

    public class SimpleSampleBeanImpl implements SessionBean {
    
        public String [] test() {
          ...
    
          // Begin a nested work area.
          userWorkArea.begin("SimpleSampleBean");
          try {
            userWorkArea.set("company",
                             SimpleSampleCompany.London_Development);
          }
          catch (NotOriginator e) {
          }
    
          SimpleSampleCompany company =
             (SimpleSampleCompany) userWorkArea.get("company");
          SimpleSamplePriority priority =
             (SimpleSamplePriority) userWorkArea.get("priority");
    
          // Complete all nested work areas before returning.
          try {
            userWorkArea.complete();
          }
          catch (NoWorkArea e) {
          }
          catch (NotOriginator e) {
          }
       }
    }
    

示例

工作区对象类型。在下面的示例中,客户机创建了一个工作区并将两个属性插入其中:站点标识和优先级。站点标识被设置为只读属性;客户机不允许工作区的接收方覆盖站点标识。该属性由主公司和 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 文档的路径是:参考 > API - 应用程序编程接口


指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twa_develop
文件名:twa_develop.html