EJB客户端的上下文查找返回$ Proxy119对象而不是实际的类对象

时间:2015-04-14 06:55:22

标签: jboss ejb-3.0

我只是在JBoss中调用这样的EJB 3.0:

MyEJB myEJB = (MyEJB) initialContext.lookup("");

现在查找工作正常。返回的对象具有预期的viewType,moduleName,beanName。但它抛出了这个例外:

2015-04-10 18:49:08,266 ERROR [org.teiid.CONNECTOR] (Worker0_QueryProcessorQueue1128) Connector worker process failed for atomic-request=GfxN/Obks1QK.1.1.0: java.lang.ClassCastException: com.sun.proxy.$Proxy119 cannot be cast to com.MyEJB

如何将其强制转换为我打算使用的对象?

2 个答案:

答案 0 :(得分:1)

这是代码的示例示例:

HelloLocalHome helloHome;
HelloLocal hello;

//In your main or init method, 

// 1. Retreive the Home Interface using a JNDI Lookup
// Retrieve the initial context for JNDI.       
// No properties needed when local
Context context = new InitialContext();

// Retrieve the home interface using a JNDI lookup using
// the java:comp/env bean environment variable        
// specified in web.xml
helloHome = (HelloLocalHome) context.lookup("java:comp/env/ejb/HelloBean");

//2. Narrow the returned object to be an HelloHome object.      
// Since the client is local, cast it to the correct object type.
//3. Create the local Hello bean instance, return the reference 
hello = (HelloLocal)helloHome.create();

//And the call will be as follows :
hello.sayHello("James Earl")

答案 1 :(得分:0)

事实证明这样做的正确方法是:

    StatelessEJBLocator<MyEjb> locator = new StatelessEJBLocator(
            MyEjb.class, APP_NAME, MODULE_NAME,
            MyEjbRemote.class.getSimpleName(), DISTINCT_NAME);

    MyEjb myEjb = org.jboss.ejb.client.EJBClient.createProxy(locator);