Andrea Catania
Andrea Catania

Reputation: 1383

Error if the @Local annotation is used (EJB)

GIT: https://github.com/AndreaCatania/libreria

I have an ear file that contain two module, EJB and WAR.

I have this interface for EJB

@Remote
public interface Library2IFace{
    public String getName();
    public void setName(String name);
    public String getText();
    public void setText(String text);
}

All works fine. The WAR module does the lockup of the EJB correctly.

When I set the Annotation @Local instead of @Remote I get this error:

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)

UPDATE The code of MyObj is:

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";
}

Upvotes: 1

Views: 280

Answers (1)

Steve C
Steve C

Reputation: 19445

The error message in the stack trace indicates that the caller has access to a different copy of the Library2IFace class, most likely in the war file's /WEB-INF/lib directory.

You need to ensure that there is only one copy of the class available in the entire EAR, and it should be in a jar in the EAR/lib directory or the owning ejb-jar.

@Ajan's suggestion is a common way of managing this.

Upvotes: 1

Related Questions