廉頗老矣,尚能飯否

          java:從技術到管理

          常用鏈接

          統計

          最新評論

          JNDI簡單入門

          JNDI就是為JAVA中命名和目錄服務定義的JAVA API,是命名服務的抽象機制。在J2EE中,JNDI的目的是用來查找J2EE服務器的注冊資源只要該對象在命名服務器上注冊過,且你知道命名服務器的地址和該對象在命名服務器上注冊的JNDI名。這樣你就可以在無需知道對象位置的情況下獲取和使用對象。SUN對JNDI只提供接口,使用JNDI只需要用到JNDI接口而不必關心具體實現。
          使用main方法做JNDI的demo時出現NoInitialContextException是因為無法從System.properties中獲得必要的JNDI參數,在服務器環境下,服務器啟動時就把這些參數放到System.properties中了,于是直接new InitialContext()就搞定了。但是在單機環境下,可沒有JNDI服務在運行,那就需要手動啟動一個JNDI服務。在JDK 5的rt.jar中一共找到了4種SUN自帶的JNDI實現:LDAP,CORBA,RMI,DNS。
          這4種JNDI要正常運行還需要底層的相應服務。一般我們沒有LDAP或CORBA服務器,也就無法啟動這兩種JNDI服務,DNS用于查域名的,以后再研究,唯一可以在main()中啟動的就是基于RMI的JNDI服務。
          現在我們就可以在main()中啟動基于RMI的JNDI服務并且綁一個對象到JNDI上。注意,我直接把JNDI的相關參數放入了System.properties中,這樣,后面的代碼如果要查JNDI,直接new InitialContext()就可以了

          在RMI中綁JNDI的限制是,綁定的對象必須是Remote類型,所以就需要自己擴展一個。其實JNDI還有兩個Context.SECURITY_PRINCIPAL和Context.SECURITY_CREDENTIAL,如果訪問JNDI需要用戶名和口令,這兩個也要提供,不過一般用不上。下面是兩個使用JNDI的簡單例子的代碼,可以直接運行。

          package com.ellen.jndi;

          import java.io.Serializable;
          import java.rmi.Remote;
          import java.rmi.RemoteException;
          import java.rmi.registry.LocateRegistry;
          import java.util.Date;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;

          //在RMI中綁JNDI的限制是,綁定的對象必須是Remote類型
          class Person implements Remote, Serializable {
           private static final long serialVersionUID = -8592182872966400365L;

           private String name;
           private String pass;

           public String getName() {
            return name;
           }

           public void setName(String name) {
            this.name = name;
           }

           public String getPass() {
            return pass;
           }

           public void setPass(String pass) {
            this.pass = pass;
           }

           public String toString() {
            return "name=" + this.getName() + "&pass=" + this.getPass();
           }

          }

          // 在RMI中綁JNDI的限制是,綁定的對象必須是Remote類型
          // 外部擴展,可以內部擴展也可以外部擴展
          class RemoteDate extends Date implements Remote {
          };

          public class Demo {
           public static void initDate() throws NamingException, RemoteException {
            // 設置參數
            LocateRegistry.createRegistry(1099);
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
            System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
            InitialContext ctx = new InitialContext();
            // 在RMI中綁JNDI的限制是,綁定的對象必須是Remote類型
            // 內部擴展,可以內部擴展也可以外部擴展
            class RemoteDate extends Date implements Remote {
            }
            ;
            ctx.bind("java:comp/env/systemStartTime", new RemoteDate());
            ctx.close();
           }

           public static void initDate2() throws NamingException, RemoteException {
            // 設置參數
            LocateRegistry.createRegistry(1099);
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
            System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
            InitialContext ctx = new InitialContext();
            // 自己擴展,可以內部擴展也可以外部擴展
            // class RemoteDate extends Date implements Remote {
            // }
            // ;
            ctx.bind("java:comp/env/systemStartTime", new RemoteDate());
            ctx.close();
           }

           public static void findDate() throws NamingException, RemoteException {
            // 直接使用
            InitialContext ctx = new InitialContext();
            Date startTime = (Date) ctx.lookup("java:comp/env/systemStartTime");
            System.out.println("+++++++++++++++++++++++" + startTime.toString());
            ctx.close();
           }

           public static void jndiDate() throws NamingException, RemoteException {
            // Demo.initDate();
            Demo.initDate2();
            Demo.findDate();
           }

           public static void initPerson() throws NamingException, RemoteException {
            // 設置參數
            LocateRegistry.createRegistry(1099);
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
            System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
            InitialContext ctx = new InitialContext();
            // Person person = new Person();
            // person.setName("ellen");
            // person.setPass("000727");
            ctx.bind("java:comp/env/person", new Person());
            ctx.close();
           }

           public static void initPerson2() throws NamingException, RemoteException {
            // 設置參數
            LocateRegistry.createRegistry(1099);
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
            System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
            InitialContext ctx = new InitialContext();
            Person person = new Person();
            person.setName("ellen");
            person.setPass("000727");
            ctx.bind("java:comp/env/person", person);
            ctx.close();
           }

           public static void findPerson() throws NamingException, RemoteException {
            // 直接使用
            InitialContext ctx = new InitialContext();
            Person person = (Person) ctx.lookup("java:comp/env/person");
            System.out.println("------" + person.toString());
            System.out.println("------" + person.getName());
            System.out.println("------" + person.getPass());
            ctx.close();
           }

           public static void jndiPerson() throws NamingException, RemoteException {
            // Demo.initPerson();
            Demo.initPerson2();
            Demo.findPerson();
           }

           public static void main(String[] args) throws NamingException, RemoteException {
            // 為什么兩個jndi的例子不能同時運行
            // internal error: ObjID already in use
            // Demo.jndiDate();
            Demo.jndiPerson();
           }

          }



          柳德才
          13691193654
          18942949207
          QQ:422157370
          liudecai_zan@126.com
          湖北-武漢-江夏-廟山

          posted on 2010-06-20 09:31 liudecai_zan@126.com 閱讀(2206) 評論(1)  編輯  收藏 所屬分類: 重塑金身:2010-06-08

          評論

          # re: JNDI簡單入門 2011-04-28 09:47 路過

          因為綁定的是同一個端口,將initDate和initPerson的端口修改為不同的就可以了  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 河东区| 芦山县| 麻栗坡县| 美姑县| 肇州县| 色达县| 广安市| 荣昌县| 蒲城县| 夏津县| 台州市| 房山区| 依兰县| 孟连| 黄平县| 资兴市| 浮梁县| 新疆| 阿鲁科尔沁旗| 海城市| 黄陵县| 安达市| 娱乐| 安义县| 许昌市| 岫岩| 大洼县| 额敏县| 黄浦区| 穆棱市| 阳谷县| 桓台县| 桐梓县| 阿坝县| 噶尔县| 从江县| 石渠县| 封丘县| 绥中县| 天水市| 陆川县|