How to solve class loading problem between ear files on JBOSS 6.0?
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(CMTTxIntercep开发者_如何学Gotor.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.
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.
精彩评论