Enterprise Java Bean (EJB)
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 for client application
Benefits of EJB
For several reasons, enterprise beans simplify the development of large, distributed applications. First, because the EJB container provides system-level services to enterprise beans, the bean developer can concentrate on solving business problems. The EJB containerand not the bean developeris responsible for system-level services such as transaction management and security authorization.
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.
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
When to Use EJB
You should consider using enterprise beans if your application has any of the following requirements:
The application must be scalable. To accommodate a growing number of users, you may need to distribute an application's components across multiple machines. 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. With only a few lines of code, remote clients can easily locate enterprise beans. These clients can be thin, various, and numerous.
Type of EJB
Session Bean Message-Driven Bean Entity beans have been replaced by Java Persistence API
What is Session Bean?
A key difference between session bean and other bean types is the scope of their lives. A session bean instance is relatively short-lived object. Session bean instance is created when a client request it, and ends when it finishing process the client request Session bean instances are not share between multiple clients and do not represent data in database Session bean is used for interacting with application client Session bean can implements business logic of an application
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 is finished, the client-specific state should not be retained
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 clientbean session. If the client removes the bean or terminates, the session ends and the state disappears.
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.
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 an email that confirms an online order, etc..
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
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)
Session bean best practices
Choose your bean type carefully. Stateless session beans will be suitable most of the time. Carefully examine whether your application needs stateful session beans, because it comes with a price. If the EJB client lies in the web tier, then using HttpSession may be a better choice than stateful session beans. Carefully examine interface types for session beans. Remote interfaces involve network access and may slow down your applications.
Try it
Session Bean Pattern
Ensures that the common services are supported by both Remote and Local interfaces
ServiceInterface businessService() businessService()
RemoteInterface
LocalInterface
BeanImplementation
Try it..