创建EJB Project.
创建接口:
StatelessDao.java :
package com.cjq.ejb;import java.rmi.RemoteException;import javax.ejb.EJBObject;public interface StatelessDao extends EJBObject { public void say() throws RemoteException;}StatelessHome.java:
package com.cjq.ejb;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBHome;public interface StatelessHome extends EJBHome { public StatelessDao create() throws CreateException,RemoteException;}创建Bean类:
StatelessBean.java:
package com.cjq.ejb;import java.rmi.RemoteException;import javax.ejb.EJBException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;public class StatelessBean implements SessionBean { @Override public void ejbActivate() throws EJBException, RemoteException { // TODO Auto-generated method stub } @Override public void ejbPassivate() throws EJBException, RemoteException { // TODO Auto-generated method stub } @Override public void ejbRemove() throws EJBException, RemoteException { // TODO Auto-generated method stub } @Override public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException { // TODO Auto-generated method stub } public void say(){ System.out.println("test"); }}META-INF目录下创建xml文件:
ejb-jar.xml:
jboss.xml:EjbTest ejbtest1 com.cjq.ejb.StatelessHome com.cjq.ejb.StatelessDao com.cjq.ejb.StatelessBean Stateless Bean
导出这个ejb项目成EjbTest.jar. 并放入目录内:D:\jboss-6.1.0.Final\server\default\deployejbtest1 ejbtest1
新建java Project:
创建java类:
package com.cjq.client;import java.rmi.RemoteException;import java.util.Hashtable;import javax.ejb.CreateException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;import com.cjq.ejb.StatelessDao;import com.cjq.ejb.StatelessHome;public class EjbTestClient { public static void main(String[] args) { // TODO Auto-generated method stub String url="127.0.0.1:1099"; Hashtable在这个java Project内的Build Path要加入生成的EjbTest.jar,并且将D:\jboss-6.1.0.Final\client 下所有的jar添加进来。h = new Hashtable (); h.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); h.put(Context.PROVIDER_URL, url); try{// NamingContext InitialContext ctx = new InitialContext(h); Object obj = ctx.lookup("ejbtest1"); StatelessHome home = (StatelessHome)PortableRemoteObject.narrow(obj, StatelessHome.class); StatelessDao dao ; try{ dao = (StatelessDao)home.create(); dao.say(); }catch(RemoteException e){ e.printStackTrace(); }catch(CreateException e){ e.printStackTrace(); } }catch(NamingException ex){ ex.printStackTrace(); } }}
此时运行,出现错误:
log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Exception in thread "main" java.lang.ClassCastException at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(Unknown Source) at javax.rmi.PortableRemoteObject.narrow(Unknown Source) at com.cjq.client.EjbTestClient.main(EjbTestClient.java:27)Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object ... 3 more暂时不知道如何解决。