예: JNDI로 EJB 홈 또는 비즈니스 인터페이스 검색
JNDI(Java™ Naming and Directory Interface)를 사용하는 대부분의 애플리케이션은 컨테이너에서 사용합니다. 일부는 그렇지 않습니다. 애플리케이션이 컨테이너에서 실행되는지 여부에 따라 오브젝트 검색에 사용하는 이름이 다릅니다. 애플리케이션이 검색 이름으로 corbaname URL을 사용하는 것이 보다 편리할 경우가 있습니다. 컨테이너 기반 JNDI 클라이언트 및 thin Java 클라이언트는 corbaname URL을 사용할 수 있습니다.
다음 예는 다른 유형의 애플리케이션에서 JNDI 검색을 수행하는 방법을 표시합니다.
컨테이너에서 실행 중인 애플리케이션에서 JNDI 검색
컨테이너에서 실행하는 애플리케이션은 java: 검색 이름을 사용할 수 있습니다. 이름 서버의 네임스페이스에 바인드되기 때문에 이 양식의 검색 이름은 오브젝트를 검색하는 데 사용된 검색 이름이 오브젝트의 이름에 종속되지 않도록 간접 레벨을 제공합니다. 애플리케이션에 대한 배치 디스크립터는 java: 이름 및 이름 서버 검색 이름에서 맵핑을 제공합니다. java: 이름이 해당 오브젝트로 제대로 맵핑되도록 컨테이너는 배치 디스크립터 정보를 기반으로 java: 네임스페이스를 설정합니다.
// Get the initial context as shown in a previous example.
...
// Look up the business interface using the JNDI name.
try {
java.lang.Object ejbBusIntf =
initialContext.lookup(
"java:comp/env/com/mycompany/accounting/Account");
accountIntf =
(Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
}
catch (NamingException e) { // Error getting the business interface
...
}
다음 예는 EJB 1.x 또는 2.x EJB 홈의 검색을 표시합니다. 실제 홈 검색 이름은 애플리케이션의 배치 디스크립터에서 판별됩니다. 엔터프라이즈 Bean(EJB)은 EJB 컨테이너에 상주하며, Bean 및 상주하는 애플리케이션 서버 사이에 인터페이스를 제공합니다.
// Get the initial context as shown in a previous example
...
// Look up the home interface using the JNDI name
try {
java.lang.Object ejbHome =
initialContext.lookup(
"java:comp/env/com/mycompany/accounting/AccountEJB");
accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
(org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
catch (NamingException e) { // Error getting the home interface
...
}
컨테이너에서 실행되지 않는 애플리케이션에서 JNDI 검색
컨테이너에서 실행하지 않는 애플리케이션은 java: 검색 이름을 사용할 수 없습니다. 애플리케이션에 대한 java: 네임스페이스를 설정하는 컨테이너이기 때문입니다. 대신, 이 유형의 애플리케이션은 이름 서버에서 직접 오브젝트를 검색해야 합니다. 각 애플리케이션 서버는 이름 서버를 포함합니다. EJB 홈과 같은 시스템 아티팩트는 해당 이름 서버의 서버 루트 컨텍스트에 관련되어 바인드됩니다. 여러 이름 서버는 시스템 네임스페이스 구조를 사용하여 연합됩니다. 다른 서버에서 오브젝트를 검색하기 위한 권장 방법은 이름이 셀의 초기 컨텍스트에서 해석되도록 이름을 규정하는 것입니다. 상대 이름이 사용되면 초기 컨텍스트는 바인드된 오브젝트 아래 컨텍스트와 동일한 서버 루트 컨텍스트여야 합니다. 규정된 이름의 양식은 규정된 이름이 토폴로지 기반 이름이거나 또는 고정된 이름인지에 따라 다릅니다. 규정된 이름의 각 양식의 예는 다음과 같습니다.
- 토폴로지 기반 규정된 이름
토폴로지 기반 규정된 이름은 바인드된 대상 오브젝트 아래 서버 루트 컨텍스트로 시스템 네임스페이스를 통해 순회합니다. 토폴로지 기반 규정된 이름은 셀의 초기 컨텍스트에서 분석됩니다.
토폴로지 기반 규정된 이름은 오브젝트가 단일 서버나 서버 클러스터에 상주하는지 여부에 따라 다릅니다. 각 검색의 예가 다음에 표시됩니다.
- 단일 서버
- 다음 예는 단일 서버, MyServer에서 실행되고 노드 Node1에서 구성된 EJB 비즈니스 인터페이스의 검색을 표시합니다.
// Get the initial context as shown in a previous example. // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the business interface using the JNDI name try { java.lang.Object ejbBusIntf = initialContext.lookup( "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/Account"); accountIntf = (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); } catch (NamingException e) { // Error getting the business interface ... }
다음 예는 단일 서버, MyServer에서 실행 중이고 노드 Node1에서 구성된 EJB 홈의 검색을 표시합니다.
// Get the initial context as shown in a previous example // Using the form of lookup name below, it doesn't matter which // server in the cell is used to obtain the initial context. ... // Look up the home interface using the JNDI name try { java.lang.Object ejbHome = initialContext.lookup( "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/AccountEJB"); accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( (org.omg.CORBA.Object) ejbHome, AccountHome.class); } catch (NamingException e) { // Error getting the home interface ... }
- 서버 클러스터
- 다음 예는 클러스터, MyCluster에서 실행 중인 EJB 비즈니스의 검색을 표시합니다.
클러스터 멤버가 실행 중인 경우 이름은 분석할 수 있습니다.
// Get the initial context as shown in a previous example. // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the business interface using the JNDI name try { java.lang.Object ejbBusIntf = initialContext.lookup( "cell/clusters/MyCluster/com/mycompany/accounting/Account"); accountIntf = (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); } catch (NamingException e) { // Error getting the business interface ... }
다음 예는 클러스터, MyCluster에서 실행 중인 EJB 홈의 검색을 표시합니다. 클러스터 멤버가 실행 중인 경우 이름은 분석할 수 있습니다.
// Get the initial context as shown in a previous example // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the home interface using the JNDI name try { java.lang.Object ejbHome = initialContext.lookup( "cell/clusters/MyCluster/com/mycompany/accounting/AccountEJB"); accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( (org.omg.CORBA.Object) ejbHome, AccountHome.class); } catch (NamingException e) { // Error getting the home interface ... }
- 고정 규정된 이름
대상 오브젝트에 셀 범위의 고정된 이름이 정의된 경우, 토폴로지 기반 규정된 이름 대신 규정된 양식을 사용할 수 있습니다. 토폴로지 기반 이름이 동작하더라도, 고정된 이름은 특정 셀 토폴로지로 또는 다른 서버로의 대상 오브젝트 이동으로 변경되지 않습니다.
규정된 고정 이름으로 EJB 비즈니스 인터페이스의 예 검색은 다음에 표시됩니다.// Get the initial context as shown in a previous example. // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the business interface using the JNDI name try { java.lang.Object ejbBusIntf = initialContext.lookup( "cell/persistent/com/mycompany/accounting/Account"); accountIntf = (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); } catch (NamingException e) { // Error getting the business interface ... }
규정된 고정 이름으로 예 검색은 다음과 같습니다.
// Get the initial context as shown in a previous example // Using the form of lookup name below, it doesn't matter which // server in the cell is used to obtain the initial context. ... // Look up the home interface using the JNDI name try { java.lang.Object ejbHome = initialContext.lookup( "cell/persistent/com/mycompany/accounting/AccountEJB"); accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( (org.omg.CORBA.Object) ejbHome, AccountHome.class); } catch (NamingException e) { // Error getting the home interface ... }
corbaname URL로 JNDI 검색
corbaname은 때로는 검색 이름으로 유용할 수 있습니다. 예를 들어, 대상 오브젝트가 연합된 네임스페이스의 멤버가 아니고 규정된 이름으로 찾을 수 없는 경우, corbaname은 오브젝트를 검색하는 편리한 방법일 수 있습니다.
// Get the initial context as shown in a previous example.
...
// Look up the business interface using a corbaname URL.
try {
java.lang.Object ejbBusIntf = initialContext.lookup(
"corbaname:iiop:someHost:2809#com/mycompany/accounting/Account");
accountIntf =
(Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
}
catch (NamingException e) { // Error getting the business interface
...
}
corbaname URL을 사용한 검색은 다음과 같습니다.
// Get the initial context as shown in a previous example
...
// Look up the home interface using a corbaname URL
try {
java.lang.Object ejbHome = initialContext.lookup(
"corbaname:iiop:someHost:2809#com/mycompany/accounting/AccountEJB");
accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
(org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
catch (NamingException e) { // Error getting the home interface
...
}