Can't put EJB in separate jar

I have an EJB with some JPA logic that I am using in my Java EE projects. Instead of copying the class in each project, I try to put it in a separate jar, so I have this structure:

  • Java EE project with EJB and WAR projects
  • JPALogic: JAR project with JPALogic class in it
  • RemoteServices: JAR project with beans interfaces
  • Services: EJB project with beans including JPALogic and RemoteServices as libraries
  • Frontend: WAR project with frontend including RemoteServices as library.

JPALogic is only used in the EJB project, and there is no reference to it in other parts of the Java EE application. In the JPALogic library, I have a JPALogic bean:

@Stateless
@LocalBean
public class JPALogic {
  @Resource
  private EJBContext ejbContext;
  @Inject @ShardedPersistenceContext
  private EntityManager em;
  public JPALogic() {
  }
  [...lots of methods...]
}

      

It works fine if the JPALogic code is directly in the EJB project, but when I put it in an external library, the deployment becomes very unstable (netbeans 8 + glassfish 4) and almost every deployment fails with this error:

Exception while deploying the app [app] : Cannot resolve reference [Remote ejb-ref name=com.my.autenticacion.services.AutRolBackendServices/jpa,Remote 3.x interface =com.my.infraestructura.jpa.JPALogic,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] because there are [2] ejbs in the application with interface com.my.infraestructura.jpa.JPALogic. 

      

I have a search, but it seems that such an error appears when the interface has multiple implementations, and it doesn't: there is only one bean named "JPALogic" and, checking ear, JPALogic.jar appears only once.

Am I doing something wrong?

+3


source to share


1 answer


If the jar contains EJB annotations (@Singleton, @Stateless, @Stateful annotations defining components), it is not a plain jar but an ejb-jar and should be packaged as a module.

Decision:

  • You should reference JPALogic.jar in application.xml as an EJB module.
  • You can also add "Class-Path: JPALogic.jar" to the META-INF / MANIFEST.MF of the referenced modules to ensure visibility. It may be visible, depending on the container.

Like this:



application.EAR
    META-INF/application.xml
        (must have a reference to the JPALogic.jar EJB module)
    JPALogic.jar (EJB-JAR)
    remoteservices.jar
    services.jar
        META-INF/MANITESF.MF Class-Path: JPALogic.jar
    remoteservices.jar
    frontend.war

      

For more details see JAVA EE 6 spec, chapter EE.8:

  • EE.8.3.2 Requirements for Loading an EJB Container Class
  • EE.8.5.2 Deploying a Java EE Application
0


source







All Articles