public interface LocalUserHome extends EJBLocalHome
{
public LocalUser create(String email,String password)
throws CreateException;
public LocalUser findByPrimaryKey(String email)
throws FinderException;
}
2.local接口提供业务方法
详细清单如下:
package test;
import javax.ejb.EJBLocalObject;
public interface LocalUser extends EJBLocalObject{
public String getEmail();
public String getPassword();
}
应为置email和password为只读,所以没有提供setter方法,不过在具体Bean里需要实现setter方法
3.实体Bean类
实现业务方法及回调方法,
详细清单如下:
package test;
import javax.ejb.EntityBean;
import javax.ejb.CreateException;
import javax.ejb.EntityContext;
public abstract class UserBean implements EntityBean{
public String ejbCreate(String email,String password)
throws CreateException
{
this.setEmail(email);
this.setPassword(password);
return null;
}
//abstract method
public abstract void setEmail(String email);
public abstract String getEmail();
public abstract void setPassword(String password);
public abstract String getPassword();
public void ejbPostCreate(String email,String password){}
public void ejbActivate(){}
public void ejbPasstivate(){}
public void ejbLoad(){}
public void ejbStore(){}
public void setEntityContext(EntityContext ctx){}
public void unsetEntityContext(){}
public void ejbRemove(){}
}
4.实体Bean的ejb-jar.xml配置文件清单:
四: 开发sessionbean
1.sessionbean 的home接口,因为需要和客户交互,所以sessionbean必须提供远程接口
清单如下:
package test;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface UserManagementHome extends EJBHome
{
public UserManagement create() throws CreateException,RemoteException;
}
注意该接口需要抛出RemoteException而本地接口则不需要
2.sessionbean远程接口,该接口提供session需要实现的逻辑方法
清单如下:
package test;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface UserManagement extends EJBObject
{
public void addUser(String email,String password) throws RemoteException;
public void removeUser(String email) throws RemoteException;
public boolean verifyPassword(String email,String password) throws RemoteException;
}
3 sessionbean的实现类
详细清单如下:
package test;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import test.LocalUser;
import test.LocalUserHome;
public class UserManagementBean implements SessionBean{
private LocalUserHome userHome=null;
//助手方法用来查找实体bean的home接口
private LocalUserHome getUserHome()
throws NamingException
{
Context initial=new InitialContext();
return (LocalUserHome)initial.lookup("java:comp/env/ejb/LocalUser");
}
public void addUser(String email,String password)
{
try{
LocalUser user=userHome.create(email,password);
}catch(CreateException e)
{
System.out.print(e.toString());
}
}
public void removeUser(String email)
{
try{
userHome.remove(email);
}catch(RemoveException e)
{
System.out.println(e.toString());
}
}
public boolean verifyPassword(String email,String password)
{
boolean verify=false;
try{
LocalUser user=userHome.findByPrimaryKey(email);
verify=user.getPassword().equals(password);
}catch(FinderException e)
{
System.out.println(e.toString());
}
return verify;
}
public void ejbCreate() throws CreateException
{
try{
userHome=this.getUserHome();
}catch(NamingException e)
{
System.out.println(e.toString());
}
}
public void ejbActivate(){}
{
try {
userHome = getUserHome();
} catch (NamingException e) {
System.out.print("Unable to find local user home in Activate");
}
}
public void ejbRemove(){}
public void ejbPassivate(){
userHome=null;
}
public void setSessionContext(SessionContext ctx){}
}
4:sessionbean的配置描述文件:
五:客户端Client
package test;
import test.UserManagementHome;
import test.UserManagement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class Client{
public static void main(String[] args)
{
try{
Context ctx=new InitialContext();
Object ref=ctx.lookup("UserManagementHome");
UserManagementHome home=(UserManagementHome)PortableRemoteObject.narrow(ref,UserManagementHome.class);
UserManagement user=home.create();
user.addUser("[email protected]","lms");
user.addUser("[email protected]","sohu");
user.addUser("[email protected]","163");
user.removeUser("[email protected]");
boolean login=user.verifyPassword("[email protected]","sohu");
System.out.println(login);
}catch(Exception e)
{
System.out.print(e.toString());
}
}
}
六:jboss的配置文件jboss.xml
清单如下:
该清单提供了实体bean和sessionbean的jndi设置,一个sessionbean对实体bean的引用描述
七打包:
可以用ant工具打包,也可以用jar打包
目录结构
--
--test
--META-INF
假设类文件放在f:/java目录下,
执行命令:
c:/
cd f:/java
jar cvf user.jar *
完成
八,部署
jboss支持热部署 ,把jar包放到deploy目录下即可实现自动部署,数据库也会自动创建
,也可以手工创建数据库,表明和实体bean名称相对应
九运行:
jboss不需要一个client jar文件,只要把$jboss_home/client下的jar加到classpath下
就可运行,另外jboss需要一个jndi.properties文件,也需要放到classpath下
如果通过jcreator pro 运行client实例,把jndi.properties文件放到java文件同目录下即可
如果有找不到类的异常抛出,可以在jcreator中手工手工加入相关包,jbossall-client.jar
jnp_client.jar 这两个文件在$jboss_home/client目录下
好了,到此结束大家运行一下试试看
let't go!!!
相关附件提供了所有类及配置文件,以及数据源配置文件
参考资料:http://www-900.ibm.com/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle/991A4E2DCA2D6CE148256BCE00135FAE?OpenDocument