임베드 가능 EJB 컨테이너를 사용한 애플리케이션 개발

임베드 가능한 EJB(Enterprise JavaBeans) 컨테이너를 사용하여 애플리케이션을 개발하려면 이 태스크를 사용하십시오. 임베드 가능 컨테이너에서 실행하는 애플리케이션은 더 빨리 시작하며 전체 애플리케이션 서버에서 실행할 때 더 작은 설치면적이 필요합니다. 결국 애플리케이션 서버에서 실행할 수 있는 애플리케이션을 빨리 개발 및 테스트하기 위한 최적 환경입니다.

시작하기 전에

임베드 가능한 EJB 컨테이너 애플리케이션을 작성하려면 JDK(Java™ Development Kit) 버전 1.8 이상의 버전 8.0을 사용하도록 개발 환경을 설정해야 합니다. 개발 환경은 또한 클래스 경로에 WebSphere® 임베드 가능한 컨테이너 JAR(Java archive) 파일도 포함해야 합니다. com.ibm.ws.ejb.embeddableContainer_9.0.jar 파일은 WebSphere Application Server 설치 디렉토리 아래의 \runtimes 디렉토리에 위치합니다.
제한사항: EJB 씬 클라이언트인 com.ibm.ws.ejb.thinclient_9.0.jar과 EJB 임베드 가능 JAR 파일인 com.ibm.ws.ejb.embeddableContainer_9.0.jar은 동일한 클래스 경로에 공존할 수 없습니다.
제한사항: com.ibm.ws.ejb.embeddableContainer_9.0.jar 파일은 메시지를 영어로 제공하도록 제한됩니다. 추가 언어에 대한 지원이 필요한 경우 com.ibm.ws.ejb.embeddableContainer_9.0.jar. 대신 com.ibm.ws.ejb.embeddableContainer_nls_9.0.jar 파일을 사용하십시오. com.ibm.ws.ejb.embeddableContainer_nls_9.0.jar은 WebSphere Application Server의 설치 디렉토리 아래의 \runtimes 디렉토리에 위치합니다.

Bean이 javax.annotation.Resource 어노테이션을 검색 속성과 함께 사용하는 경우, Java Endorsed 표준 대체 메커니즘을 사용하여 시스템의 JDK에서 사용할 수 있는 javax.annotation.Resource API를 대체해야 합니다. 사용자가 선택하는 대상 디렉토리에 app_server_root\runtimes\endorsed\endorsed_apis_9.0.jar 파일을 복사하십시오. 복사된 JAR 파일을 포함하는 디렉토리를 지정하려면 Java 명령에서 java.endorsed.dirs 특성을 사용하십시오.

