如何解决JBOSS 6.0上ear文件之间的类加载问题?

发布于 2024-10-29 06:02:19 字数 2716 浏览 0 评论 0原文

我有一个包含此 bean 的耳朵文件,

@Stateless
public class ProjectServiceImpl implements ProjectServiceLocal, ProjectServiceRemote {

    @Override
    public List<Project> listProjects() {
        Project project = new Project();
        project.setId(0l);
        project.setName("Foo");
        return Arrays.asList(project);
    }

}

第二个耳朵文件有此 bean

@Stateless
public class EntryServiceImpl implements EntryServiceLocal, EntryServiceRemote {

    @EJB(lookup="packagename/ProjectServiceImpl/local")
    private ProjectServiceLocal projectService;

    @Override
    public List<Entry> listEntries() {
        Project project = projectService.listProjects().get(0); // here happens the class cast exception
        Entry entry = new Entry();
        entry.setId(0l);
        entry.setProject(project);
        entry.setFrom(new Date());
        entry.setTo(new Date());
        return Arrays.asList(entry);
    }
}

这是我得到的异常:

Exception in thread "main" javax.ejb.EJBException: java.lang.ClassCastException: packagename.dto.Project cannot be cast to packagename.dto.Project
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:183)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:251)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.required(CMTTxInterceptor.java:349)

我该如何解决这个问题?

我知道两个 Ear 文件都配置了不同的类加载器,但我不知道如何将两者配置为具有相同的类加载器。

这大致就是我的 jboss-app.xml 中的内容

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jboss-app
    PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\">

<jboss-app>
    <loader-repository>
        packagename:archive=ear1.ear
    </loader-repository>
</jboss-app>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jboss-app
    PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\">

<jboss-app>
    <loader-repository>
        packagename:archive=ear2.ear
    </loader-repository>
</jboss-app>

我还为每个耳朵创建了一个 jboss-classloading.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
    <classloading xmlns="urn:jboss:classloading:1.0" name="ear1.ear" domain="packagename">
</classloading>

但是

<?xml version="1.0" encoding="UTF-8"?>
    <classloading xmlns="urn:jboss:classloading:1.0" name="ear2.ear" domain="packagename">
</classloading>

没有任何效果。而且我找不到任何有用的文档。

I have an ear file which contains this bean

@Stateless
public class ProjectServiceImpl implements ProjectServiceLocal, ProjectServiceRemote {

    @Override
    public List<Project> listProjects() {
        Project project = new Project();
        project.setId(0l);
        project.setName("Foo");
        return Arrays.asList(project);
    }

}

a second ear file has this bean

@Stateless
public class EntryServiceImpl implements EntryServiceLocal, EntryServiceRemote {

    @EJB(lookup="packagename/ProjectServiceImpl/local")
    private ProjectServiceLocal projectService;

    @Override
    public List<Entry> listEntries() {
        Project project = projectService.listProjects().get(0); // here happens the class cast exception
        Entry entry = new Entry();
        entry.setId(0l);
        entry.setProject(project);
        entry.setFrom(new Date());
        entry.setTo(new Date());
        return Arrays.asList(entry);
    }
}

This is the exception I get:

Exception in thread "main" javax.ejb.EJBException: java.lang.ClassCastException: packagename.dto.Project cannot be cast to packagename.dto.Project
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:183)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:251)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.required(CMTTxInterceptor.java:349)

How can I solve this?

I understand that both ear files have different classloaders configured, but I don't know how to configure both to have the same classloader.

this is rougly what I have in my jboss-app.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jboss-app
    PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\">

<jboss-app>
    <loader-repository>
        packagename:archive=ear1.ear
    </loader-repository>
</jboss-app>

and

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jboss-app
    PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\">

<jboss-app>
    <loader-repository>
        packagename:archive=ear2.ear
    </loader-repository>
</jboss-app>

I also created a jboss-classloading.xml files for each ear:

<?xml version="1.0" encoding="UTF-8"?>
    <classloading xmlns="urn:jboss:classloading:1.0" name="ear1.ear" domain="packagename">
</classloading>

and

<?xml version="1.0" encoding="UTF-8"?>
    <classloading xmlns="urn:jboss:classloading:1.0" name="ear2.ear" domain="packagename">
</classloading>

But nothing works. And I can't find any helpful documentation.

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

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

发布评论

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

评论(1

我要还你自由 2024-11-05 06:02:20

通过加载器存储库标签,您可以告诉 JBoss 将每个耳朵放入单独的类加载器中。

如果您通常需要隔离,您可以将有问题的类放入 jar 文件中,例如 server//lib 中,以便它位于双耳都可以访问的基类加载器中(通过委托)。

或者,您可以将 Ear 部署器设置为没有类加载器隔离并删除这些加载器存储库,以便这些类最终位于 UCL 中。

With the loader repository tags, you tell JBoss to put each of the ears in a separate classloader.

If you generally need isolation, you could put the offending class within a jar file e.g. in server//lib so that it is in a base classloader that both ears have access to (through delegation).

Or you can set up the ear deployer to have no classloader isolation and remove those loader repositories, so that the classes end up in the UCL.

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