enterprise java b19
TRANSCRIPT
-
7/29/2019 Enterprise Java B19
1/19
-
7/29/2019 Enterprise Java B19
2/19
What is EJB?
EJB is a server-side component that encapsulates
the business logic of an application.
Business logic is the code that fulfills the purpose
of the application; In Banking application EJB
might implement the business logic in methods
called transferFund, checkingBalance.
EJB is not for end-user, EJB is a middleware forclient application
-
7/29/2019 Enterprise Java B19
3/19
Benefits of EJB
For several reasons, enterprise beans simplifythe development of large, distributedapplications. First, because the EJB container
provides system-level services to enterprisebeans, the bean developer can concentrate onsolving business problems. The EJBcontainerand not the bean developeris
responsible for system-level services such astransaction management and securityauthorization.
-
7/29/2019 Enterprise Java B19
4/19
Second, because the beansand not the
clientscontain the application's business logic,
the client developer can focus on the
presentation of the client. The client
developer does not have to code the routines
that implement business rules or access
databases. As a result, the clients are thinner,a benefit that is particularly important for
clients that run on small devices.
-
7/29/2019 Enterprise Java B19
5/19
Third, because enterprise beans are portable
components, the application assembler can
build new applications from existing beans.
These applications can run on any compliant
Java EE server provided that they use the
standard APIs
-
7/29/2019 Enterprise Java B19
6/19
When to Use EJB
You should consider using enterprise beans if yourapplication has any of the following requirements:
The application must be scalable. To accommodate agrowing number of users, you may need to distribute an
application's components across multiple machines.
Transactions must ensure data integrity. Enterprise beanssupport transactions, the mechanisms that manage theconcurrent access of shared objects.
The application will have a variety of clients. With only afew lines of code, remote clients can easily locateenterprise beans. These clients can be thin, various, andnumerous.
-
7/29/2019 Enterprise Java B19
7/19
Type of EJB
Session Bean
Message-Driven Bean
Entity beans have been replaced by JavaPersistence API
-
7/29/2019 Enterprise Java B19
8/19
What is Session Bean?
A key difference between session bean and other beantypes is the scope of their lives. A session beaninstance is relatively short-lived object.
Session bean instance is created when a client requestit, and ends when it finishing process the client request
Session bean instances are not share between multipleclients and do not represent data in database
Session bean is used for interacting with applicationclient
Session bean can implements business logic of anapplication
-
7/29/2019 Enterprise Java B19
9/19
Session Bean Sub Types
Stateless Session Beans
Is a session bean that do not hold state across
client request
When a client invokes the methods of a stateless
bean, the bean's instance variables may contain a
state specific to that client, but only for the
duration of the invocation. When the method isfinished, the client-specific state should not be
retained
-
7/29/2019 Enterprise Java B19
10/19
Stateful Session Bean
Is a session bean that hold the conversation state
of a specific client with the server
Its like Session object (HttpSession) on web
application
The state is retained for the duration of the client-
bean session. If the client removes the bean orterminates, the session ends and the state
disappears.
-
7/29/2019 Enterprise Java B19
11/19
When to Use Session Beans
Stateful session beans are appropriate if any
of the following conditions are true:
The bean's state represents the interaction
between the bean and a specific client.
The bean needs to hold information about the
client across method invocations.
-
7/29/2019 Enterprise Java B19
12/19
To improve performance, you might choose a
stateless session bean if it has any of these
traits:
The bean's state has no data for a specific client.
In a single method invocation, the bean performs
a generic task for all clients. For example, you
might use a stateless session bean to send anemail that confirms an online order, etc..
-
7/29/2019 Enterprise Java B19
13/19
Session Bean: Local and Remote
Interface
To invoked a session bean, a client need to
create the session bean object through its
interface
Session bean has 2 interface: local and remote
-
7/29/2019 Enterprise Java B19
14/19
When to use Local or Remote?
Use Local interface if the client is in one
packaging with the ejb (.ear)
Use Remote interface if the client is in remote
location or deploy separately with ejb (.war
and .jar)
-
7/29/2019 Enterprise Java B19
15/19
Session bean best practices
Choose your bean type carefully. Stateless sessionbeans will be suitable most of the time. Carefullyexamine whether your application needs stateful
session beans, because it comes with a price. Ifthe EJB client lies in the web tier, then usingHttpSession may be a better choice than statefulsession beans.
Carefully examine interface types for sessionbeans. Remote interfaces involve network accessand may slow down your applications.
-
7/29/2019 Enterprise Java B19
16/19
Try it
-
7/29/2019 Enterprise Java B19
17/19
Session Bean Pattern
Ensures that the common services are
supported by both Remote and Local
interfaces
-
7/29/2019 Enterprise Java B19
18/19
ServiceInterface
businessService()
businessService()
RemoteInterface LocalInterface
BeanImplementation
-
7/29/2019 Enterprise Java B19
19/19
Try it..