远程调用同一服务器上存在的 EJB 时出现 ClassCastException

发布于 2024-09-05 15:39:27 字数 1062 浏览 9 评论 0原文

我有2个ejb。调用 Ejb-B 的 Ejb-A。他们不在同一个耳朵里。

为了可移植性,Ejb-B 可能存在于同一服务器上,也可能不存在。 (有一个外部属性文件,其中包含 Ejb-B 的提供程序 URL。我对此无法控制。)

示例代码:在 Ejb-A

EjbBDelegate delegateB = EjbBDelegateHelper.getRemoteDelegate(); // lookup from list of URLs from props... 
BookOfMagic bom = delegateB.getSomethingInteresting();

用例/结果中:

  1. 当 Ejb-B 不存在与 Ejb-A 位于同一服务器上,一切正常。 (它通过 URL 进行循环)
  2. 当 Ejb-B 确实存在 在同一服务器上,并且 Ejb-A 碰巧在同一服务器上调用 Ejb-B 时,一切正常。
  3. 当 Ejb-B 确实存在 在同一台服务器上,并且 Ejb-A 在不同的服务器上调用 Ejb-B 时,我得到:

javax.ejb.EJBException:嵌套异常为:java.lang.ClassCastException:$Proxy126 java.lang.ClassCastException:$Proxy126

我正在使用 Weblogic 10.0、Java 5、EJB3

基本上,如果 Ejb-B 存在于服务器上,则必须仅在该服务器上调用它。

这让我相信该类是由本地类加载器加载的(在部署时?),然后当远程调用时,另一个类加载器正在加载它。 (导致异常)但它应该可以工作,因为它应该被序列化到目标类加载器中......

我做错了什么?

此外,在本地复制时,Ejb-A 会优先选择同一服务器上的 Ejb-B,因此很难复制。但在其他机器上却并非如此。

注意:这对于 EJB2 来说一切正常

I have 2 ejbs. Ejb-A that calls Ejb-B. They are not in the same Ear.

For portability Ejb-B may or may not exist on the same server. (There is an external property file that has the provider URLs of Ejb-B. I have no control over this.)

Example Code: in Ejb-A

EjbBDelegate delegateB = EjbBDelegateHelper.getRemoteDelegate(); // lookup from list of URLs from props... 
BookOfMagic bom = delegateB.getSomethingInteresting();

Use Cases/Outcomes:

  1. When Ejb-B DOES NOT EXIST on the same server as Ejb-A, everything works correctly. (it round-robbins through the URLs)
  2. When Ejb-B DOES EXIST on the same server, and Ejb-A happens to call Ejb-B on the same server, everything works correctly.
  3. When Ejb-B DOES EXIST on the same server, and Ejb-A calls Ejb-B on a different server, I get:

javax.ejb.EJBException: nested exception is: java.lang.ClassCastException: $Proxy126
java.lang.ClassCastException: $Proxy126

I'm using Weblogic 10.0, Java 5, EJB3

Basically, if Ejb-B Exists on the server, it must be called ONLY on that server.

Which leads me to believe that the class is getting loaded by a local classloader (on deployment?), then when called remotely, a different classloader is loading it. (causing the Exception) But it should work, as it should be Serialized into the destination classloader...

What am I doing wrong??

Also, when reproducing this locally, Ejb-A would favor the Ejb-B on the same server, so it was difficult to reproduce. But this wasn't the case on other machines.

NOTE: This all worked correctly for EJB2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2024-09-12 15:39:27

因此,我能够通过向 bean 添加一个 @RemoteHome(MyRemoteHome.class) 来“修复”这个问题,

public interface MyRemoteMethods {
    String myMethod() throws RemoteException; // this Ex is required
}

public interface MyRemote extends EJBObject, MyRemoteMethods { 
}

public interface MyRemoteHome extends EJBHome {
    public MyRemote create() throws CreateException, RemoteException;
}

然后再向我的 bean

...
@RemoteHome(MyRemoteHome.class)
public class MyBean implements MyRemoteMethods {
...

添加。 以前,只有 bean 和 Remote 接口存在。 (没有本地使用)

总体而言,此修复使我的 EJB 3 更像 EJB 2,并且不那么干净(涉及更多代码和类)。我也不清楚为什么我可以从本地和远程服务器对RemoteHome接口进行jndi查找(并调用创建)并且不会得到ClassCastException,但是当我对远程接口执行此操作时,我确实得到一个ClassCastException。这是 Weblogic 中的错误吗? (我也看到过一些类似使用JBoss的帖子)

So I was able to 'fix' this issue by adding a @RemoteHome(MyRemoteHome.class) to the bean

public interface MyRemoteMethods {
    String myMethod() throws RemoteException; // this Ex is required
}

public interface MyRemote extends EJBObject, MyRemoteMethods { 
}

public interface MyRemoteHome extends EJBHome {
    public MyRemote create() throws CreateException, RemoteException;
}

then my bean

...
@RemoteHome(MyRemoteHome.class)
public class MyBean implements MyRemoteMethods {
...

Previously, only the bean and the Remote interface existed. (there is no local usage)

Overall, this fix makes my EJB 3 a bit more EJB 2-ish and not as clean (more code and classes involved). I am also unclear why I can do a jndi lookup for the RemoteHome interface (and call create) from my local and remote servers and not get a ClassCastException, but when I do this for the Remote interface, I do get a ClassCastException. Is this a bug in Weblogic? (I've also seen some posts similar using JBoss)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文