문제

I am trying to build a simpmle Hello world type EJB 2.1 application. The intended runtime for this application is supposed to be Jboss 5.1.0. Here is the code i have written.

EJB Configuration file:

ejb/TestEJBInterfaceBean com.TestEJB.TestEJBInterfaceHome com.TestEJB.TestEJBInterfaceRemote com.TestEJB.TestEJBInterfaceBean Stateless Container

Home interface:

import javax.ejb.EJBHome;

public interface TestEJBInterfaceHome extends EJBHome {
    public TestEJBInterfaceRemote create() throws java.rmi.RemoteException,
            javax.ejb.CreateException;
}

Remote Interface:

import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface TestEJBInterfaceRemote extends EJBObject { 

    public String ping(String version) throws RemoteException;
}

Bean class:

import java.rmi.RemoteException;
import java.util.Date;

import org.jboss.logging.Logger;

public class TestEJBIInterfaceBean extends BaseSessionBean implements TestEJBIInterfaceRemote{

    private static final long serialVersionUID = 1L;

    final Logger log = Logger.getLogger(TestEJBIInterfaceBean.class);   


    public String ping(String arg0) throws RemoteException {

        String response = this.getClass().getSimpleName() + " pinged @ " + new Date().getTime();

        log.info(response);
        return response;
    }

    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 {
        // TODO Auto-generated method stub

    }

    public EJBHome getEJBHome() throws RemoteException {
        // TODO Auto-generated method stub
        return null;
    }

    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

    }

}

The Test Client:

public class TestEJBInterfaceClientTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Context ctx = new InitialContext();

            TestEJBInterfaceRemote obj = (TestEJBInterfaceRemote)ctx.lookup("ejb/TestEJBInterfaceBean");

            System.out.println(obj.ping("12345"));

        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

When i run the client i get the following error:

Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.TestEJB.TestEJBInterfaceRemote
    at TestEJBInterfaceClientTest.main(TestEJBInterfaceClientTest.java:21)

The error seems to suggest that the cast is wrong but i suspect that the casting has nothing to do with the error. (i tried to cast to TestEJBInterfaceHome but i get the same error). My suspisions are actually in the version of the application.

Questions

  • Is there any possibility that Jboss is treating this as an EJB3 application? Looking at the configuration file i am not specifying that this is an EJB2.1 so maybe that is causing a problem?
  • Is there any way to find out what Type is returned from the ctx.lookup call? i tried getClass().getName, getClass().getCanonicalName() and all i get back is names like $proxy0, $proxy20 etc.
  • Have i missed something obvious?
도움이 되었습니까?

해결책

The result of a pre-EJB-3.0 lookup is the home interface, so try casting to TestEJBInterfaceHome instead. Note that for portability, you need to use PortableRemoteObject.narrow on the return value of ctx.lookup before casting to the target interface.

The actual type is a proxy class, so getClass().getName() is returning the correct thing. To determine what interfaces are implemented by the class, use getClass().getInterfaces().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top