Search This Blog

Showing posts with label EJB. Show all posts
Showing posts with label EJB. Show all posts

Thursday 23 March 2017

Session Bean

Session Bean


session bean encapsulates business logic that can be invoked programmatically by a client over local, remote, or web service client views. The session bean performs work for its client, by executing business tasks inside the server.
A session bean is not persistent.

Types of Session Beans
Session beans are of three types: 
·         Stateful 
·         Stateless 
·         Singleton.

Stateful Session Beans

A stateful session bean is a type of enterprise bean which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables. EJB Container creates a separate stateful session bean to process client's each request. As soon as request scope is over, statelful session bean is destroyed.
The state of an object is the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client/bean session.
A session bean is not shared; it can have only one client. When the client terminates, its session bean terminates and is no longer associated with the client.
The state is retained for the duration of the client/bean session. If the client removes the bean, the session ends and the state disappears.
Stateful session beans are appropriate in any of the following conditions.
·         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.
·         The bean mediates between the client and the other components of the application, presenting a simplified view to the client.
·         Behind the scenes, the bean manages the work flow of several enterprise beans.

Stateless Session Beans

A stateless session bean is a type of enterprise bean which is normally used to do independent operations. A stateless session bean does not have any associated client state, but it may preserve its instance state. EJB Container normally creates a pool of few stateless bean's objects and use these objects to process client's request.
stateless session bean does not maintain a conversational state with the client. 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 is finished, the client-specific state should not be retained.
Because they can support multiple clients, stateless session beans can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.
A stateless session bean can implement a web service, but a stateful session bean cannot.
Stateless session bean are appropriate in following circumstances
·         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.
·         The bean implements a web service.

Singleton Session Beans
singleton session bean is instantiated once per application and exists for the lifecycle of the application. Singleton session beans are designed for circumstances in which a single enterprise bean instance is shared across and concurrently accessed by clients.
Singleton session beans offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application.
Singleton session beans maintain their state between client invocations but are not required to maintain their state across server crashes or shutdowns.
Applications that use a singleton session bean may specify that the singleton should be instantiated upon application startup, which allows the singleton to perform initialization tasks for the application. The singleton may perform cleanup tasks on application shutdown as well, because the singleton will operate throughout the lifecycle of the application.
Singleton session beans are appropriate in the following circumstances.
·         State needs to be shared across the application.
·         A single enterprise bean needs to be accessed by multiple threads concurrently.
·         The application needs an enterprise bean to perform tasks upon application startup and shutdown.
·         The bean implements a web service.


Tuesday 21 March 2017

Enterprise Beans

Enterprise Beans


Enterprise beans are Java EE components that implement Enterprise JavaBeans (EJB) technology. Enterprise beans run in the EJB container. The EJB container provides system-level services, such as transactions and security, to its enterprise beans

What Is an Enterprise Bean?
An enterprise bean is a server-side component that encapsulates the business logic of an application. The business logic is the code that fulfils the purpose of the application.
For Example :
In an ecommerce site the enterprise beans might implement the business logic in methods called checkOut and orderProduct. By invoking these methods, clients can access the services provided by the application.

Benefits of Enterprise Beans
Enterprise beans simplify the development of large, distributed applications.
  • The EJB container provides system-level services to enterprise beans, the bean developer can concentrate on solving business problems. The EJB container, rather than the bean developer, is responsible for system-level services, such as transaction management and security authorization.
  • The beans contain 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.
  • Enterprise beans are portable components, the application assembler can build new applications from existing beans. Provided that they use the standard APIs, these applications can run on any compliant Java EE server.


When to Use Enterprise Beans
  • The application must be scalable.
  • Transactions must ensure data integrity. Enterprise beans support transactions, the mechanisms that manage the concurrent access of shared objects.
  • The application will have a variety of clients. 


There are two types of enterprise beans.
  • Session Beans : Performs a task for a client; optionally, may implement a web service
  • Message-driven Beans : Acts as a listener for a particular messaging type, such as the Java Message Service API