enterprise javabeans (ejb) overview -...

30
Enterprise JavaBeans (EJB) Overview Revision: v2018-10-16 Built on: 2018-12-05 08:37 EST Copyright © 2018 jim stafford ([email protected]) This presentation provides an introduction to enterprise development using the server-side Enterprise JavaBean (EJB) standard.

Upload: duongkiet

Post on 12-Aug-2019

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Enterprise JavaBeans (EJB) Overview

Revision: v2018-10-16Built on: 2018-12-05 08:37 EST

Copyright © 2018 jim stafford ([email protected])

This presentation provides an introduction to enterprise development using the server-side

Enterprise JavaBean (EJB) standard.

Page 2: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the
Page 3: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

iii

Purpose ............................................................................................................................ v

1. Goals .................................................................................................................... v

2. Objectives ............................................................................................................. v

1. Why Enterprise? .......................................................................................................... 1

1.1. Related Patterns ................................................................................................. 1

1.1.1. Pattern: Session Facade .......................................................................... 1

1.2. Pattern: Remote Facade ..................................................................................... 3

1.2.1. Context .................................................................................................... 3

1.2.2. Problem ................................................................................................... 4

1.2.3. Forces ..................................................................................................... 4

1.2.4. Solution ................................................................................................... 4

1.2.5. Consequences ......................................................................................... 5

1.3. Pattern: Data Transfer Object (DTO) .................................................................... 5

1.3.1. Context .................................................................................................... 5

1.3.2. Problem ................................................................................................... 6

1.3.3. Forces ..................................................................................................... 6

1.3.4. Solution ................................................................................................... 6

1.3.5. Consequences ......................................................................................... 7

2. Overview of EJB Styles ............................................................................................... 9

2.1. EJB Uses ........................................................................................................... 9

2.2. EJB Granularity .................................................................................................. 9

2.3. EJB Bean Types ................................................................................................ 9

2.3.1. Entities (formerly Entity EJB) .................................................................... 9

2.3.2. Stateless Session EJB ........................................................................... 10

2.3.3. Stateful Session EJB .............................................................................. 12

2.3.4. Singleton Session EJB ........................................................................... 14

2.3.5. Message Driven EJB (MDB) ................................................................... 15

2.4. EJB Interface Styles .......................................................................................... 15

2.4.1. Business Interface .................................................................................. 15

2.4.2. Remote Interface .................................................................................... 16

2.4.3. Local Interface ....................................................................................... 16

2.4.4. No Interface EJB .................................................................................... 17

2.4.5. Other Interface Types ............................................................................. 17

2.5. EJB Deployments ............................................................................................. 18

2.5.1. EJB Module ........................................................................................... 18

2.5.2. Naked EJB Deployment .......................................................................... 19

2.5.3. EJB EAR Deployment ............................................................................ 19

2.5.4. EJB WAR ("flexible") Deployment ............................................................ 20

2.5.5. EAR Deployment Class Loaders ............................................................. 20

A. Sources ...................................................................................................................... 23

Page 4: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

iv

Page 5: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

v

Purpose

1. Goals• Introduce the purpose of an EJB (why?)

• Introduce the different EJB types

• Introduce different EJB interface options

• Introduce different deployment options for EJBs

2. Objectives

At the completion of this topic, the student shall be able to:

• State reasons for/against adding an EJB to a JavaEE application

• State the four different types of EJBs and reasons for/against using each

• State the three different built-in EJB interface types and when to/not-to use each

• State the three different deployment options and when to/not-to use each

Note

Web integration adds an additional HTTP/REST-capable interface to EJB.

Page 6: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

vi

Page 7: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 1.

1

Why Enterprise?

Figure 1.1. Needs and JavaEE/EJB Implementation Options

1.1. Related Patterns

1.1.1. Pattern: Session Facade

1.1.1.1. Context

• Multi-tiered application

• Non-trivial business logic and data

1.1.1.2. Problem

• Potential tight coupling between clients and complex business objects

• Too many method invocations between clients and business objects

• Business methods exposed for misuse by clients

Page 8: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 1. Why Enterprise?

2

Figure 1.2. Session Facade Problem

1.1.1.3. Forces

• Hide complex interactions behind a simple client interface

• Reduce the number of business objects exposed to the client across the network

• Hide implementation, interactions, and dependencies of business components

1.1.1.4. Solution

• Use a business logic class to encapsulate the interactions required with the business objects

