jeudi 29 mai 2014

Java - les annotations EJB Demistifying et injection - Stack Overflow


I'm currently puzzled with the way Glassfish 3.1.2.2 handles EJBs.


I have an OSGi project, which consists of many OSGi bundles (jars). Also, there are a few WARs, including a Tapestry web application.


In one such bundle, let's call it "interfaces.jar", I have an interface defined:


public interface MyInterface() {
public static final String JNDI_NAME = "java:global/MyInterface";
void myMethod();
}

The implementation of that interface is as following, and it's contained in bundle "beans.jar":


@Stateless
@EJB(name = MyInterface.JNDI_NAME, beanInterface = MyInterface)
public class MyBean implements MyInterface() {
void myMethod() {
...
}
}

I am calling it from my Tapestry WAR app via JNDI lookup:


InitialContext.doLookup(MyInterface.JNDI_NAME);

Now, I was reading EJB 3.1 specification, and it says that I can one of the following scenarios:



  1. Interface has @Local annotation; EJB is implementing this interface.

  2. Interface is a plain Java interface without annotation; EJB with @Local annotation is implementing it.

  3. Interface is a plain Java interface without annotation; EJB is implementing it.

  4. Interface is a plain Java interface without annotation; EJB with @Local annotation is not implementing it.

  5. EJB doesn’t have any special annotations.


So, by elimination:



  1. I don't have @Local on interface

  2. I don't have @Local on EJB

  3. Seems somewhat right

  4. I don't have @Local on EJB

  5. I have @EJB annotation on my EJB


So, it seems that it's case 3:



"Because it’s the only implemented interface of the EJB, a container assumes that it must be a local business interface."



Now, a few questions:



  1. Is my interface a local or remote one, since there is no local or remote annotation?

  2. If it is local, I should be able to inject it with @EJB annotation, but it fails?

  3. If it's remote, it is not in compliance with the explanation a few lines above?

  4. If I add either @Local or @Remote, and perform JNDI lookup, I get a naming exception and NPE telling me there is nothing under that JNDI_NAME. How is that possible?

  5. What exactly does @EJB(name = ..., beanInterface = ...) do on bean class and how does it interact with @Local and @Remote annotations?



I'm currently puzzled with the way Glassfish 3.1.2.2 handles EJBs.


I have an OSGi project, which consists of many OSGi bundles (jars). Also, there are a few WARs, including a Tapestry web application.


In one such bundle, let's call it "interfaces.jar", I have an interface defined:


public interface MyInterface() {
public static final String JNDI_NAME = "java:global/MyInterface";
void myMethod();
}

The implementation of that interface is as following, and it's contained in bundle "beans.jar":


@Stateless
@EJB(name = MyInterface.JNDI_NAME, beanInterface = MyInterface)
public class MyBean implements MyInterface() {
void myMethod() {
...
}
}

I am calling it from my Tapestry WAR app via JNDI lookup:


InitialContext.doLookup(MyInterface.JNDI_NAME);

Now, I was reading EJB 3.1 specification, and it says that I can one of the following scenarios:



  1. Interface has @Local annotation; EJB is implementing this interface.

  2. Interface is a plain Java interface without annotation; EJB with @Local annotation is implementing it.

  3. Interface is a plain Java interface without annotation; EJB is implementing it.

  4. Interface is a plain Java interface without annotation; EJB with @Local annotation is not implementing it.

  5. EJB doesn’t have any special annotations.


So, by elimination:



  1. I don't have @Local on interface

  2. I don't have @Local on EJB

  3. Seems somewhat right

  4. I don't have @Local on EJB

  5. I have @EJB annotation on my EJB


So, it seems that it's case 3:



"Because it’s the only implemented interface of the EJB, a container assumes that it must be a local business interface."



Now, a few questions:



  1. Is my interface a local or remote one, since there is no local or remote annotation?

  2. If it is local, I should be able to inject it with @EJB annotation, but it fails?

  3. If it's remote, it is not in compliance with the explanation a few lines above?

  4. If I add either @Local or @Remote, and perform JNDI lookup, I get a naming exception and NPE telling me there is nothing under that JNDI_NAME. How is that possible?

  5. What exactly does @EJB(name = ..., beanInterface = ...) do on bean class and how does it interact with @Local and @Remote annotations?


0 commentaires:

Enregistrer un commentaire