<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    改變開發方式的 hbm+rmic

    Posted on 2007-12-24 13:36 G_G 閱讀(1102) 評論(4)  編輯  收藏 所屬分類: EJB
    為了了解hbm的rmic真正的威力還有為了更近一步了解;在此特使用文本和dos命令完成這次學習。
    在此讀者最好先做好不使用 IDE 的準備(^_^!? 文本編輯,dos編輯和運行)
    閱讀需要:
    ?java,javac,rmiregistry,rmic,ant 等命令有所涉及(只是簡單使用,不會也別怕?。?/font>

    可行性使用說明:
    ? 持久層程序員成功開啟rmiregistry服務,在局域網內其他使用數據的隊友就只要加載
    hbmBean 的映射jar和 dao接口jar后 就可以取得數據。根本感覺不到hbn使用。
    優點:
    1.其他隊員編譯classpath中不需要hbm的任何東西,跟關注自己東西。
    2.強制接口規范。
    3.正交編程。
    4.DAO測試性能方便。
    5.這還是 EJB 原理,轉換容易。

    缺點:
    1.rmic 突破放火墻能力有限,可以換成EJB
    2.大型項目注冊服務器,配置繁瑣,可以使用EJB3.0
    ?


    使用:
    1.jdk1.5
    2.jar使用(為了用hbm)?????
    ? ? jta.jar;
    ??? asm.jar;
    ? ? commons-collections-2.1.1.jar;
    ? ? commons-logging-1.0.4.jar;
    ? ? hibernate3.jar;
    ??? dom4j-1.6.1.jar;
    ??? cglib-2.1.3.jar;
    ? ? antlr-2.7.5H3.jar
    ??? MYSQL.jar
    3.ant1.7(使用hbm生成 mapping;config...)
    4.Middlegen-Hibernate-r5 同上
    5.使用文件路徑
    ? E:\rmi_test->
    ???? +-src
    ???? --hibernate3.xml
    ???? --hibernate3_ant.properties

    開始:
    ?1>使用ant 對 hbm? 配置映射參考 :ant 項目實際 并使用 hibernate3.xml
    ??? dos 輸入 -> ant -f hibernate3.xml ddl2hbm (使用了Middlegen-Hibernate-r5)
    ???????????? -> ant -f hibernate3.xml create_config (hibernate.cfg.xml建立)
    ??? 在 hibernate.cfg.xml中添加-> <mapping resource="hibernate\Liu.hbm.xml" />
    ??? dos 輸入 -> ant -f hibernate3.xml hbm2java
    ?
    變成+-src
    ?????? |-hibernate
    ????????? |-Liu.hbm.xml
    ????????? |-Liu.java
    ?????? |-hbmConfig
    ????????? |-hibernate.cfg.xml
    ?2>手動建hbmHibernateSessionFactory在hbmConfig文件中(為了簡單copy->myeclipes生成的)
    package???hbmConfig;?
    import?org.hibernate.HibernateException;
    import?org.hibernate.Session;
    import?org.hibernate.cfg.Configuration;
    /**
    ?*?Configures?and?provides?access?to?Hibernate?sessions,?tied?to?the
    ?*?current?thread?of?execution.??Follows?the?Thread?Local?Session
    ?*?pattern,?see?{
    @link?http://hibernate.org/42.html}.
    ?
    */
    public?class?HibernateSessionFactory?{

    ????
    /**?
    ?????*?Location?of?hibernate.cfg.xml?file.
    ?????*?NOTICE:?Location?should?be?on?the?classpath?as?Hibernate?uses
    ?????*?#resourceAsStream?style?lookup?for?its?configuration?file.?That
    ?????*?is?place?the?config?file?in?a?Java?package?-?the?default?location
    ?????*?is?the?default?Java?package.<br><br>
    ?????*?Examples:?<br>
    ?????*?<code>CONFIG_FILE_LOCATION?=?"/hibernate.conf.xml".?
    ?????*?CONFIG_FILE_LOCATION?=?"/com/foo/bar/myhiberstuff.conf.xml".</code>?
    ?????
    */
    ????
    private?static?String?CONFIG_FILE_LOCATION?=?"/hbmConfig/hibernate.cfg.xml";

    ????
    /**?Holds?a?single?instance?of?Session?*/
    ????
    private?static?final?ThreadLocal<Session>?threadLocal?=?new?ThreadLocal<Session>();

    ????
    /**?The?single?instance?of?hibernate?configuration?*/
    ????
    private?static?final?Configuration?cfg?=?new?Configuration();

    ????
    /**?The?single?instance?of?hibernate?SessionFactory?*/
    ????
    private?static?org.hibernate.SessionFactory?sessionFactory;

    ????
    /**
    ?????*?Returns?the?ThreadLocal?Session?instance.??Lazy?initialize
    ?????*?the?<code>SessionFactory</code>?if?needed.
    ?????*
    ?????*??
    @return?Session
    ?????*??
    @throws?HibernateException
    ?????
    */
    ????
    public?static?Session?currentSession()?throws?HibernateException?{
    ????????Session?session?
    =?(Session)?threadLocal.get();

    ????????
    if?(session?==?null)?{
    ????????????
    if?(sessionFactory?==?null)?{
    ????????????????
    try?{
    ????????????????????cfg.configure(CONFIG_FILE_LOCATION);
    ????????????????????sessionFactory?
    =?cfg.buildSessionFactory();
    ????????????????}
    ????????????????
    catch?(Exception?e)?{
    ????????????????????System.err.println(
    "%%%%?Error?Creating?SessionFactory?%%%%");
    ????????????????????e.printStackTrace();
    ????????????????}
    ????????????}
    ????????????session?
    =?sessionFactory.openSession();
    ????????????threadLocal.set(session);
    ????????}

    ????????
    return?session;
    ????}

    ????
    /**
    ?????*??Close?the?single?hibernate?session?instance.
    ?????*
    ?????*??
    @throws?HibernateException
    ?????
    */
    ????
    public?static?void?closeSession()?throws?HibernateException?{
    ????????Session?session?
    =?(Session)?threadLocal.get();
    ????????threadLocal.set(
    null);

    ????????
    if?(session?!=?null)?{
    ????????????session.close();
    ????????}
    ????}

    ????
    /**
    ?????*?Default?constructor.
    ?????
    */
    ????
    private?HibernateSessionFactory()?{
    ????}

    }

    dos中編譯->
    E:\rmi_test\src>javac -cp?? .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\hibernate3.jar???
    hbmConfig/*.java


    3>DAO實現
    接口->
    package?rmic;

    import?java.rmi.Remote;
    import?java.rmi.RemoteException;

    public?interface?IHello?extends?Remote?{
    ????
    public?String?sayHello(String?id)?throws?RemoteException;
    }

    實現類->
    package?rmic;

    import?java.rmi.Naming;
    import?java.rmi.RemoteException;
    import?java.rmi.server.UnicastRemoteObject;
    //取消顯示指定的編譯器警告!
    //參考?:?http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/SuppressWarnings.html
    @SuppressWarnings("serial")
    public?class?Hello?extends?UnicastRemoteObject?implements?IHello?{
    ????
    public?Hello()?throws?RemoteException?{
    ????????
    super();
    ????}
    ????
    ????
    public?void?rebind(String?name)?{?
    ????????
    try?{
    ????????????Naming.rebind(name,
    this);
    ????????????System.out.println(
    "Server?is?running");
    ????????}?
    catch(Exception?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????}
    ????
    public?String?sayHello(String?id)?throws?RemoteException?{
    ????????System.out.println(
    "run?Server.");
    ????????
    ????org.hibernate.Session?session?
    =?hbmConfig.HibernateSessionFactory.currentSession();
    ????org.hibernate.Query?qu?
    =?session.createQuery("from?Liu?lt?where?lt.id=:id");
    ????hibernate.Liu?ll?
    =?(hibernate.Liu)qu.setString("id"
    ,id).uniqueResult();
    ????hbmConfig.HibernateSessionFactory.closeSession();


    ????????
    return?"Hello?"+ll.getName()+"?This?is?processed?by?RMI";
    ????}

    }

    dos編譯->
    E:\rmi_test\src>javac -cp .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.
    2\hibernate3.jar? rmic/*.java

    4>java注冊機開啟
    E:\rmi_test\src>rmiregistry 1099

    5> 本例簡單服務器開啟
    package?start;

    import?java.rmi.RMISecurityManager;
    import?rmic.Hello?;

    public?class?Start?{

    ????
    public?static?void?main(String[]?args)?{
    ????????
    try?{
    ????????????
    //System.setSecurityManager(new?RMISecurityManager());
    ????????????new?Hello().rebind("RMI/Mclaren");
    ????????}?
    catch(Exception?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????}
    }

    dos編譯->E:\rmi_test\src>javac? start/*.java
    運行->
    java -classpath .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\jta.jar;
    D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\asm.jar;
    D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\commons-collections-2.1.1.jar;
    D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\commons-logging-1.0.4.jar;
    D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\hibernate3.jar;
    D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\dom4j-1.6.1.jar;
    E:\lib\MYSQL.JAR;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\cglib-2.1.3.jar;
    E:\and\ant\bin\antlr-2.7.5H3.jar
    ? start.Start


    6>客戶端
    ?得到存根Hello_Stub.class-> E:\rmi_test\src>rmic rmic.Hello
    ?換個地方:c:\\TT 并copy rmic中的IHello.class和Hello_Stub.class連同目錄rmic一同copy
    package?client;

    import?java.rmi.Naming;
    import?rmic.IHello;

    public?final?class?Client?{
    ????
    public?static?void?main(String[]?args)?{
    ????????System.out.println(
    "client.");
    ????????
    try?{
    ????????????IHello?hello?
    =?(IHello)Naming.lookup("rmi://localhost:1099/RMI/Mclaren");
    ????????????System.out.println(hello.sayHello(
    "1"));
    ????????}?
    catch(Exception?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????}

    }

    ?dos編譯-> C:\TT>javac -d . Client.java
    ?C:\TT>java client.Client
    ?client....
    ?Hello gg This is processed by RMI (gg就是數據 成功! )

    mysql> select * from liu;
    +----+------+-----+
    | id | name | avg |
    +----+------+-----+
    |? 1 | gg?? |? 24 |
    +----+------+-----+









    評論

    # re: 改變開發方式的 hbm+rmic  回復  更多評論   

    2007-12-24 15:26 by 隔葉黃鶯
    沒看到多少內容以及有意義的東西,直接就是 RMI 了,至于服務端愛做什么事情是服務端的事情了。

    # re: 改變開發方式的 hbm+rmic  回復  更多評論   

    2007-12-24 18:30 by G_G
    對于剛接觸 EJB 的我 來說
    這原理的 使用 還是 很吸引 我的

    @隔葉黃鶯
    我們位置不同吧!你可能已經一大牛了
    呵呵

    # re: 改變開發方式的 hbm+rmic  回復  更多評論   

    2007-12-25 13:05 by 隔葉黃鶯
    EJB 的遠程接口的方式調用實際確實是用的 RMI
    而且在程序中啟動RMI注冊服務比單獨命令啟動要方便些。

    # re: 改變開發方式的 hbm+rmic[未登錄]  回復  更多評論   

    2007-12-26 09:23 by G_G
    @隔葉黃鶯
    謝謝提醒

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


    網站導航:
     
    主站蜘蛛池模板: 69式国产真人免费视频| 99久热只有精品视频免费看| 99久久精品日本一区二区免费| 亚洲AV色香蕉一区二区| 三上悠亚电影全集免费| 亚洲理论电影在线观看| 国产综合免费精品久久久| 亚洲va无码专区国产乱码| 国产中文字幕在线免费观看 | 日韩免费观看视频| 亚洲AV无码AV日韩AV网站| 免费a级毛片在线观看| 免费无遮挡无码视频在线观看| 亚洲精品动漫人成3d在线| 一级毛片免费播放男男| 国产成人亚洲综合无码精品| 亚洲电影免费在线观看| 亚洲国产理论片在线播放| 免费a级毛片高清视频不卡| 毛片亚洲AV无码精品国产午夜| 亚洲精品和日本精品| 国产一级高青免费| 久久精品九九亚洲精品| 成人无遮挡裸免费视频在线观看| 亚洲av无码成人影院一区| 国产亚洲精品不卡在线| 91在线老王精品免费播放| 久久亚洲精品国产亚洲老地址 | 久久91亚洲人成电影网站| 97av免费视频| 亚洲av无码一区二区三区四区| 亚洲色大成网站WWW久久九九 | 久久WWW免费人成—看片| 亚洲制服中文字幕第一区| 在线jyzzjyzz免费视频| 99re6在线视频精品免费| 亚洲中文字幕无码久久2020| 亚洲国产成人影院播放| 亚洲免费精彩视频在线观看| 精品免费AV一区二区三区| 亚洲av无码专区国产乱码在线观看|