锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
闃呰闇瑕侊細
聽java,javac,rmiregistry,rmic,ant 絳夊懡浠ゆ湁鎵娑夊強錛堝彧鏄畝鍗曚嬌鐢紝涓嶄細涔熷埆鎬曪紒錛?/font>
鍙鎬т嬌鐢ㄨ鏄庯細
聽 鎸佷箙灞傜▼搴忓憳鎴愬姛寮鍚痳miregistry鏈嶅姟錛屽湪灞鍩熺綉鍐呭叾浠栦嬌鐢ㄦ暟鎹殑闃熷弸灝卞彧瑕佸姞杞?br />hbmBean 鐨勬槧灝刯ar鍜?dao鎺ュ彛jar鍚?灝卞彲浠ュ彇寰楁暟鎹傛牴鏈劅瑙変笉鍒癶bn浣跨敤銆?br />浼樼偣錛?br />1.鍏朵粬闃熷憳緙栬瘧classpath涓笉闇瑕乭bm鐨勪換浣曚笢瑗匡紝璺熷叧娉ㄨ嚜宸變笢瑗褲?br />2.寮哄埗鎺ュ彛瑙勮寖銆?br />3.姝d氦緙栫▼銆?br />4.DAO嫻嬭瘯鎬ц兘鏂逛究銆?br />5.榪欒繕鏄?EJB 鍘熺悊錛岃漿鎹㈠鏄撱?br />
緙虹偣錛?br />1.rmic 紿佺牬鏀劇伀澧欒兘鍔涙湁闄愶紝鍙互鎹㈡垚EJB
2.澶у瀷欏圭洰娉ㄥ唽鏈嶅姟鍣紝閰嶇疆綣佺悙錛屽彲浠ヤ嬌鐢‥JB3.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聽 閰嶇疆鏄犲皠鍙傝?錛?/font>ant 欏圭洰瀹為檯 騫朵嬌鐢?hibernate3.xml
聽聽聽 dos 杈撳叆 -> ant -f hibernate3.xml ddl2hbm (浣跨敤浜哅iddlegen-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>鎵嬪姩寤篽bmHibernateSessionFactory鍦?/font>hbmConfig鏂囦歡涓?font color="#ffa500">(涓轟簡綆鍗昪opy->myeclipes鐢熸垚鐨?
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瀹炵幇
鎺ュ彛->
import聽java.rmi.Remote;
import聽java.rmi.RemoteException;
public聽interface聽IHello聽extends聽Remote聽{
聽聽聽聽public聽String聽sayHello(String聽id)聽throws聽RemoteException;
}
瀹炵幇綾?>
import聽java.rmi.Naming;
import聽java.rmi.RemoteException;
import聽java.rmi.server.UnicastRemoteObject;
//鍙栨秷鏄劇ず鎸囧畾鐨勭紪璇戝櫒璀﹀憡錛?br />//鍙傝兟狅細聽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娉ㄥ唽鏈哄紑鍚?br />E:\rmi_test\src>rmiregistry 1099
5> 鏈緥綆鍗曟湇鍔″櫒寮鍚?br />
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>瀹㈡埛绔?br />聽寰楀埌瀛樻牴Hello_Stub.class-> E:\rmi_test\src>rmic rmic.Hello
聽鎹釜鍦版柟錛歝:\\TT 騫禼opy rmic涓殑IHello.class鍜孒ello_Stub.class榪炲悓鐩綍rmic涓鍚宑opy
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 |
+----+------+-----+
]]>
鏁堟灉鍏堜笂 錛堜綘鐨勭粍鍛橈紝涓嶉渶瑕佹暟鎹簱錛屼笉闇瑕乭bm錛屼笉闇瑕侀厤緗枃浠訛級
瀹㈡埛绔細鍦ㄦ湇鍔″櫒寮鍚?騫禼lient绔唴classpath 鍖呭惈鏈?*_Stub.class 鍜?hbmBean絳夌被(搴曚笅鏈塧nt綆鍖栭儴緗? 鍦ㄥ唴緗戜腑闅忎究閭e彴鐢佃剳閮藉彲浠ヤ腑榪愯,閭e彴寮鍙戠數鑴?font color="#a52a2a">鍙互娌℃湁hbm錛屽彲浠ユ病鏈夋暟鎹簱
import聽java.rmi.Naming;
import聽dao.ILiuDao;
import聽dao.LiuDaoRemote;
import聽dao.imp.DaoFactory;
public聽class聽Client聽{
聽聽聽聽public聽static聽void聽main(String[]聽args)聽{
聽聽聽聽聽聽聽聽try聽{
聽聽聽聽聽聽聽聽聽聽聽聽ILiuDao聽hello聽=聽(ILiuDao)Naming.lookup("rmi://192.168.1.109:1099/RMI/LiuDaoRemote");
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(hello.getLiu("1").getName());
聽聽聽聽聽聽}聽catch(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();
聽聽聽聽聽聽聽聽}
聽聽聽聽}
}
鏈嶅姟鍣ㄧ錛?br />dao闇姹傛帴鍙B?font color="#ffa500"> 鍏朵粬闃熷憳鎻愪緵緇欑鐞嗘寔涔呭眰鐨勯槦鍛?/font>
import聽java.rmi.Remote;
import聽java.rmi.RemoteException;
import聽hibernate.Customer;
import聽hibernate.Liu;
public聽interface聽ILiuDao聽extends聽Remote{
聽聽聽聽public聽abstract聽Liu聽getLiu(String聽id)聽throws聽RemoteException;
}
dao瀹炵幇
import聽java.rmi.Naming;
import聽java.rmi.RemoteException;
import聽java.rmi.server.UnicastRemoteObject;
import聽org.hibernate.Query;
import聽org.hibernate.Session;
import聽hbmConfig.HibernateSessionFactory;
import聽hibernate.Customer;
import聽hibernate.Liu;
public聽class聽LiuDaoRemote聽extends聽UnicastRemoteObject聽implements聽ILiuDao聽{
聽聽聽聽public聽LiuDaoRemote()聽throws聽RemoteException聽{
聽聽聽聽聽聽聽聽super();
聽聽聽聽}
聽聽聽聽public聽Liu聽getLiu(String聽id)throws聽RemoteException{
聽聽聽聽聽聽聽聽Liu聽ll聽=聽聽null聽;
聽聽聽聽聽聽聽聽Session聽session聽=聽HibernateSessionFactory.currentSession();
聽聽聽聽聽聽聽聽Query聽qu聽=聽session.createQuery("from聽Liu聽lt聽where聽lt.id=:id");
聽聽聽聽聽聽聽聽ll聽=聽(Liu)qu.setString("id",id).uniqueResult();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽return聽ll聽;
聽聽聽聽}
}
浣跨敤ant緇欏鎴風鎻愪緵 jar
聽聽聽聽聽聽聽聽<rmic聽base="${class}"聽includes="${rmic.package}/**/*Remote.class"/>
聽聽聽 聽聽聽 <!-- 涓や釜jar 鍖?錛岀粰瀹㈡埛绔彁渚涚殑
聽聽聽聽 聽聽聽 聽聽聽 1. rmic甯︽湁綰﹀畾鐨?鍒板叆 *_Stub.class騫墮櫎dao瀹炵幇綾伙紙*Remote.class錛夋墦鍖?br />聽聽聽 聽聽 聽聽 聽聽 2. hbmBean 閬撳叆 hbmjava
聽 聽 聽 聽聽 -->
聽聽聽聽聽聽聽聽<jar聽destfile="${rh.dir}/rmic.jar">
聽聽聽聽聽聽聽聽聽聽聽聽<fileset聽dir="${class}">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽<include聽name="${rmic.package}/**/*.class"聽/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽<exclude聽name="${rmic.package}/**/*Remote.class"/>
聽聽聽聽聽聽聽聽聽聽聽聽</fileset>
聽聽聽聽聽聽聽聽</jar>
聽聽聽聽聽聽聽聽<jar聽destfile="${rh.dir}/hbmBean.jar"聽>
聽聽聽聽聽聽聽聽聽聽聽聽<fileset聽dir="${class}">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽<include聽name="${hbm.bean.package}/**/*.class"聽/>
聽聽聽聽聽聽聽聽聽聽聽聽</fileset>
聽聽聽聽聽聽聽聽</jar>聽聽聽聽
聽聽聽聽</target>
寮鍚湇鍔″櫒錛?br />
import聽java.rmi.Naming;
import聽java.rmi.RMISecurityManager;
import聽java.rmi.RemoteException;
import聽java.rmi.registry.LocateRegistry;
import聽sun.awt.AppContext;
import聽dao.DaoFactoryRemote;
import聽dao.ILiuDao;
import聽dao.LiuDaoRemote;
import聽hibernate.Liu;
public聽class聽StartMain聽{
聽聽聽聽
聽聽聽聽public聽static聽void聽main(String[]聽args)聽throws聽Exception聽{
聽聽聽聽聽聽聽聽聽聽聽try聽{聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.setProperty("java.security.policy",聽"rmi.policy");聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽LocateRegistry.createRegistry(1099);聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Naming.rebind("RMI/LiuDaoRemote",聽new聽LiuDaoRemote());聽
聽聽聽聽聽聽聽聽聽聽聽聽}聽catch(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽}
}
]]>
璋㈣阿鍦ㄤ腑鍗堣繙紼嬫暀鎴?EJB
鍘熺悊鍙傝冿細EJB鍘熺悊瀛︿範_RMIC
榪欏彲鑳芥槸鎴戠涓涓狤JB 鍜屽ぇ瀹跺垎浜細
鐜錛?/strong>jboss+ejb+jdk5.0
EJB緇勪歡錛?br />鎺ュ彛
瀹炵幇綾?/font>
jdk緙栬緫+鎵撳寘鎴恓ar鍚庣儹閮ㄧ講鍒?font color="#a52a2a">%JBOSS_HOME%\server\default\deploy
瀹㈡埛绔細
緇撴灉鏄細
Hello EJB 3.0 Test