我正在尝试构建一个simpmle Hello world类型的EJB 2.1应用程序。此应用程序的预期运行时应该是Jboss 5.1.0。 这是我写的代码。
EJB配置文件:
EJB / TestEJBInterfaceBean
com.TestEJB.TestEJBInterfaceHome
com.TestEJB.TestEJBInterfaceRemote
com.TestEJB.TestEJBInterfaceBean
无状态
集装箱
家庭界面:
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类:
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
}
}
测试客户端:
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();
}
}
}
当我运行客户端时,我收到以下错误:
Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.TestEJB.TestEJBInterfaceRemote
at TestEJBInterfaceClientTest.main(TestEJBInterfaceClientTest.java:21)
错误似乎表明演员表错了,但我怀疑演员表与错误无关。 (我试图转换为TestEJBInterfaceHome,但我得到了同样的错误)。我的怀疑实际上是在应用程序的版本中。
问题
ctx.lookup
来电中返回的类型?我尝试了getClass().getName
,getClass().getCanonicalName()
,我回来的就是$proxy0, $proxy20
等名字。答案 0 :(得分:2)
EJB-3.0之前的查找结果是home接口,因此请尝试转换为TestEJBInterfaceHome
。请注意,为了便于移植,您需要在转换到目标接口之前对ctx.lookup的返回值使用PortableRemoteObject.narrow。
实际类型是代理类,因此getClass().getName()
返回正确的内容。要确定类实现了哪些接口,请使用getClass().getInterfaces()
。