8

Let's say I want to create a library which I will use in future projects but I also want to include EJBs in that library referencing other EJBs etc.. That library would also contain simple java classes. What is the best way to do that? How do I define the dependencies in this case? I thought I would define them with annotations. If the user of the library wants to configure other dependencies he will be able to do so by overriding them inside the ejb-jar.xml of his project. Has anyone done something like this in the past? How would you go about it when developing in Eclipse?

Basically my problem is that as far as I can tell if I simply create an ejb-jar which I am going to include in all my projects the ejb container is going to instantiate my e.g. MDBs at deployment time even if I don't need all of the MDBs that are contained inside my library but only some of them.

Is a solution to not define MDBs as EJBs with annotations or inside the ejb-jar.xml but only their dependencies?

What about the session beans? Will they be automatically instantiated even if I don't use them inside a project?

Bat0u89
  • 610
  • 2
  • 17
  • 25
  • 1
    I don't know if this is the best way to do it but I've worked with a company before where we packed everything as a `.jar` and then included that as a maven dependency. We could then access the EJB's in the `.jar` as needed. Again, not sure if that's the best way but it is an option if you wanted to try. – Michael Platt Jan 05 '17 at 19:54
  • 1
    Stateless beans could be instantiated even if you don't use them, yes. SLSB must be pooled, so some instances could be created in advance, without any request. – user3714601 Jan 06 '17 at 12:38
  • As far as I remember, for most of the application servers on the market you can configure the pool with zero as initial size. Does this solve your problem ? – Leonardo Jan 10 '17 at 11:00
  • @Leonardo actually I am looking for a way to organize ejb code the best way possible. Mainly to create a library of ejbs with their dependencies defined somehow within the library but that are also configurable outside of it. I'm thinking that maybe this is not possible but I don't have deep knowledge of ejbs. – Bat0u89 Jan 12 '17 at 16:36

1 Answers1

2

EAR files. Although not very common, you could include your library and its dependencies in an EAR file and distribute that. What I'd like to do is to distribute the library in its own Jar file along with a documentation of dependencies (e.g.: a Maven' POM file or ivy's xml file). Either way, you'll need a dependency manager.

There is also an option to build a fat JAR file in which all the dependencies are exploded. I don't really like that. If I have to include dependencies, I'd go with EAR files.

Amir Keibi
  • 1,991
  • 28
  • 45