ejb的小例子(未能成功运行)

创建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:

<?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>

jboss.xml:

<?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>

导出这个ejb项目成EjbTest.jar. 并放入目录内:D:\jboss-6.1.0.Final\server\default\deploy


新建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();
		}
	}

}

在这个java Project内的Build Path要加入生成的EjbTest.jar,并且将D:\jboss-6.1.0.Final\client 下所有的jar添加进来。

此时运行,出现错误:

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

暂时不知道如何解决。


你可能感兴趣的:(ejb的小例子(未能成功运行))