EJB

EJB: Enterprise JavaBean

EJBs are server-side components that encapsulate business logic and take care of transactions and security.

  • Programming Model: annotate a single POJO that will be deployed into a container.
  • EJB container: a runtime environment that provides services.

Architecture Overview of EJB

1. Types of Session Beans

Mainly Session Beans

  • Stateless: The session bean contains no conversational state between methods, and any instance can be used for any client. It is used to handle tasks that can be concluded with a single method call.
  • Stateful: The session bean contains conversational state, which must be retained across methods for a single user. It is useful for tasks that have to be done in several steps.
  • Singleton: A single session bean is shared between clients and supports concurrent access. The container will make sure that only one instance exists for the entire application.

2. Containers

  • In-process container: separated process using application servers, required start/stop.
  • Embedded container: run on JavaSE

3. Services Given by the Container

  • Remote client communication
  • Dependency injection
  • State management
  • Pooling
  • Component life cycle
  • Messaging
  • Transaction management
  • Security
  • Concurrency support
  • Interceptors
  • Asynchronous method invocation

3. Specification, API packages, RI

(1) EJB Lite vs. Full EJB 3.2

(2) API packages:

  • javax.ejb: Classes and interfaces that define the contracts between the EJB and its clients and between the EJB and the container
  • javax.ejb.embeddable Classes for the embeddable API
  • javax.ejb.spi Interfaces that are implemented by the EJB container

(3) Reference Implementation: Glassfish 4

4. EJB

An EJB includes
(1) a bean class annotated by @Stateless or @Stateful or @Singleton or by XML DD
(2) a interface (methods implemented by bean class) annotated by @Local or @Remote or @WebService or Non-interface (@LocalBean)

@Local
public interface ItemLocal {
List<Book> findBooks();
List<CD> findCDs();
}
@WebService
public interface ItemSOAP {
List<Book> findBooks();
List<CD> findCDs();
Book createBook(Book book);
CD createCD(CD cd);
}
@Path(/items)
public interface ItemRest {
List<Book> findBooks();
}
@Stateless
public class ItemEJB implements ItemLocal, ItemSOAP, ItemRest {
// ...
}

5. JDNI

JNDI names have been specified so the code could be portable. Each time a session bean with its interfaces is deployed to the container, each bean/interface is automatically bound to a portable JNDI name.

java:<scope>[/<app-name>]/<module-name>/<bean-name>[!<fully-qualified-interface-name>]

(1) <scope>:= global | app | module | comp
(2) <app-name>:= name.ear | name.war
(3) <module-name>:= name.jar
(4) <bean-name>:= class_name of the bean
(5) <fully-qualified-interface-name>:= like henry416.mytest.myclass.mymethod

package org.agoncal.book.javaee7;
@Stateless
@Remote(ItemRemote.class)
@Local(ItemLocal.class)
@LocalBean
public class ItemEJB implements ItemLocal, ItemRemote {
// ...
}

Once deployed, the container will create three JNDI names so an external component will be able to access the ItemEJB using the following global JNDI names:

java:global/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemRemote
java:global/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemLocal
java:global/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemEJB
//in myapplication.ear
java:global/myapplication/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemRemote
java:global/myapplication/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemLocal
java:global/myapplication/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemEJB
// in same application
java:app/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemRemote
java:app/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemLocal
java:app/cdbookstore/ItemEJB!org.agoncal.book.javaee7.ItemEJB
java:module/ItemEJB!org.agoncal.book.javaee7.ItemRemote
java:module/ItemEJB!org.agoncal.book.javaee7.ItemLocal
java:module/ItemEJB!org.agoncal.book.javaee7.ItemEJB

Project
(1) mvn archetype:generate
(2)Choose org.codehaus.mojo.archetypes:ejb-javaee7 version:

Sample Links
https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html
http://www.tutorialspoint.com/ejb/
https://blogs.oracle.com/roumen/entry/accessing_ejb_3_session_beans
http://javahowto.blogspot.ca/2007/06/simple-ejb-3-application-hand-made.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s