创建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; }
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; }
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"); } }
ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"> <display-name>EjbTest </display-name> <enterprise-beans> <session> <ejb-name>ejbtest1</ejb-name> <home>com.cjq.ejb.StatelessHome</home> <remote>com.cjq.ejb.StatelessDao</remote> <ejb-class>com.cjq.ejb.StatelessBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar>
<?xml version="1.0" encoding="UTF-8"?> <jboss> <enterprise-beans> <session> <ejb-name>ejbtest1</ejb-name> <jndi-name>ejbtest1</jndi-name> </session> </enterprise-beans> </jboss>
新建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<String,String> h = new Hashtable<String, String>(); 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