Figure 1.3. Session Facade Participants

Page 9: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Pattern: Remote Facade

3

Figure 1.4. Session Facade Solution

1.1.1.5. Consequences

• Simplifies complex systems

• May appear to be a no value pass-thru in simple systems

• Should involve more than one business object per facade

• Should have more than one facade per system

• Decouples the business objects from being aware of one another

• Improves perceived network performance for remote interfaces

• Centralizes security and transactions in some cases

POJO Business Logic is an example of Session Facade

Pattern

You have already implemented the Session Facade Pattern with your POJO

business logic from previous work. There is nothing magical about server-side

EJBs over POJO business logic, relative to the Session Facade Pattern, except

for the enhanced ability to implement remote, transactional, secure, and load-

balanced behavior without impacting the focus on the business.

1.2. Pattern: Remote Facade

1.2.1. Context

• Distributed environment with remote clients

Page 10: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 1. Why Enterprise?

4

1.2.2. Problem

• Remote clients require access to server-side resources.

Figure 1.5. Remote Facade Problem

1.2.3. Forces

• Core service layer (business logic and its business objects) contains fine grain objects and

methods

• Significant number of fine-grain remote calls will not work

1.2.4. Solution

• Provide a Remote Facade

• Coarse-grain facade over service layer

• Contains no detailed business logic -- it calls it

• Translates course-grain methods and objects into fine-grain method calls and objects to/from

service layer

• Bulk accessors wrap fine-grain access methods

• Service Layer does not have a remote interface

• Fine-grain business objects mapped to database might not be serializable

Page 11: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Consequences

5

Figure 1.6. Remote Facade Participants

Figure 1.7. Remote Facade Solution

1.2.5. Consequences

• Remote clients obtain access to desired resources (functional)

• Remote client interaction is efficient (appropriate)

1.3. Pattern: Data Transfer Object (DTO)

1.3.1. Context

• Business Objects represent too much information or behavior to transfer to remote client

Page 12: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 1. Why Enterprise?

6

1.3.2. Problem

Figure 1.8. DTO Problem

• Client may get information they don't need

• Client may get information they can't handle

• Client may get information they are not authorized to use

• Client may get too much information/behavior to be useful (e.g., entire database serialized to

client)

1.3.3. Forces

• Some clients are local and can share object references with business logic

• Handling specifics of remote clients outside of core scope of business logic

1.3.4. Solution

• Layer a Remote Facade over Business Logic

• Remote Facade constructs Data Transfer Objects (DTOs) from Business Objects that are

appropriate for remote client view

• Remote Facade uses DTOs to construct or locate Business Objects to communicate with

Business Logic

Page 13: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Consequences

7

Figure 1.9. DTO Participants

1.3.5. Consequences

• Clients only get what they need

• Clients only get what they understand

• Clients only get what they are authorized to use

• Remote and Local interfaces to services are different

• Makes it harder to provide location transparency

• Lightweight Business Objects can be used as DTOs

• Remote Facade must make sure they are “pruned” of excess related items before transferring

to client

• Remote Facade must make sure they are “cleaned” of DAO persistence classes before

transferring to client

Page 14: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

8

Page 15: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2.

9

Overview of EJB Styles

2.1. EJB Uses• Implement a task

• Interact with other beans

• Examples:

• TaxDistrict.calcTax(double amount)

• Teller.transfer(long fromAccount, long toAccount, double amount)

• Registrar.listStudents(String course, int offset, int limit)

2.2. EJB Granularity• Session EJB per Use Case too fine

• CreateAccountEJB

• DepositEJB

• WithdrawEJB

• TransferEJB

• Group cohesive Use Cases into larger-grain Session EJBs

• Teller

• createAccount()

• deposit()

• withdraw()

• transfer()

2.3. EJB Bean Types• All are POJO-based

• i.e., most Java classes can be made into an EJB

2.3.1. Entities (formerly Entity EJB)

Figure 2.1. Example Entity

@javax.persistence.Entity

