Search code examples
jakarta-eeejbejb-3.0java-ee-6business-logic

Where to put business logic in a Java EE enterprise application


When creating an enterprise Java EE application, part of the business logic like messaging (MDBs) has to be put inside the EJB module. However, there are some EJBs which can be placed inside either the EJB module or the web module. I know that separating modules allows the web tier and the business tier to be deployed in different machines. Thus, I would place my @Stateful shopping cart EJB in the web module. However, I cannot think of a standard criteria which can be applied to every piece of business logic, deciding where to put the EJBs enclosing them. Is there a guideline, standard or recommended practice for this?


Solution

  • This is what I've found out so far.

    The EJB module is supposed to enclose the core of the business logic for a large enterprise application. Web module, on the other hand, encloses a full-fledged web application which is supposed to take advantage of the business engine provided by the EJB module. Usually these modules are distributed among different servers for larger applications, and there are client applications (a Swing/JFX desktop application for instance) other than the web application which need to consume services provided by the core business logic which resides in EJB modules.

    In other words, one would put the business logic in a separate EJB module if:

    1. It is meant to be used by multiple web or desktop applications, or
    2. Scaling considerations may force the core business logic to be handled by separate servers

    In other cases, one may put his EJBs inside the web module and package the whole thing as a WAR file.