﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Skynet-随笔分类-EJB</title><link>http://www.blogjava.net/Good-Game/category/27862.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 24 Dec 2007 15:26:14 GMT</lastBuildDate><pubDate>Mon, 24 Dec 2007 15:26:14 GMT</pubDate><ttl>60</ttl><item><title>改变开发方式的 hbm+rmic</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/24/170028.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 24 Dec 2007 05:36:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/24/170028.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/170028.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/24/170028.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/170028.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/170028.html</trackback:ping><description><![CDATA[为了了解hbm的rmic真正的威力还有为了更近一步了解；在此特使用文本和dos命令完成这次学习。<br />在此读者最好先做好不使用 IDE 的准备（^_^!  文本编辑，dos编辑和运行）<br /><b><font color="#ffa500">阅读需要：<br /></font></b> java,javac,<font color="#ffa500"><font color="#000000">rmiregistry,rmic,ant 等命令有所涉及（只是简单使用，不会也别怕！）</font></font><br /><br /><b><font color="#ffa500">可行性使用说明：<br /></font></b><font color="#ffa500"><font color="#000000">  持久层程序员成功开启rmiregistry服务，在局域网内其他使用数据的队友就只要加载<br />hbmBean 的映射jar和 dao接口jar后 就可以取得数据。根本感觉不到hbn使用。<br />优点：<br />1.其他队员编译classpath中不需要hbm的任何东西，跟关注自己东西。<br />2.强制接口规范。<br />3.正交编程。<br />4.DAO测试性能方便。<br />5.这还是 EJB 原理，转换容易。<br /><br />缺点：<br />1.rmic 突破放火墙能力有限，可以换成EJB<br />2.大型项目注册服务器，配置繁琐，可以使用EJB3.0<br />   </font></font><b><font color="#ffa500"><br /></font></b><br /><font color="#ffa500"><b>使用:</b></font><br />1.jdk1.5<br />2.jar使用（为了用hbm）      <br />    jta.jar;<br />    asm.jar;<br />    commons-collections-2.1.1.jar;<br />    commons-logging-1.0.4.jar;<br />    hibernate3.jar;<br />    dom4j-1.6.1.jar;<br />    cglib-2.1.3.jar;<br />    antlr-2.7.5H3.jar<br />    MYSQL.jar<br />3.ant1.7(使用hbm生成 mapping;config...)<br />4.Middlegen-Hibernate-r5 同上<br />5.使用文件路径 <br />  E:\rmi_test-&gt;<br />     +-src<br />     --hibernate3.xml<br />     --hibernate3_ant.properties<br /><br /><b><font color="#ffa500">开始：<br /></font></b><font color="#ffa500"><font color="#000000"> 1&gt;使用ant 对 hbm  配置映射参考 ：</font></font><a id="viewpost1_TitleUrl" class="singleposttitle" href="/Good-Game/archive/2007/12/12/167192.html">ant 项目实际</a><font color="#ffa500"><font color="#000000"> 并使用 </font></font>hibernate3.xml <br />    dos 输入 -&gt; <b>ant -f hibernate3.xml ddl2hbm </b>(使用了Middlegen-Hibernate-r5)<br />             -&gt; <b>ant -f hibernate3.xml create_config </b>(hibernate.cfg.xml建立)<br />    在 hibernate.cfg.xml中添加-&gt; &lt;mapping resource="hibernate\Liu.hbm.xml" /&gt;<br />    dos 输入 -&gt; <b>ant -f hibernate3.xml hbm2java <br /> </b>变成+-src<br />       |-hibernate<br />          |-Liu.hbm.xml<br />          |-Liu.java<br />       |-hbmConfig<br />          |-hibernate.cfg.xml<b><br /></b> 2&gt;手动建hbm<font color="#ffa500"><font color="#000000">HibernateSessionFactory在</font></font>hbmConfig文件中<font color="#ffa500"><font color="#000000">(为了简单copy-&gt;myeclipes生成的)<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">   hbmConfig;  <br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.HibernateException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Session;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.cfg.Configuration;<br /></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /> * Configures and provides access to Hibernate sessions, tied to the<br /> * current thread of execution.  Follows the Thread Local Session<br /> * pattern, see {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://hibernate.org/42.html</span><span style="color: rgb(0, 128, 0);">}.<br /> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> HibernateSessionFactory {<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> <br />     * Location of hibernate.cfg.xml file.<br />     * NOTICE: Location should be on the classpath as Hibernate uses<br />     * #resourceAsStream style lookup for its configuration file. That<br />     * is place the config file in a Java package - the default location<br />     * is the default Java package.&lt;br&gt;&lt;br&gt;<br />     * Examples: &lt;br&gt;<br />     * &lt;code&gt;CONFIG_FILE_LOCATION = "/hibernate.conf.xml". <br />     * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".&lt;/code&gt; <br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> String CONFIG_FILE_LOCATION </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/hbmConfig/hibernate.cfg.xml</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> Holds a single instance of Session </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> ThreadLocal</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Session</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> threadLocal </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ThreadLocal</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Session</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">();<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> The single instance of hibernate configuration </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> Configuration cfg </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Configuration();<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> The single instance of hibernate SessionFactory </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> org.hibernate.SessionFactory sessionFactory;<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     * Returns the ThreadLocal Session instance.  Lazy initialize<br />     * the &lt;code&gt;SessionFactory&lt;/code&gt; if needed.<br />     *<br />     *  </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"> Session<br />     *  </span><span style="color: rgb(128, 128, 128);">@throws</span><span style="color: rgb(0, 128, 0);"> HibernateException<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> Session currentSession() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> HibernateException {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Session) threadLocal.get();<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (session </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (sessionFactory </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />                </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />                    cfg.configure(CONFIG_FILE_LOCATION);<br />                    sessionFactory </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cfg.buildSessionFactory();<br />                }<br />                </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />                    System.err.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%%%% Error Creating SessionFactory %%%%</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />                    e.printStackTrace();<br />                }<br />            }<br />            session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />            threadLocal.set(session);<br />        }<br /><br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> session;<br />    }<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     *  Close the single hibernate session instance.<br />     *<br />     *  </span><span style="color: rgb(128, 128, 128);">@throws</span><span style="color: rgb(0, 128, 0);"> HibernateException<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> closeSession() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> HibernateException {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Session) threadLocal.get();<br />        threadLocal.set(</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">);<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (session </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />            session.close();<br />        }<br />    }<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     * Default constructor.<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> HibernateSessionFactory() {<br />    }<br /><br />}<br /></span></div><br />dos中编译-&gt;<b><br />E:\rmi_test\src&gt;javac -cp   .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\hibernate3.jar    <br />hbmConfig/*.java<br /><br /><br /></b>3&gt;DAO实现<br />接口-&gt;<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> rmic;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Remote;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> IHello </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Remote {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String sayHello(String id) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException;<br />}</span></div><br />实现类-&gt;<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> rmic;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.server.UnicastRemoteObject;<br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取消显示指定的编译器警告！<br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">参考 ： </span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/SuppressWarnings.html</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">@SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">serial</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Hello </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> UnicastRemoteObject </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> IHello {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Hello() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException {<br />        </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">();<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> rebind(String name) { <br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            Naming.rebind(name,</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">);<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Server is running</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String sayHello(String id) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException {<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">run Server<img src="http://www.blogjava.net/images/dot.gif" />.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        <br /><font color="#a52a2a">    org.hibernate.Session session </font></span><font color="#a52a2a"><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> hbmConfig.HibernateSessionFactory.currentSession();<br />    org.hibernate.Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from Liu lt where lt.id=:id</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    hibernate.Liu ll </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (hibernate.Liu)qu.setString(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">id</span><span style="color: rgb(0, 0, 0);">"</span></font><span style="color: rgb(0, 0, 0);"><font color="#a52a2a">,id).uniqueResult();<br />    hbmConfig.HibernateSessionFactory.closeSession();</font><br /><br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Hello </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">ll.getName()</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> This is processed by RMI</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />}</span></div><br />dos编译-&gt;<br /><b>E:\rmi_test\src&gt;javac -cp .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.<br />2\hibernate3.jar  rmic/*.java<br /><br /></b>4&gt;java注册机开启<br /><b>E:\rmi_test\src&gt;rmiregistry 1099<br /><br /></b>5&gt; 本例简单服务器开启<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> start;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RMISecurityManager;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> rmic.Hello ;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Start {<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">System.setSecurityManager(new RMISecurityManager());</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Hello().rebind(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">RMI/Mclaren</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br />}</span></div><br />dos编译-&gt;<b>E:\rmi_test\src&gt;javac  start/*.java<br /></b>运行-&gt;<br /><b>java -classpath .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\jta.jar;<br />D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\asm.jar;<br />D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\commons-collections-2.1.1.jar;<br />D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\commons-logging-1.0.4.jar;<br />D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\hibernate3.jar;<br />D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\dom4j-1.6.1.jar;<br />E:\lib\MYSQL.JAR;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\cglib-2.1.3.jar;<br />E:\and\ant\bin\antlr-2.7.5H3.jar<br />  start.Start</b><br /><br />6&gt;客户端<br /> 得到存根Hello_Stub.class-&gt;<b> E:\rmi_test\src&gt;rmic rmic.Hello<br /></b> 换个地方：c:\\TT 并copy rmic中的IHello.class和Hello_Stub.class连同目录rmic一同copy<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> client;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> rmic.IHello;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Client {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">client<img src="http://www.blogjava.net/images/dot.gif" />.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            IHello hello </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (IHello)Naming.lookup(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">rmi://localhost:1099/RMI/Mclaren</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            System.out.println(hello.sayHello(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br /><br />}</span></div><br /> dos编译-&gt;<b> C:\TT&gt;javac -d . Client.java<br /> C:\TT&gt;java client.Client<br /> client....<br /> Hello <font color="#a52a2a">gg</font> This is processed by RMI (gg就是数据 成功！ )</b><br /><font color="#ffa500">mysql&gt; select * from liu;<br />+----+------+-----+<br />| id | name | avg |<br />+----+------+-----+<br />|  1 | gg   |  24 |<br />+----+------+-----+</font><br /><br /><br /><br /><br /><br /><br /><br /></font></font><img src ="http://www.blogjava.net/Good-Game/aggbug/170028.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-24 13:36 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/24/170028.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起--分离持久层(rmic)</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/18/168547.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Tue, 18 Dec 2007 09:47:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/18/168547.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/168547.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/18/168547.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/168547.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/168547.html</trackback:ping><description><![CDATA[学习了Rmic后 ，联系自己hbm的一些想法。并在实际开发中做到分离持久层.<br />在次，特留记录。使用不当望大家踊跃拍砖！<a id="viewpost1_TitleUrl" class="singleposttitle" href="/Good-Game/archive/2007/12/05/165530.html">EJB原理学习_RMIC</a><br /><br />效果先上 （你的组员，不需要数据库，不需要hbm，不需要配置文件）<br /><b>客户端：<font color="#ffa500">在服务器开启,并client端内classpath 包含有 *_Stub.class 和 hbmBean等类<font color="#a52a2a">(底下有ant简化部署)</font></font></b><font><font><font color="#ffa500"><font color="#000000"><span style="color: rgb(0, 0, 0);"></span></font></font></font></font><b><font color="#ffa500"> 在内网中随便那台电脑都可以中运行,那台开发电脑<font color="#a52a2a">可以没有</font></font></b><font color="#ffa500"><font color="#a52a2a">hbm，可以没有数据库</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> Client;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.ILiuDao;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.LiuDaoRemote;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.imp.DaoFactory;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Client {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            ILiuDao hello </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (ILiuDao)Naming.lookup(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">rmi://192.168.1.109:1099/RMI/LiuDaoRemote</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            System.out.println(hello.getLiu(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">).getName());<br />      } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br />}<br /></span></div><br /><font color="#000000"><b>服务器端：<br />dao需求接口 <font color="#ffa500"> 其他队员提供给管理持久层的队员</font><br /></b><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> dao;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Remote;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> hibernate.Customer;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> hibernate.Liu;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> ILiuDao </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Remote{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> Liu getLiu(String id) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException;<br /></span><span style="color: rgb(0, 0, 0);">}<br /></span></div><b><br />dao实现<br /></b><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> dao;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.server.UnicastRemoteObject;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Query;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Session;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> hbmConfig.HibernateSessionFactory;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> hibernate.Customer;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> hibernate.Liu;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> LiuDaoRemote </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> UnicastRemoteObject </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> ILiuDao {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> LiuDaoRemote() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException {<br />        </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">();<br />    }<br /></span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Liu getLiu(String id)</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException{<br />        Liu ll </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.currentSession();<br />        Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from Liu lt where lt.id=:id</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        ll </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Liu)qu.setString(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">id</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,id).uniqueResult();<br />        HibernateSessionFactory.closeSession();<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> ll ;<br />    }<br />}</span></div><b><br />使用ant给客户端提供 jar</b><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">target </span><span style="color: rgb(255, 0, 0);">name </span><span style="color: rgb(0, 0, 255);">= "rmic"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">rmic </span><span style="color: rgb(255, 0, 0);">base</span><span style="color: rgb(0, 0, 255);">="${class}"</span><span style="color: rgb(255, 0, 0);"> includes</span><span style="color: rgb(0, 0, 255);">="${rmic.package}/**/*Remote.class"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(0, 0, 0);">        &lt;!-- 两个jar 包 ，给客户端提供的<br />             1. rmic带有约定的 到入 *_Stub.class并除dao实现类（*Remote.class</span><font><font><font color="#ffa500"><font color="#000000"><span style="color: rgb(0, 0, 255);"></span></font></font></font></font><span style="color: rgb(0, 0, 0);">）打包<br />                2. hbmBean 道入 hbmjava<br />         --&gt;<br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">jar </span><span style="color: rgb(255, 0, 0);">destfile</span><span style="color: rgb(0, 0, 255);">="${rh.dir}/rmic.jar"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />            </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">fileset </span><span style="color: rgb(255, 0, 0);">dir</span><span style="color: rgb(0, 0, 255);">="${class}"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />              </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">include </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="${rmic.package}/**/*.class"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />              </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">exclude </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="${rmic.package}/**/*Remote.class"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />            </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">fileset</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">jar</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">jar </span><span style="color: rgb(255, 0, 0);">destfile</span><span style="color: rgb(0, 0, 255);">="${rh.dir}/hbmBean.jar"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />            </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">fileset </span><span style="color: rgb(255, 0, 0);">dir</span><span style="color: rgb(0, 0, 255);">="${class}"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />              </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">include </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="${hbm.bean.package}/**/*.class"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />            </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">fileset</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">jar</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">    <br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">target</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span></div><br /><b><br />开启服务器：<br /></b><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> test;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RMISecurityManager;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.registry.LocateRegistry;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sun.awt.AppContext;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.DaoFactoryRemote;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.ILiuDao;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.LiuDaoRemote;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> hibernate.Liu;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> StartMain {<br />    <br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> Exception {<br />           </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> { <br />                System.setProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">java.security.policy</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">rmi.policy</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);  <br />                LocateRegistry.createRegistry(</span><span style="color: rgb(0, 0, 0);">1099</span><span style="color: rgb(0, 0, 0);">);  <br />                Naming.rebind(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">RMI/LiuDaoRemote</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> LiuDaoRemote());  </span><span style="color: rgb(0, 0, 0);"><br />            } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />                e.printStackTrace();<br />            }<br />    }<br /><br />}</span></div><b><br /><br /><br /><br /></b></font></font><img src ="http://www.blogjava.net/Good-Game/aggbug/168547.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-18 17:47 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/18/168547.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EJB小例</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/05/165546.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Wed, 05 Dec 2007 08:01:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/05/165546.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/165546.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/05/165546.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/165546.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/165546.html</trackback:ping><description><![CDATA[
		<p>在此特别感谢：sql技术群主 ☆蓝蓝心情★<br />谢谢在中午远程教我 EJB<br /><strong><font color="#ffa500">原理参考：</font></strong><a id="homepage1_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl01_TitleUrl" href="/Good-Game/archive/2007/12/05/165530.html">EJB原理学习_RMIC</a><br />这可能是我第一个EJB 和大家分享：<br /><font color="#ffa500"><strong>环境：</strong></font>jboss+ejb+jdk5.0<br /></p>
		<br />
		<font color="#ffa500">
				<b>需要jar包</b>
				<br />注：Client中 Test.jar 就是 EJBTest(服务器打的包)<br /></font>
		<img src="http://www.blogjava.net/images/blogjava_net/Good-Game/27888/r_EJB_jar.bmp" />
		<br />
		<p>
				<br />
		</p>
		<p>
				<br />
		</p>
		<p>
				<font color="#ffa500">
						<strong>EJB组件：<br /></strong>接口</font>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<span style="color: rgb(0, 0, 255);">package</span>
				<span style="color: rgb(0, 0, 0);"> org.test;<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> javax.ejb.Remote;<br /><br /><font color="#006400">//在我的EJB原理中介绍<br />//是可以被</font></span>
				<font color="#006400"> rmic  的接口</font>
				<br />
				<span style="color: rgb(0, 0, 0);">@Remote<br /></span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">interface</span>
				<span style="color: rgb(0, 0, 0);"> IHello {<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> String sayHello(String name);<br />}<br /></span>
		</div>
		<p>
				<br />
				<font color="#ffa500">实现类</font>
				<br />
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<span style="color: rgb(0, 0, 255);">package</span>
				<span style="color: rgb(0, 0, 0);"> org.test;<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> javax.ejb.Stateless;<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> org.jboss.annotation.ejb.RemoteBinding;<br /><br />@Stateless<br /><font color="#008000">//设置远程JNDI名字（设置远程JNDI名字，客户端在通过JNDI lookup的时候，用的就是这个名字）  <br />//不同中间件的实现，默认JNDI名字是不一样的，所以不一定是***/Remote或***/Local<br />//RemoteBinding是 jboss特有的<br /></font>@RemoteBinding(jndiBinding</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<b>
						<font color="#ff0000">
								<font color="#a52a2a">
										<span style="color: rgb(0, 0, 0);">MclarenEJB</span>
								</font>
						</font>
				</b>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br /></span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">class</span>
				<span style="color: rgb(0, 0, 0);"> Hello </span>
				<span style="color: rgb(0, 0, 255);">implements</span>
				<span style="color: rgb(0, 0, 0);"> IHello {<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> String sayHello(String name) {<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">Hello </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">name;<br />    }<br />}<br /></span>
		</div>
		<p>
				<br />jdk编辑+打包成jar后热部署到<font color="#a52a2a">%JBOSS_HOME%\server\default\deploy<br /></font><font color="#a52a2a"><br /></font><br /><font color="#ffa500"><strong>客户端：</strong></font></p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> java.util.Properties;<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> javax.naming.Context;<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> javax.naming.InitialContext;<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> org.test.IHello;<br /><br /><br /></span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">class</span>
				<span style="color: rgb(0, 0, 0);"> Client {<br /></span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />        </span>
				<span style="color: rgb(0, 0, 255);">try</span>
				<span style="color: rgb(0, 0, 0);"> {</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">         Properties props </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> Properties();<br />         props.put(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">java.naming.factory.initial</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">, </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">org.jnp.interfaces.NamingContextFactory</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br />         props.put(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">java.naming.provider.url</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">, </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">jnp://localhost:1099</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br />         props.put(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">java.naming.factory.url.pkgs</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">, </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">org.jboss.naming:org.jnp.interfaces</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />         Context context </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> InitialContext(props);<br /><br />         IHello h </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> (IHello) context.lookup(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<b>
						<span style="color: rgb(0, 0, 0);">MclarenEJB</span>
				</b>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br />         System.out.println(h.sayHello(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">EJB 3.0 Test</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">));<br />        } </span>
				<span style="color: rgb(0, 0, 255);">catch</span>
				<span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br /><br />}<br /></span>
		</div>
		<p>
				<font color="#a52a2a">
						<strong>结果是：<br />Hello EJB 3.0 Test<br /><br /><br /></strong>
				</font>
				<br />
				<br />
		</p>
<img src ="http://www.blogjava.net/Good-Game/aggbug/165546.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-05 16:01 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/05/165546.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EJB原理学习_RMIC</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/05/165530.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Wed, 05 Dec 2007 07:39:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/05/165530.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/165530.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/05/165530.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/165530.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/165530.html</trackback:ping><description><![CDATA[EJB原理，有幸在 Q群上‘☆蓝蓝心情★’ 得以一教；<br />特在次郑重的道一声 <font color="#a52a2a"><b>谢谢了.大哥<br /><br /></b><font color="#000000">在次我就分享给大家，本人语言组织能力有限，</font></font><font color="#a52a2a"><font color="#000000">说的不好可别怪我大哥啊。（呵呵）<br /><br />EJB技术对</font></font><font color="#a52a2a"><font color="#000000">传输上进行封装，使程序员不需要关心太多网络问题</font></font><font color="#a52a2a"><font color="#000000">。服务器都基于统一javabean操作的 <br />在这就是基于 rmic 命令，和 rmiregistry 端口 来实现的。<br />rmic 和 rmiregistry 这些都在%JAVA_HOME%/bin下可以找到。<br />下面我就用大哥给写的一段 <font color="#0000ff"><b>没用</b></font>EJB容器 的代码来和大家分享EJB原理：<br /><font color="#ffa500"><b><br /></b><br />1.rmic命令介绍：<br /><font color="#0000ff">引用：  http://www.iplab.cs.tsukuba.ac.jp/liuxj/jdk1.2/zh/docs/tooldocs/solaris/rmic.html</font><br /></font></font></font><b>rmic</b> 编译器根据编译后的 Java 类（含有远程对象实现）名，为远程对象生成 stub 和 skeleton（远程对象是指实现 <tt>java.rmi.Remote</tt> 接口的对象）。<br />在 <b>rmic</b> 命令中所给的类必须是经 <b>javac</b> 命令成功编译且是完全包限定的类。例如，按如下所示对类文件名 <tt>HelloImpl</tt> 运行 <b>rmic</b>：<br /><font color="#a52a2a"><font color="#000000"><font color="#ffa500"><br /></font></font></font><font color="#a52a2a"><font color="#000000"><br /><font color="#ffa500"><b>2.EJB组建（<font color="#a52a2a">服务器端</font>）</b></font></font></font><br /><font color="#a52a2a"><font color="#000000"><font color="#ffa500">接口：</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> org.rmi.test;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Remote;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> IHello </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Remote {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String sayHello(String name) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException;<br />}<br /></span></div><br /><font color="#ffa500">实现类：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> org.rmi.test;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RemoteException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.server.UnicastRemoteObject;<br /><font color="#006400">//取消显示指定的编译器警告！<br />//参考 ： <a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/SuppressWarnings.html">http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/SuppressWarnings.html</a></font><br />@SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">serial</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) <br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Hello </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> UnicastRemoteObject </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> IHello {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Hello() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException {<br />        </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">();<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> rebind(String name) { <br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            Naming.rebind(name,</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">);<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Server is running<img src="http://www.blogjava.net/images/dot.gif" /></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String sayHello(String name) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> RemoteException {</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Hello </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> This is processed by RMI</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />}<br /></span></div><br /></font>上面的继承 </font></font><tt>java.rmi.Remote 和可序列化的感觉是一样的；在网络中转成流格式后，便与传输。<br /><font color="#ffa500"><br />3. jdk1.5编译和 </font></tt><font color="#a52a2a"><font color="#000000"><font color="#ffa500">rmic </font></font></font><tt><font color="#ffa500">编译</font></tt><tt><font color="#ffa500">后</font><br /> 1)javac .....<br /> 2)rmic  rmic org.rmi.test.Hello(后得到 Hello_Stub.class)<br /><br /><font color="#ffa500">4.服务器开启：<br />在项目根目录下加 rmi.policy<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">grant {<br />  Permission java.security.AllPermission </span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">connect,listen,accept</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 128, 0);">;<br /></span><span style="color: rgb(0, 0, 0);">}</span><span style="color: rgb(0, 128, 0);">;</span></div><br />后运行：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> org.rmi.test;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.RMISecurityManager;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Start {<br /></span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) {</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            System.setSecurityManager(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> RMISecurityManager());<br /> <font color="#006400">           //这里还不太明白</font><br />            </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Hello().rebind(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">RMI/Mclaren</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); <br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br /><br />}<br /></span></div></font></tt><tt><font color="#ffa500"><b><br /></b>5.开端口：</font><br />rmiregistry 1099  （开 端口）</tt><br /><tt><font color="#ffa500"><br /><b>客户端：（另台电脑也可以了）</b><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.rmi.Naming;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.rmi.test.IHello;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Client {</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) {</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            IHello hello </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (IHello)Naming.lookup(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">rmi://localhost:1099/RMI/Mclaren</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            System.out.println(hello.sayHello(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Mclaren</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br /><br />}<br /></span></div><br /></font><br /></tt><font color="#a52a2a"><b>结果是：<br />Hello Mclaren This is processed by RMI<br /><br /></b></font><img src ="http://www.blogjava.net/Good-Game/aggbug/165530.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-05 15:39 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/05/165530.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>