작업 영역을 사용하는 애플리케이션 개발

애플리케이션은 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;
}
주의: EJB(Enterprise JavaBeans) 애플리케이션은 원격 또는 인터페이스 또는 둘 다의 메소드 구현 내에서 UserWorkArea 인터페이스만 사용할 수 있습니다. 마찬가지로, 서블릿은 HTTPServlet 클래스의 서비스 메소드 내에서만 인터페이스를 사용할 수 있습니다. 서블릿 또는 엔터프라이즈 Bean의 라이프 사이클 메소드 내에서 작업 영역을 사용하는 것은 작업 영역 프로그래밍 모델에서 벗어나는 것으로 간주되어 지원되지 않습니다.
작업 영역 서비스는 UserWorkArea 인터페이스에서 사용할 수 있도록 다음 예외를 정의합니다.
NoWorkArea
요청에는 연관된 작업 영역이 필요하지만 작업 영역이 없는 경우에 발생합니다.
NotOriginator
요청이 가져온 작업 영역의 내용을 조작하려는 경우에 발생합니다.
PropertyReadOnly
요청이 읽기 전용이거나 고정 읽기 전용 특성을 수정하려는 경우에 발생합니다.
PropertyFixed
지정된 특성에 고정 모드 중 하나가 들어 있는 경우에 remove 메소드에 의해 발생합니다.

프로시저

  1. 다음 중 하나의 방법으로 파티션에 액세스하십시오.
    • UserWorkArea 파티션 액세스 - UserWorkArea 파티션에 액세스합니다.
    • 사용자 정의 작업 영역 파티션 액세스 - 사용자 정의 작업 영역에 액세스합니다.
    다음 단계에서는 예제로 UserWorkArea 파티션을 사용합니다. 그러나 사용자 정의 파티션을 같은 방법으로 사용할 수도 있습니다.
  2. 새 작업 영역을 시작하십시오.

    begin 메소드를 사용하여 새 작업 영역을 작성하고 이를 호출 스레드와 연관시키십시오. 작업 영역은 이 작업 영역을 시작하는 스레드로 범위가 지정되어 여러 스레드에게 액세스되지 않습니다. begin 메소드는 문자열을 인수로 취하는데, 문자열이 작업 영역을 이름 지정하는 데 사용됩니다. 인수는 널이 아니어야 하며, 널인 경우 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 메소드를 제공합니다. 2인수 set 메소드는 보통의 특성 모드를 갖는 특성을 삽입합니다. 3인수 set 메소드는 특성 모드를 세 번째 인수로 갖습니다. 두 set 메소드 모두가 키와 값을 인수로 갖습니다. 키는 문자열이고, 값은 유형 java.io.Serializable의 오브젝트입니다. 어떤 인수도 널일 수 없으며 널인 인수는 java.lang.NullPointer 예외가 발생하게 합니다.

    다음 SimpleSample 애플리케이션은 SimpleSampleCompany 클래스 및 SimpleSampleProperty 클래스와 같은 두 클래스의 오브젝트를 특성의 값으로 사용합니다. SimpleSampleCompany 클래스는 사이트 ID에 사용되고 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™ Serializable 오브젝트를 키와 연관된 값으로 리턴합니다. 예를 들어 작업 영역에서 회사 키의 값을 검색하기 위해 이전 코드 예는 값을 검색할 작업 영역에 대해 get 메소드를 사용합니다.

    특성 모드 설정. UserWorkArea 인터페이스의 2인수 set 메소드는 키와 값을 인수로 취하고 정상인 기본 특성 모드를 갖는 특성을 삽입합니다. 다른 모드를 갖는 특성을 설정하려면 애플리케이션이 3인수 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) {
          }
       }
    }
    

작업 영역 오브젝트 유형. 다음 예에서 클라이언트는 작업 영역을 작성하고 작업 영역에 사이트 ID와 우선순위의 두 특성을 삽입합니다. 사이트 ID는 읽기 전용 특성으로 설정되므로 클라이언트는 작업 영역의 받는 사람이 사이트 ID를 대체하도록 허용하지 않습니다. 이 특성은 회사 키와 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은 먼저 작업 영역에서 사이트 ID 및 우선순위 특성을 읽습니다. 그런 다음 Bean은 의도적으로 가져온 작업 영역에 직접 쓰고 읽기 전용 사이트 ID 특성을 대체하려 시도하지만 작업이 실패합니다.

SimpleSampleBean이 성공적으로 중첩된 작업 영역을 시작하며, 여기에서 클라이언트의 우선순위를 대체한 후 다른 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 문서는 참조 > API - 애플리케이션 프로그래밍 인터페이스 경로의 Information Center 목차에서 사용 가능합니다.


주제 유형을 표시하는 아이콘 태스크 주제



시간소인 아이콘 마지막 업데이트 날짜: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twa_develop
파일 이름:twa_develop.html