SessionBean的学习:
用于测试EJB的client代码:
package cn.choelea.ejb;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloClient {
public HelloClient() {}
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
Context ctx=new InitialContext(properties );
Object obj = ctx.lookup("HelloWorldEJB");
HelloWorldHome home = (HelloWorldHome) javax.rmi.PortableRemoteObject.narrow(obj,
HelloWorldHome.class);
HelloWorld hello = home.create();
System.out.println(hello.helloWorld());
}
}
客户端可以是个简单的java application. classpath 中要有服务器端打包的EJB的jar文件(或者只导入两个接口类即可)和weblogic.jar
Weblogic Server端EJB
HelloWorld 远程接口:
package cn.choelea.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface HelloWorld extends EJBObject{
public String helloWorld() throws RemoteException;
}
Home 接口:
package cn.choelea.ejb;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface HelloWorldHome extends EJBHome{
public HelloWorld create() throws CreateException,RemoteException;
}
EJB 类:
package cn.choelea.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EJBHome;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloWorldBean implements SessionBean,HelloWorld{
private SessionContext ctx;
public EJBHome getEJBHome() throws RemoteException {
// TODO Auto-generated method stub
return null;
}
public void ejbCreate(){}
public Handle getHandle() throws RemoteException {
// TODO Auto-generated method stub
return null;
}
public Object getPrimaryKey() throws RemoteException {
// TODO Auto-generated method stub
return null;
}
public boolean isIdentical(EJBObject arg0) throws RemoteException {
// TODO Auto-generated method stub
return false;
}
public void remove() throws RemoteException, RemoveException {
// TODO Auto-generated method stub
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
ctx = arg0;
}
public String helloWorld() throws RemoteException {
return "Hello World. Welcome to EJB!";
}
}
EJB部署文件:(ejb-jar.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar id="ejb-jar_ID"> <enterprise-beans> <session> <ejb-name>HelloWorld</ejb-name> <home>cn.choelea.ejb.HelloWorldHome</home> <remote>cn.choelea.ejb.HelloWorld</remote> <ejb-class>cn.choelea.ejb.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar>
weblogic-ejb-jar.xml 描述文件
<?xml version="1.0"?> <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd"> <!-- Sample MessageDriven bean Weblogic deployment descriptor --> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>HelloWorld</ejb-name> <jndi-name>HelloWorldEJB</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar>
1321

被折叠的 条评论
为什么被折叠?



