Module 5
Module 5
Concurrency
Assuming each Service is represented by one instance, dependency injection alone is
a fine solution for a single-threaded application; only one client may be accessing a
resource at a given time.
However, this quickly becomes a problem in situations where a centralized server is
fit to serve many simultaneous requests.
Transactions
A transaction is a single unit of work items, which follows the ACID properties.
ACID stands for Atomic, Consistent, Isolated, and Durable.
o Atomic − If any of the work item fails, the whole unit will be considered failed.
Success meant, all items execute successfully.
Timers
Timer Service is a mechanism by which scheduled application can be build. For
example, salary slip generation on the 1st of every month. EJB 3.0 specification has
specified @Timeout annotation, which helps in programming the EJB service in a
stateless or message driven bean. EJB Container calls the method, which is annotated
by @Timeout.
EJB Timer Service is a service provided by EJB container, which helps to create timer
and to schedule callback when timer expires.
JNDI stands for Java Naming and Directory Interface. It is a set of API and service
interfaces. Java based applications use JNDI for naming and directory services. In
context of EJB, there are two terms.
o Binding − This refers to assigning a name to an EJB object, which can be used
later.
Interoperability
Interoperability is a vital part of EJB. The specification includes the required support
for Java RMI-IIOP for remote method invocation and provides for transaction,
naming, and security interoperability.
EJB also requires support for JAX-WS, JAX-RPC, Web Services for Java EE, and
Web Services Metadata for the Java Platform specifications.
Lifecycle callbacks
Callback is a mechanism by which the life cycle of an enterprise bean can be
intercepted. EJB 3.0 specification has specified callbacks for which callback handler
methods are created. EJB Container calls these callbacks. We can define callback
methods in the EJB class itself or in a separate class. EJB 3.0 has provided many
annotations for callbacks.
Interceptors
While it’s really nice that EJB provides aspectized handling of many of the container
services, the specification cannot possibly identify all cross-cutting concerns facing
your project.
For this reason, EJB makes it possible to define custom interceptors upon business
methods and lifecycle callbacks. This makes it easy to contain some common code in
a centralized location and have it applied to the invocation chain without impacting
your core logic.
Platform Integration
As a key technology within the Java Enterprise Edition (JEE) 6, EJB aggregates many
of the other platform frameworks and APIs:
Java Transaction Service
Java Persistence API
Java Naming and Directory Interface (JNDI)
Security Services
Web Services
5.3 Developing your first EJB
Developing an EJB involves most of the same steps and concepts that you have become
familiar with when developing plain old Java objects (POJOs), with a few minor
differences. The following steps illustrate the typical process to develop and deploy an
EJB:
1. Write the classes and interfaces for your enterprise bean
2. Write a deployment descriptor
3. Package the enterprise bean and associated files inside of a jar file
4. Deploy the bean
5.4 Models
Session beans are divided into three basic types: stateless, stateful, and singleton.
The life of a singleton bean is very similar to that of the stateless session bean; it is either
not yet instantiated or ready to service requests. In general, it is up to the Container to
determine when to create the underlying bean instance, though this must be available
before the first invocation is executed. Once made, the singleton bean instance lives in
memory for the life of the application and is shared among all requests.
5.5 Message Driven Bean
A message driven bean (MDB) is a bean that contains business logic. But, it is invoked
by passing the message. So, it is like JMS Receiver.
JMS provides two types of messaging models.
o publish-and -subscribe(topic): messaging, one producer can send a message to
many consumers through a virtual channel called a topic.
o point -to-point (queue): on the other hand, is intended for a message that is to be
processed once
LifeCycle of a Message Driven Bean
The MDB instance’s lifecycle has two states: Does Not Exist and Method-Ready Pool.
The Method-Ready Pool is similar to the instance pool used for stateless session beans.
Mapping Persistence
The Programming model
Entities are plain Java classes in Java Persistence.
declare and allocate these bean classes just as you would any other plain Java object.
interact with the entity manager service to persist, update, remove, locate, and query for
entity beans.
The entity manager service is responsible for automatically managing the entity beans’
state.
This service takes care of enrolling the entity bean in transactions and persisting its state
to the database.
Entity Relationship
In order to model real-world business concepts, entity beans must be capable of forming
relationships.
need