@javax.persistence.Queries({

@javax.persistence.Query(name="Reservation.getReservationsByName", query="

select r from Reservation r

where r.person.firstName like :firstName and

r.person.lastName like :lastName")

})

public class Reservation {

@javax.persistence.Id

private long id;

private String confirmation;

@javax.persistence.Temporal(javax.persistence.TemporalType.DATE)

private Date startDate;

Page 16: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2. Overview of EJB Styles

10

@javax.persistence.Temporal(javax.persistence.TemporalType.DATE)

private Date endDate;

@javax.persistence.ManyToOne

@javax.persistence.JoinColumn

private Person person;

• Represents shared data in the database

• Typically interact at row/object level

• Moved to Java Persistence API (JPA) Spec in EJB 3.0/JavaEE 5

• Not constrained to server-side

• Examples:

• Account, Owner

• Account.deposit(double amount)

• Account.getOwner()

• Good for representing persistent information

• Contains no means to access external resources other than directly related information

2.3.2. Stateless Session EJB

Figure 2.2. Stateless EJB Lifecycle

Figure 2.3. Example Stateless EJB

@javax.ejb.Stateless

public class HotelMgmtEJB implements HotelMgmtRemote, HotelMgmtLocal {

@PersistenceContext(unitName="ejbjpa-hotel")

private EntityManager em;

private HotelDAO dao;

private HotelMgmt hotelMgmt;

@PostConstruct

public void init() {

dao = new JPAHotelDAO();

((JPAHotelDAO)dao).setEntityManager(em);

Page 17: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Stateless Session EJB

11

hotelMgmt = new HotelMgmtImpl();

((HotelMgmtImpl)hotelMgmt).setHotelDao(dao);

}

@Override

public Room getRoom(int number) {

return hotelMgmt.getRoom(number);

}

}

• Performs stateless actions on server-side

• Maintains no conversational state

• Each method ignorant of what went before it and after it

• Persistence Context re-instantiated with each call (Transaction-Scope)

• Methods take in a set of parameters and return a result

• May maintain implementation state when pooled

• example: DataSource, JMS resources, references to other EJBs

• Not the default behavior. Only an option when number of instances needed is limited and

initialization is expensive

• In no case should business state ever be maintained outside of the scope of a method or a

back-end database or other stateful resource.

• Examples:

• TaxCalculator.calcTax(doubt amount)

• BuyerMgmt.placeBid(int auctionId, double amount)

• Dmv.getExpiredLicenses()

• Good for implementing stateless access to resources

• Contains no means to provide individualized access other than through separate deployments

and call parameters

Page 18: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2. Overview of EJB Styles

12

2.3.3. Stateful Session EJB

Figure 2.4. Stateful EJB Lifecycle

Figure 2.5. Example Stateful EJB

@javax.ejb.Stateful

public class ReservationEJB implements ReservationRemote {

@PersistenceContext(unitName="ejbjpa-hotel", type=PersistenceContextType.EXTENDED)

private EntityManager em;

@EJB

private HotelMgmtLocal hotelMgmt;

@Resource

private SessionContext ctx;

private List<Guest> guests = new LinkedList<Guest>();

@Override

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

public int addGuest(Guest guest) {

if (guest!=null) {

guests.add(guest);

em.persist(guest); //<== no transaction active yet

}

return guests.size();

}

@Override

@TransactionAttribute(TransactionAttributeType.REQUIRED)

@Remove

public List<Guest> reserveRooms() throws RoomUnavailableExcepton {

List<Room> rooms = hotelMgmt.getAvailableRooms(null, 0, guests.size());

Page 19: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Stateful Session EJB

13

if (rooms.size() < guests.size()) {

//no rollback needed, we didn't do anything

throw new RoomUnavailableExcepton(String.format("found on %d out of %d required",

rooms.size(), guests.size()));

}

//assign each one of them a room

List<Guest> completed = new ArrayList<Guest>(guests.size());

Iterator<Room> roomItr = rooms.iterator();

for (Guest guest: guests) {

Room room = roomItr.next();

try {

//the room could be unavailable -- depending on whether pessimistic lock created

guest = hotelMgmt.checkIn(guest, room); //<== will attempt to also persist guest

completed.add(guest);

} catch (RoomUnavailableExcepton ex) {

//rollback any previous reservations

ctx.setRollbackOnly();

throw ex;

}

}

return completed;

}

}

• Used to cache resources for client on server-side

• Maintains conversational state

• Object can cache values between calls

• example: iterator

• Persistence Context (with cache of entities) can be retained between calls (Extended-

Scope)

• Lifetime ends on timeout or specific client call

• Maintains implementation state

• Not sharable between clients

• All resources allocated to perform work for one instance

• Able to react to transaction completion/rollback (i.e., get callback)

• example: commit data cache

• example: issue message

• Good for caching client state and back-end resource state over a multi-call session

• Inefficient to scale cached state for each user and across multiple servers

Page 20: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2. Overview of EJB Styles

14

2.3.4. Singleton Session EJB

Figure 2.6. Singleton EJB Lifecycle

Figure 2.7. Example Singleton EJB

@javax.ejb.Singleton

@javax.ejb.Startup

@javax.ejb.ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)

@javax.ejb.AccessTimeout(value=3000)

public class StatsEJB implements StatsLocal, StatsRemote {

private int total;

@Override

@javax.ejb.Lock(LockType.WRITE)

public void increment() { ... }

@Override

@javax.ejb.Lock(LockType.READ)

public int getTotal() { ... }

...

}

• Provides a single Session EJB instance on server-side

• Single instance shared across all clients

• Has READ/WRITE lock and timeout concerns

• Good for tracking shared state in memory

• Creates a bottleneck for concurrent access

Page 21: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Message Driven EJB (MDB)

15

2.3.5. Message Driven EJB (MDB)

Figure 2.8. Example Message Driven EJB

@javax.ejb.MessageDriven(activationConfig={

@javax.ejb.ActivationConfigProperty(

propertyName="destinationType",

propertyValue="javax.jms.Topic"),

@javax.ejb.ActivationConfigProperty(

propertyName="destination",

propertyValue="java:/topic/ejava/examples/asyncMarket/topic1"),

@javax.ejb.ActivationConfigProperty(

propertyName="messageSelector",

propertyValue="JMSType in ('forSale', 'saleUpdate')"),

@javax.ejb.ActivationConfigProperty(

propertyName="acknowledgeMode",

propertyValue="Auto-acknowledge")

})

public class BuyerMDB implements MessageListener {

@javax.ejb.EJB

private BuyerLocal buyer;

@javax.annotation.security.PermitAll

public void onMessage(javax.jms.Message message) {

...

}

}

• Used to receive JMS messages

• Similar to Stateless EJB, but with no callable interface

• Typically provides a JMS facade for injected Session EJBs

• Good for implementing a JMS interface facade to business logic

• Unable to be called outside the scope of a JMS/JCA call -- should delegate to Session EJBs

• Has no client context (i.e., anonymous)

• Can be assigned access roles using @javax.annotation.security.RunAs to be permitted to

invoke methods of protected EJBs

2.4. EJB Interface Styles

2.4.1. Business Interface

Figure 2.9. Example Business Interface

public interface Teller {

Account createAccount(String accNum) throws BankException;

void updateAccount(Account account) throws BankException;

Account getAccount(String acctNum) throws BankException;

}

Page 22: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2. Overview of EJB Styles

16

• Pure POJO

• No ties to EJB

• No sense of local or remote -- just local

2.4.2. Remote Interface

Figure 2.10. Example Remote Interface

@javax.ejb.Remote

public interface TellerRemote extends Teller {

Ledger getLedger() throws BankException;

List<Owner> getOwners(int index, int count) throws BankException;

String whoAmI();

}

public class Ledger implements java.io.Serializable {

...

}

• Uses RMI-based technology to provide built-in implementation

• Data types must be built-in types or Serializable types

• Pass-by-value semantics between client and EJB

• Usable by local (same application/EAR) and remote (outside of same application/EAR)

endpoints

2.4.3. Local Interface

Figure 2.11. Example Local Interface

@Local

public interface TellerLocal extends Teller {

}

• Pass-by-reference semantics between client and EJB

• client and EJB share the same object instance

• Must load class from same classloader

• Data types need not be built-in or Serializable types

• Restricted to be from application (e.g., must be from same EAR to share reference)

Design Remote Interfaces for Remote Clients

Remote clients are different from local clients and require different considerations

when designing the remote interface. Although JavaEE/EJB make it technically

possible to implement location independence the interface hierarchies, this is

usually not feasible when dealing with non-trivial interface requirements. Design

Page 23: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

No Interface EJB

17

local interfaces for local clients (within the deployed application) and design remote

interfaces for remote clients.

2.4.4. No Interface EJB

Figure 2.12. Example No Interface EJB

@javax.ejb.Singleton

@javax.ejb.Startup

@javax.ejb.ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)

