GIT:https://github.com/AndreaCatania/libreria
我的ear文件包含两个模块,EJB和WAR。
我有这个EJB接口
@Remote
public interface Library2IFace{
public String getName();
public void setName(String name);
public String getText();
public void setText(String text);
}
一切正常。 WAR模块正确执行EJB的锁定。
当我设置注释@Local而不是@Remote时,我收到此错误:
java.lang.ClassCastException: com.ac.ejbsclient.ejb.Library2IFace$$$view3 cannot be cast to com.ac.ejbsclient.ejb.Library2IFace
com.ac.test.servlet.MyObj.<init>(MyObj.java:15)
com.ac.test.servlet.DeServlet.doGet(DeServlet.java:17)
javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:197)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
更新 MyObj的代码是:
public class MyObj{
public MyObj(){
try{
lb1 = (LibraryIFace) new InitialContext().lookup("java:global/"+NAME_VERSION_EAR+"/"+NAME_VERSION_EJBMODULE+"/LibraryBean");
lb2 = (Library2IFace) new InitialContext().lookup("java:global/"+NAME_VERSION_EAR+"/"+NAME_VERSION_EJBMODULE+"/Library2Bean");
}catch( NamingException e ){
e.printStackTrace();
}
}
public void setName(String name){
lb1.setName(name);
}
public String getName(){
return lb1.getName();
}
public void setText(String text){
lb1.setText(text);
}
public String getText(){
return lb1.getText();
}
public void setName2(String name){
lb2.setName(name);
}
public String getName2(){
return lb2.getName();
}
public void setText2(String text){
lb2.setText(text);
}
public String getText2(){
return lb2.getText();
}
public void createBook(int bookId, String name){
lb1.createBook(bookId,name);
}
public Book getBook(int bookId){
return lb1.getBook(bookId);
}
private static LibraryIFace lb1;
private static Library2IFace lb2;
private static final String NAME_VERSION_EAR = "libreriaEar";
private static final String NAME_VERSION_EJBMODULE = "libreria-core-1.0-SNAPSHOT";
}
答案 0 :(得分:1)
堆栈跟踪中的错误消息表明调用者可以访问Library2IFace
类的不同副本,最有可能位于war文件&WEB-INF / lib目录中。
您需要确保整个EAR中只有一个类可用,并且它应该位于EAR / lib目录中的jar或拥有的ejb-jar中。
@ Ajan的建议是管理此事的常用方法。