1

Can not inject EJB from one module to EJB in another module. Both modules on singe glassfish instance.

Remote interface in main module:

@Remote
public interface TestEJBLocal {
    String getMessage();

}

Implementation in main module:

@Stateless
public class TestEJB implements TestEJBRemote {
    @Override
    public String getMessage() {
        return "Hello EJB World";
    }
}

Remote interface in 3rdparty module(consumer):

@Remote
public interface TestEJBLocal {
    String getMessage();

}

Consumer bean in 3rdparty module:

@Named
@RequestScoped
public class testBean {

    @EJB(lookup="java:global/mavenproject3-ear/mavenproject3-ejb-1.0-SNAPSHOT/TestEJB!com.versetty.ejb.TestEJBRemote")
    private TestEJBRemote messageBean;

    public void doEJBCall() {
        System.out.println(messageBean.getMessage());
    }

}

Consumer JSF-page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Hello from Facelets
        <h:form>
            <h:commandButton action="#{testBean.doEJBCall()}" title="call"/>
        </h:form>
    </h:body>
</html>

Exception:

Caused by: java.lang.IllegalArgumentException: Can not set com.versetty.web.remote.TestEJBRemote field com.versetty.web.bean.testBean.messageBean to com.versetty.ejb._TestEJBRemote_Wrapper
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:688)
... 59 more
  • possible duplicate of [access a Local Session Bean from another EAR?](http://stackoverflow.com/questions/5681197/access-a-local-session-bean-from-another-ear) – hugh Jul 11 '15 at 14:30
  • 1
    Surely, the error indicates you are looking up a different bean definition to the one you are specifying at injection point: com.versetty.web.remote.TestEJBRemote and com.versetty.ejb._TestEJBRemote_Wrapper proxy bean would be having the same package naming – maress Jul 11 '15 at 18:12
  • It works. Thanks a lot. – Andrey Poputnikov Jul 11 '15 at 18:39

1 Answers1

0

You've used a @Local interface, but you're trying to access it from a different EJB application - you should use @Remote for this. This answer gives a great explanation of why.

Community
  • 1
  • 1
hugh
  • 2,237
  • 1
  • 12
  • 25
  • If i use Remote interface: Caused by: java.lang.IllegalArgumentException: Can not set com.versetty.web.remote.TestEJBRemote field com.versetty.web.bean.testBean.testRemote to com.versetty.ejb._TestEJBRemote_Wrapper – Andrey Poputnikov Jul 11 '15 at 14:38
  • Ok, that sounds like you've added a remote interface TestEJBRemote and a field "testRemote", but the field is still of type TestEJBLocal? If that's the case, please try changing it to TestEJBRemote. If not, could you please add the changes you've made and the error you got to your question as "I also tried...", so we can debug further? – hugh Jul 11 '15 at 15:03
  • See, i changed the question body. – Andrey Poputnikov Jul 11 '15 at 15:37
  • Thank you for making the update. From this (http://stackoverflow.com/questions/28774739/java-ee-stateless-ejb-illegalargumentexception-can-not-set-field) it looks like you also need public getters and setters for `messageBean` in `testBean`? – hugh Jul 11 '15 at 15:48
  • Are the getter and setter using TestEJBRemote too, not TestEJB? (Again, it would be good to add this code to the question). Could you double check too that the setter's name and signature is correct? The IAE is a reflection issue, so it looks the JEE has built the EJB alright, it just can't inject it on your field. If that doesn't work, could you try moving the EJB annotation to the setter method? – hugh Jul 11 '15 at 18:17
  • Tried move annotation to setter. maress rights! The package names must be the same. On the master bean, and on the 3rdparty consumer bean. – Andrey Poputnikov Jul 11 '15 at 18:39
  • Oh... yes, they do - are yours not? Does correcting that fix the error? (Ideally you should put your interfaces into a separate JAR which both components can have a dependency on, so you can guarantee both are using the same objects, and don't need repeated code. It also means you have a convenient artifact you can share with literal third parties.) – hugh Jul 11 '15 at 18:56
  • I understood: best practice is to put domain classes and remote ejb interfaces to external jars and use this jars as dependencies of other related modules of system. – Andrey Poputnikov Jul 12 '15 at 21:53