@javax.ejb.AccessTimeout(value=3000)

public class StatsEJB {

private int total;

@javax.ejb.Lock(LockType.WRITE)

public void increment() { ... }

@javax.ejb.Lock(LockType.READ)

public int getTotal() { ... }

...

}

• Interfaces are an encapsulation issue and not an EJB issue

• If you do not need the encapsulation of an interface -- EJB will *not* make you add one for

local access

• Must have interface when using remote clients

2.4.5. Other Interface Types• JAX-RS support

• SOAP support

• CORBA/IIOP support

Figure 2.13. Example JAX-RS Interface

import java.net.URI;

import javax.inject.Inject;

import javax.ws.rs.Consumes;

import javax.ws.rs.FormParam;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.Context;

import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.UriBuilder;

import javax.ws.rs.core.UriInfo;

@Path("/products")

Page 24: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2. Overview of EJB Styles

18

public class ProductsResource {

@Inject

private InventoryMgmtEJB ejb;

@Context

private UriInfo uriInfo;

/**

* Updates a product with the values of the object passed in

* @param id

* @param product

* @return updated product if successful

*/

@PUT @Path("{id}")

@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})

@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})

@JsonbAnnotation //helps trigger JSON-B response marshaling when DTO also JAXB

public Response updateProduct(@PathParam("id") int id,

@JsonbProperty Product product //annotation helps trigger JSON-B

//request demarshaling when DTO also JAXB

) {

logger.debug("{} {}", request.getMethod(), uriInfo.getAbsolutePath());

try {

Product p = ejb.updateProduct(product);

logger.info("updated pojo product={}", p);

return Response.ok(p)

.tag("" + p.getVersion())

.build();

} catch (Exception ex) {

return ResourceHelper.serverError(logger, "update product", ex).build();

}

}

}

