Posted on 2007-12-18 17:47
G_G 阅读(1092)
评论(0) 编辑 收藏 所属分类:
EJB
学习了Rmic后 ,联系自己hbm的一些想法。并在实际开发中做到分离持久层.
在次,特留记录。使用不当望大家踊跃拍砖!
EJB原理学习_RMIC效果先上 (你的组员,不需要数据库,不需要hbm,不需要配置文件)
客户端:在服务器开启,并client端内classpath 包含有 *_Stub.class 和 hbmBean等类(底下有ant简化部署) 在内网中随便那台电脑都可以中运行,那台开发电脑可以没有hbm,可以没有数据库
package Client;
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();
}
}
}
服务器端:
dao需求接口 其他队员提供给管理持久层的队员
package dao;
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实现
package 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
<target name = "rmic">
<rmic base="${class}" includes="${rmic.package}/**/*Remote.class"/>
<!-- 两个jar 包 ,给客户端提供的
1. rmic带有约定的 到入 *_Stub.class并除dao实现类(*Remote.class)打包
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>
开启服务器:
package test;
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();
}
}
}