【收集】EJB3.0的各应用服务器提供的JNDI接口
生活随笔
收集整理的這篇文章主要介紹了
【收集】EJB3.0的各应用服务器提供的JNDI接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于JNDI and EJB JNDI?(The Java Naming and Directory Interface,Java?命名和目錄接口)?是一組在Java?應用中訪問命名和目錄服務的API。為開發人員提供了查找和訪問各種命名和目錄服務的通用、統一的方式。借助于JNDI?提供的接口,能夠通過名字定位用戶、機器、網絡、對象服務等。 命名服務:就像DNS一樣,通過命名服務器提供服務,大部分的J2EE?服務器都含有命名服務器。 目錄服務:一種簡化的RDBMS?系統,通過目錄具有的屬性保存一些簡單的信息。目錄服務通過目錄服務器實現,比如微軟ACTIVE DIRECTORY?等。 JNDI?的好處: (1)包含大量命名和目錄服務,可以使用相同API?調用訪問任何命名或目錄服務。 (2)可以同時連接多個命名和目錄服務。 (3)允許把名稱同JAVA?對象或資源關聯起來,不必知道對象或資源的物理ID。 (4)使用通用接口訪問不同種類的目錄服務 (5)使得開發人員能夠集中使用和實現一種類型的命名或目錄服務客戶API?上。 上下文:由0?或多個綁定構成。比如java/MySql,java?為上下文(context),MySql?為命名 子上下文(subConext):上下文下的上下文。比如MyJNDITree/ejb/helloBean,ejb?為子上下文。 因為JNDI?是一組接口,所以我們只需根據接口規范編程就可以。要通過JNDI?進行資源訪問,我們必須設置初始化上下文的參數,主要是設置JNDI?驅動的類名(java.naming.factory.initial)?和提供命名服務的URL (java.naming.provider.url)。 因為Jndi?的實現產品有很多。所以java.naming.factory.initial?的值因提供JNDI?服務器的不同而不同,java.naming.provider.url?的值包括提供命名服務的主機地址和端口號。 訪問Jboss?服務器的例子代碼: Properties?props?=?new?Properties();
props.setProperty("java.naming.factory.initial",?"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url",?"localhost:1099");
InitialContext?=?new?InitialContext(props);
HelloWorld?helloworld?=?(HelloWorld)?ctx.lookup("HelloWorldBean/remote");
訪問Sun?應用服務器的例子代碼: Properties?props?=?new?Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.provider.url",?"localhost:3700");
InitialContext?=?new?InitialContext(props);
HelloWorld?helloworld?=?(HelloWorld)?ctx.lookup("com.foshanshop.ejb3.HelloWorld");
訪問Weblogic10?應用服務器的例子代碼: Properties?props?=?new?Properties();
props.setProperty("java.naming.factory.initial",?"weblogic.jndi.WLInitialContextFactory");
props.setProperty("java.naming.provider.url",?"t3://localhost:7001");
InitialContext?=?new?InitialContext(props);
HelloWorld?helloworld?=?(HelloWorld)?ctx.lookup("HelloWorldBean#com.foshanshop.ejb3.HelloWorld");
JBOSS?環境下JNDI?樹的命名約定: (1)java:copm?這個上下文環境和其子上下文環境僅能被與之相關的特定應用組件訪問和使用 (2)java: 子上下文環境和綁定的對象只能被Jboss?服務器虛擬機內的應用訪問 (3)其他上下文環境?只要實現序列化就可以被遠程用戶調用。
...
@Remote
@RemoteBinding?(jndiBinding="testbinding/myAccount")
publicinterface?MyAccount?extends?Serializable?{
????publicint?Add(int?a,?int?b);
????publicint?getResult()?;
}
?
那么在client端調用上面的EJB的代碼為:
InitialContext?ctx?=?new?InitialContext(props);
MyAccount?bean1?=?(MyAccount)?ctx.lookup("testbinding/myAccount");
?
props.setProperty("java.naming.factory.initial",?"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url",?"localhost:1099");
InitialContext?=?new?InitialContext(props);
HelloWorld?helloworld?=?(HelloWorld)?ctx.lookup("HelloWorldBean/remote");
訪問Sun?應用服務器的例子代碼: Properties?props?=?new?Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.provider.url",?"localhost:3700");
InitialContext?=?new?InitialContext(props);
HelloWorld?helloworld?=?(HelloWorld)?ctx.lookup("com.foshanshop.ejb3.HelloWorld");
訪問Weblogic10?應用服務器的例子代碼: Properties?props?=?new?Properties();
props.setProperty("java.naming.factory.initial",?"weblogic.jndi.WLInitialContextFactory");
props.setProperty("java.naming.provider.url",?"t3://localhost:7001");
InitialContext?=?new?InitialContext(props);
HelloWorld?helloworld?=?(HelloWorld)?ctx.lookup("HelloWorldBean#com.foshanshop.ejb3.HelloWorld");
JBOSS?環境下JNDI?樹的命名約定: (1)java:copm?這個上下文環境和其子上下文環境僅能被與之相關的特定應用組件訪問和使用 (2)java: 子上下文環境和綁定的對象只能被Jboss?服務器虛擬機內的應用訪問 (3)其他上下文環境?只要實現序列化就可以被遠程用戶調用。
?
?
當你把EJB發布到JBOSS后,你就可以在jboss?的管理平臺查看她們的JNDI?名,輸入下面URL?http://localhost:8080/jmx-console/,點擊“service=JNDIView” link,在出現的page里找到“List of MBean operations:”欄的“list()”方法,click “Invoke”button,就會看到下面的界面 在上圖中可以看見HelloWorld?會話Bean?的JNDI路徑,JNDI?路徑名的組成規則是“上層名稱/下層名稱,每層之間以”/”分隔。HelloWorld?會話Bean?的JNDI路徑名是:HelloWorldBean/remote 下面要重點說明一下Jboss EJB JNDI?名稱默認的命名規則,命名規則如下: 1>?如果EJB?打包進后綴為*.ear?的J2EE?發布文件,默認的JNDI?路徑名稱是 訪問本地接口:EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local 訪問遠程接口:EAR-FILE-BASE-NAME/EJB-CLASS-NAME/remote 例:EJB HelloWorld?打包進名為?HelloWorld.ear?的J2EE?應用,訪問她遠程接口的JNDI?名是:HelloWorld/HelloWorldBean/remote 2>?如果EJB?應用打包成后綴為*.jar?的發布文件,?默認的JNDI?路徑名稱是 訪問本地接口:EJB-CLASS-NAME/local 訪問遠程接口:EJB-CLASS-NAME/remote 例:?HelloWorld?應用打包成HelloWorld.jar?文件,訪問她遠程接口的JNDI?名稱是:HelloWorldBean/remote 注意:EJB-CLASS-NAME?是不帶包名的,如com.foshanshop.ejb3.impl.HelloWorldBean?只需取HelloWorldBean。 目前網上很多教材獲取JNDI?路徑名的方式不適用在jboss?下,如: HelloWorld helloworld = (HelloWorld) ctx.lookup(HelloWorld.class.getName()); 這種方式適用于Sun Application Server?及glassfish?
??
自定義JNDI?命名 默認的JNDI?命名規則上面已經介紹過,但有些情況下需要自定義名稱。在Jboss?中要自定義JNDI?名稱,可以使用@LocalBinding?和@RemoteBinding?注釋 關鍵代碼(只需要在bean interface前加binding注釋): import?org.jboss.annotation.ejb.RemoteBinding;...
@Remote
@RemoteBinding?(jndiBinding="testbinding/myAccount")
publicinterface?MyAccount?extends?Serializable?{
????publicint?Add(int?a,?int?b);
????publicint?getResult()?;
}
?
那么在client端調用上面的EJB的代碼為:
InitialContext?ctx?=?new?InitialContext(props);
MyAccount?bean1?=?(MyAccount)?ctx.lookup("testbinding/myAccount");
?
轉載于:https://www.cnblogs.com/anme/archive/2009/10/12/1582016.html
總結
以上是生活随笔為你收集整理的【收集】EJB3.0的各应用服务器提供的JNDI接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#类中操作UI层控件状态[原]
- 下一篇: Delphi 2010 新增功能之: I