2.5. EJB Deployments

2.5.1. EJB Module

Figure 2.14. EJB Module

• Contains one or more EJB beans

Page 25: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Naked EJB Deployment

19

2.5.2. Naked EJB Deployment

Figure 2.15. Naked EJB Deployment

• EJB modules are deployable by themselves

• Contain no mechanism to bring in dependencies

• Can only be communicated with using remote interface

2.5.3. EJB EAR Deployment

Figure 2.16. EAR Deployment

• Can deploy multiple EJB modules, multiple WARs, and utility libraries

• Provides means to use local interfaces/pass by reference between EJBs and WAR/WEB

• Convenient wrapper for entire application

Page 26: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

Chapter 2. Overview of EJB Styles

20

2.5.4. EJB WAR ("flexible") Deployment

Figure 2.17. WAR Deployment

• Can deploy multiple EJB modules within WEB-INF/lib

• Can deploy multiple EJB beans within WEB-INF/classes

• Realistically useful when Servlet or JSP wishes to use declarative JTA transactions or security

access control within a helper class

• Ideal use for No-interface Session EJB

• Eliminates need for EAR in many cases

2.5.5. EAR Deployment Class Loaders

Figure 2.18. Class Loaders

• Arranged in a parent/child relationship

• May access classes/resources loaded by local and parent class loaders

• Do not have access to classes/resources loaded by sibling class loaders

• Requests for classes are first delegated to parent class loader

Figure 2.19. Separate Deployments

• Classes loaded by one EJB deployment are not sharable with other deployments

Page 27: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

EAR Deployment Class Loaders

21

• Must use remote interfaces and pass-by-value serialized data

• No pass by reference

• EJB classes/resources can be shared with single Web App if loaded in WEB-INF/lib

• Local interfaces/resources are local to that WAR

• EJB classes cannot be shared across WARs

• Must again resort to remote interfaces and pass-by-value serialized data to share

implementations

Figure 2.20. EAR Class Loaders

• EAR class loader is root to deployed application

• EJB class loader is host to all EJBs and root to WAR class loaders

• WAR class loaders are separate

• Allows two separate WARs to generate the same class from two separate index.jsp files

• Disallows WAR from duplicating EAR/EJB-supplied classes

• EJBs and WARs-to-EJBs can share information using pass-by-reference

Page 28: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

22

Page 29: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

23

Appendix A. Sources1. "JSR 345: Enterprise JavaBeansTM 3.2", Marina Vatkina, https://jcp.org/en/jsr/detail?id=345,

2013 Java Community Press.

2. "EJB Version History", Wikipedia, http://en.wikipedia.org/wiki/

Enterprise_JavaBeans#Version_history, August 17, 2014.

Page 30: Enterprise JavaBeans (EJB) Overview - webdev.jhuep.comjcs/ejava-javaee/coursedocs/content/pdf/ejb...v Purpose 1. Goals • Introduce the purpose of an EJB (why?) • Introduce the

24