ejb의 작은 예 (실행에 실패했습니다)

4765 단어
EJB 프로젝트를 만듭니다.
인터페이스 만들기:
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>

이 jb 프로젝트를 EjbTest로 내보냅니다.jar. 디렉터리에 넣습니다: D:\jboss-6.1.0.Final\server\default\deploy
새 자바 프로젝트:
자바 클래스 만들기:
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();
		}
	}

}

이 자바 프로젝트의 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

당분간은 어떻게 해결해야 할지 모르겠다.

좋은 웹페이지 즐겨찾기