프로시저

  1. EJB 3.X 모듈을 작성하십시오. 이 모듈을 작성할 때, WebSphere 임베드 가능 컨테이너가 지원하는 기능만 포함하도록 해야 합니다. 지원되는 기능의 전체 목록은 임베디드 가능한 EJB 컨테이너 기능 주제를 참조하십시오. EJB 모듈이 임베드 가능 컨테이너의 클래스 경로에 있는지 확인하십시오. EJB 모듈을 클래스의 디렉토리로서 또는 EJB JAR 파일로서 패키징할 수 있습니다.
  2. 임베드 가능 컨테이너를 실행하고 엔터프라이즈 Bean에 대해 메소드를 시작하는 기본 클래스를 작성하십시오. 임베드 가능 컨테이너의 인스턴스를 작성하고(선택적으로 컨테이너 구성 매개변수를 전달), 컨테이너 이름 지정 컨텍스트를 가져오고 임베드 가능 컨테이너를 닫으려면 javax.ejb.EJBContainer 클래스를 사용하십시오.

    다음 샘플 코드는 임베드 가능 컨테이너의 사용법을 설명합니다.

    //EmbeddableContainerSample.java
    import java.util.HashMap;
    import java.util.Map;
    import javax.ejb.embeddable.EJBContainer;
    import my.pkg.MyBeanIface; // this is the local business interface of the
                               // enterprise bean
    
    public class EmbeddableContainerSample {
    
       public static void main(String[] args) throws Throwable {
    
          // Create a properties map to pass to the embeddable container:
          Map<String,Object> properties = new HashMap<String,Object>();
    
          // Specify that you want to use the WebSphere embeddable container:
          properties.put(EJBContainer.PROVIDER, 
              "com.ibm.websphere.ejbcontainer.EmbeddableContainerProvider");
    
          // Create the container instance, passing it the properties map:
          EJBContainer ec = EJBContainer.createEJBContainer(properties);
    
          // Use the container context to look up a bean:
          MyBeanIface bean = ec.getContext().lookup(
              "java:global/MyEJBModule/MyBean!my.pkg.MyBeanIface");
    
          // Invoke a method on the bean instance:
          bean.doStuff();
    
          ...
    
    
          // Close the embeddable container:
          ec.close();
    
       }
    }
    

    이 샘플 코드에서, EJBContainer.PROVIDER 특성을 com.ibm.websphere.ejbcontainer.EmbeddableContainerProvider 클래스에 지정하고 해당 특성을 EJBContainer.createEJBContainer 메소드로 전달하여 임베드 가능 컨테이너의 인스턴스를 작성했습니다. 컨테이너 이름 지정 컨텍스트를 사용하여 로컬 엔터프라이즈 Bean(예: MyBean)을 검색했습니다. 검색은 이식 가능한 글로벌 이름 지정 구문을 사용합니다.

    이 샘플 코드는 임베드 가능 컨테이너에 의존하여 클래스 경로를 자동으로 스캔하여 EJB 모듈인 MyEJBModule을 찾습니다. 다른 방법으로는, EJBContainer.MODULES 특성을 사용하여 시작하기 원한 모듈을 지정했을 수 있습니다. JVM 클래스 경로에 존재해야 하는 모듈 이름의 문자열 또는 문자열 배열을 지정하려면 이 특성을 사용하십시오.

    또한 클래스 경로에 존재하지 않는 모듈의 파일 또는 파일 배열을 지정할 수도 있습니다. 이 파일 또는 파일 배열 방식은 이들 모듈이 역시 JVM 클래스 경로에는 없는 추가 라이브러리가 필요한 경우 현재 스레드의 컨텍스트 클래스 로더를 수정해야 할 수도 있습니다.

    다음 코드 샘플은 파일 배열을 사용하여 임베드 가능 컨테이너를 시작하는 방법을 설명합니다.

    ...
    // Create the properties object to pass to the embeddable container:
    Map<String,Object> props = new HashMap<String,Object>();
    
    // Specify the EJB modules to start when creating the container:
    File[] ejbModules = new File[2];
    ejbModules[0] = new File("/home/myusername/ejbs/ShoppingCartEJB.jar");
    ejbModules[1] = new File("/home/myusername/ejbs/OnlineCatalogEJB.jar");
    props.put(EJBContainer.MODULES, ejbModules);
    
    // In this example, both of these modules rely on code in a shared library.
    // In order for the embeddable container to load the shared library, the
    // context classloader must be able to load that shared library.
    
    // Set up the context classloader so that it can load the shared library:
    File sharedLibUtilityFile = new File("/home/myusername/ejbs/SharedLib.jar");
    ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
    ClassLoader newCL = new URLClassLoader(new URL[]{
       sharedLibUtilityFile.toURI().toURL()}, oldCL);
    Thread.currentThread().setContextClassLoader(newCL);
    
    // Now, create the embeddable container, passing it the properties map:
    EJBContainer ec = EJBContainer.createEJBContainer(props);
    
    // Invoke an EJB loaded by the embeddable container:
    ...

    Bean 인스턴스를 검색한 후 해당 인스턴스에서 메소드를 시작하십시오. 컨테이너 관련 태스크를 완료할 때 컨테이너를 닫으십시오. 그러면 PreDestroy로 표시된 Bean 메소드가 시작되고 임베드 가능 컨테이너가 닫힙니다. 새 것을 작성하기 전에 임베드 가능 컨테이너 인스턴스를 닫으십시오.

  3. 임베드 가능 컨테이너를 사용자 정의하십시오. 특성을 사용하여 임베드 가능 EJB 컨테이너 런타임을 사용자 정의할 수 있습니다. 지원되는 특성의 전체 목록은 임베드 가능 EJB 컨테이너 사용자 정의 특성 주제를 참조하십시오.
  4. 애플리케이션이 데이터 소스 같은 자원을 사용하기 원하는 경우, 임베드 가능 컨테이너로 전달되는 특성 맵이나 특성 파일에서 해당 자원을 작성하고 구성할 수 있습니다.

    임베드 가능 EJB 컨테이너의 한 가지 공통적인 사용법은 최종적으로 애플리케이션 서버에서 실행하는 애플리케이션을 테스트하는 것입니다. 이들 애플리케이션의 대다수는 관리 콘솔이나 wsadmin 스크립트 도구를 사용하여 서버에서 구성되는 JDBC 데이터 소스에 의존합니다. 이들 도구는 임베드 가능 컨테이너에 존재하지 않으므로, 특성을 전달하여 이들 자원을 제공하도록 WebSphere 임베드 가능 컨테이너를 구성할 수 있습니다.

    데이터 소스 구성 특성은 특성 파일에도 저장할 수 있습니다. 임베드 가능 컨테이너는 현재 작업 디렉토리의 embeddable.properties라는 파일에 저장된 특성을 자동으로 로드합니다. com.ibm.websphere.embeddable.configFileName 시스템 특성의 값으로 새 파일 위치를 지정하여 이 파일 위치를 대체할 수 있습니다.

    데이터 소스 구성 특성은 모두 DataSource로 시작하며, 구성되고 있는 데이터 소스를 식별하는 용어가 뒤따릅니다. 예를 들어, DataSource.myDataSource.somePropertyDataSource.anotherDS.someOtherProperty라고 이름 지정된 것과는 다른 데이터 소스에 적용됩니다. 데이터 소스 특성의 목록은 임베드 가능 EJB 컨테이너 사용자 정의 특성 정보에 위치합니다.

    다음은 애플리케이션이 데이터 소스를 사용할 수 있는 방법의 예입니다.

    ...
    InitialContext context = new InitialContext();
    DataSource ds = (DataSource) context.lookup("env/jdbc/AcctsPayableDS");
    Connection conn = ds.getConnection();
    // Use the connection to access the AcctsPayableDS database
    ...

    서버에서 시스템 관리자가 데이터 소스를 작성했고 이를 JNDI 네임스페이스에서 env/jdbc/AcctsPayableDS에 바인드했습니다. 그렇지 않으면 코드가 env/jdbc/AcctsPayableDS에 맵핑되거나 데이터 소스와 함께 삽입될 EJB 필드를 지정하는 java:comp 네임스페이스에서 데이터 소스를 검색했을 수 있습니다. 모든 경우에 데이터 소스가 네임스페이스에서 바인드되어야 합니다. 임베드 가능 컨테이너 인스턴스를 작성할 때 이 조치를 프로그래밍 방식으로 완료하려면 다음 코드를 사용하십시오.

    ...
    // Create a properties map to store embeddable container config properties
    Map<String,Object> props = new HashMap<String,Object>();
    
    // Set the JNDI name to bind this data source:
    props.put("DataSource.ds1.name", "env/jdbc/AcctsPayableDS");
    
    // Set the data source class name ; this is a required property
    // This example uses a Derby JDBC driver
    props.put("DataSource.ds1.dataSourceClass",
       "org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource");
    
    // Set the database name
    props.put("DataSource.ds1.databaseName", "AcctsPayableTestDB");
    
    // Create the embeddable container instance with our custom properties
    EJBContainer ec = EJBContainer.createEJBContainer(props);
    
    // Now invoke an EJB in the embeddable container...
    ...

    앞의 코드는 AcctsPayableTestDB라는 Apache Derby 데이터베이스에 대한 단순 데이터 소스를 작성하고 이를 env/jdbc/AcctsPayableDS에서 바인드합니다. 다음 텍스트를 JVM의 현재 작업 디렉토리에 있는 embeddable.properties라는 파일에 넣어서 이와 동일한 태스크를 선언적으로 완료할 수 있습니다. 또한 이 텍스트를 임의의 텍스트 파일에 넣고 해당 텍스트 파일을 com.ibm.websphere.embeddable.configFileName 시스템 특성에서 지정할 수도 있습니다.

    DataSource.ds1.name=env/jdbc/AcctsPayableDS
    DataSource.ds1.dataSourceClass=org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource
    DataSource.ds1.databaseName=AcctsPayableTestDB

    데이터 소스를 직접 검색하지 않고 EJB를 개발할 때는 자원 참조를 사용하십시오.

  5. 애플리케이션은 선언적 및 프로그램 방식의 Java EE 역할 기반 보안을 둘 다 사용하여 EJB 역할 기반 보안을 확인할 수 있습니다. EJB 역할 기반 보안을 확인하기 위해 다음 조치를 완료할 수 있습니다.
    • 권한 부여 목적으로 사용될 사용자를 정의하십시오.
    • 사용자에게 EJB에서 선언되는 역할을 지정하십시오.
    • EJB에서 EJBContext methods, isCallerInRole() 및 getCallerPrincipal()의 사용을 테스트하십시오.

    다음은 애플리케이션이 선언적 및 프로그램 방식의 보안을 사용하는 방법의 예입니다.

    import java.util.HashMap;
    import java.util.Map;
    import javax.ejb.EJBContainer;
    import my.pkg.MyBeanIface; // this is the local business interface of the
                               // enterprise bean
    public class EmbeddableContainerSample {
    
       public static void main(String[] args) throws Throwable {
          // Create a properties map to pass to the embeddable container:
          Map<String,Object> properties = new HashMap<String,Object>();
          // Specify that you want to use the WebSphere embeddable container:
          properties.put(EJBContainer.PROVIDER, 
              "com.ibm.websphere.ejbcontainer.EmbeddableContainerProvider");
          
          // Specify that you want security checking enabled:
          properties.put("com.ibm.websphere.securityEnabled", "true");
          
          // Assign the users bob, fred, and mary to the role employee:
          props.put("role.employee", "bob, fred, mary");
          // Assign the user fred to the role manager:
          props.put("role.manager", "fred");
          // The user fred will be used for the runAs role manager:
          props.put("role.runAs.manager", "fred");
          // The user fred will be used for role authorization when invoking 
          // methods on the EJB:
          props.put("user.invocation", "fred");
          // Create the container instance, passing it the properties map:
          EJBContainer ec = EJBContainer.createEJBContainer(properties);
          // Use the container context to look up a bean:
          MyBeanIface bean = ec.getContext().lookup(
              "java:global/MyEJBModule/MyBean!my.pkg.MyBeanIface");
          // Invoke a method on the bean instance:
          bean.doStuff();
          ...
          // Close the embeddable container:
          ec.close();
       }
    }

    앞의 코드는 보안을 사용으로 설정한 후 employeemanager의 두 역할과 bob, mary, fred의 세 사용자를 작성합니다. 그런 다음 manager 역할로 실행 중일 때 fred 사용자가 사용되도록 지정합니다. 임베드 가능 컨테이너를 작성하기 전에 호출하는 사용자를 fred로 설정합니다. 이것은 EJB 메소드가 시작될 때 fred에 의해 시작됨을 의미하며, 이것은 해당 메소드가 employee 또는 manager 역할이 필요한 경우 fred가 해당 메소드에 액세스할 수 있음을 의미합니다.

  6. 애플리케이션은 Bean별로 로컬 트랜잭션 포함(LTC) 동작을 지정할 수 있습니다. LTC의 설명은 로컬 트랜잭션 포함에 대해 읽으십시오.

    다음은 애플리케이션이 LTC 분석기 및 해석되지 않은 조치를 지정할 수 있는 방법의 예입니다.

    import java.util.HashMap;
    import java.util.Map;
    import javax.ejb.EJBContainer;
    import my.pkg.MyBeanIface; // this is the local business interface 
                               // of the enterprise bean
    
     public class EmbeddableContainerSample {
    
       public static void main(String[] args) throws Throwable {
    
          // Create a properties map to pass to the embeddable container:
          Map<String,Object> properties = new HashMap<String,Object>();
          // Specify that you want to use the WebSphere embeddable container:
          properties.put(EJBContainer.PROVIDER, 
              "com.ibm.websphere.ejbcontainer.EmbeddableContainerProvider");
          
          // Specify that you want the LTC resolver container-at-boundary:
          properties.put("Bean.myApp1#moduleA#bean101.LocalTransaction.Resolver",
                                                          "ContainerAtBoundary");
          
          // Specify that you want the LTC unresolved action commit:
          properties.put("Bean.myApp1#moduleA#bean101.LocalTransaction.UnresolvedAction",
                                                          "Commit");
          
          // Create the container instance, passing it the properties map:
          EJBContainer ec = EJBContainer.createEJBContainer(properties);
          // Use the container context to look up a bean:
          MyBeanIface bean = ec.getContext().lookup(
              "java:global/MyEJBModule/MyBean!my.pkg.MyBeanIface");
          // Invoke a method on the bean instance:
          bean.doStuff();
          ...
          // Close the embeddable container:
          ec.close();
       }
    }

    앞의 코드는 지정된 Bean에 대한 분석기 조치를 container-at-boundary의 기본이 아닌 값으로 설정하고 해석되지 않은 조치가 트랜잭션을 커미트하는 기본이 아닌 조치가 되게 만듭니다.

    임베드 가능 컨테이너를 실행할 때 애플리케이션 이름이 지정되지 않는 경우 <application_name>을 생략해야 합니다. 그러나 # 구분 기호를 사용해야 합니다. 예를 들어 다음과 같습니다.
    properties.put("Bean.#moduleA#bean101.LocalTransaction.UnresolvedAction", "Commit"); 

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



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