Showing posts with label ejb. Show all posts
Showing posts with label ejb. Show all posts

A Beginner's Guide to EJB

The Java EE specifications defines a standard mechanism for Java EE applications, which is known as Java EE platform. The platform uses a component based approach to provide a multitiered, distributed, transactional model for enterprise applications. A component is a unit of functionality that is assembled, along with any required resources, and it can communicate with another component inside the Java EE environment or in a networked, multi-machine, distributed environment.

In Simple word Enterprise Java Bean (EJB) is a component that is managed by EJB Container inside Application Server like Websphere, JBoss, Weblogic etc and can be invoked by another component on the same  machine or, more typically, somewhere on the network.

These components come in three varieties, each with its own defined role and life cycle:

Session beans - These may be either stateful or stateless, and are primarily used to encapsulate business logic, carry out tasks on behalf of a client, and act as controllers or managers for other beans.

Entity beans - Entity beans represent persistent objects or business concepts that exist beyond a specific application's lifetime; they are typically stored in a relational database. Entity beans can be developed using bean-managed persistence,which is implemented by the developer, or container-managed persistence, implemented by the container.

Message-driven beans - Message-driven beans listen asynchronously for Java Message Service (JMS) messages from any client or component and are used for loosely coupled, typically batch-type, processing.

Session bean

Session beans are reusable components that have conversations or sessions with clients, it can be invoked by local, remote or webservice client and primarily used to encapsulate business logic.

There are 3 types of session bean.
  • Stateless Session Bean: It doesn't maintain state of a client between multiple method calls.
  • Stateful Session Bean: It maintains state of a client across multiple requests.
  • Singleton Session Bean: One instance per application, it is shared between clients and supports concurrent access.

Stateless Session Bean

Stateless session beans do not maintain conversational state and remember nothing from previous client interaction, 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. Because of pool, instance variable values are not guaranteed to be same across method calls.

There are only two milestones in a stateless session bean's life cycle: Does Not Exist and Ready. EJB Container creates and maintains a pool of session bean first. It injects the dependency if any, then calls the @PostConstruct method if any. Now actual business logic method is invoked by the client. Then, container calls @PreDestory method if any. Now bean is ready for garbage collection.

Example of Stateless Session Bean

Remote Interface
import javax.ejb.Remote;
@Remote
public interface HotelSessionBeanRemote {
  void bookRoom(Integer roomNo, String custName);
}
Stateless EJB
@Stateless
public class HotelSessionBean implements HotelSessionBeanRemote { 
Map room = new HashMap();
void bookRoom(Integer roomNo, String custName){
 room.put(roomNo, custName);
}

Stateful Session Bean

Stateful session beans are called "stateful" because they maintain a conversational state with the client. In other words, they have state or instance fields that can be initialized and changed by the client with each method invocation. The bean can use the conversational state as it process business methods invoked by the client. 

Example of Stateful Session Bean

Remote Interface
import javax.ejb.Remote;
@Remote
public interface BankSessionBeanRemote {
  void bookRoom(Integer roomNo, String custName);
}
Stateful EJB
@Stateless
public class BankSessionBean implements HotelSessionBeanRemote { 
Map room = new HashMap();
void bookRoom(Integer roomNo, String custName){
 room.put(roomNo, custName);
}
 
 
Read More

Latest EJB Question and Answer

1- What is EJB ?
EJB stands for Enterprise JavaBean and is a widely-adopted server side component architecture for J2EE. It enables rapid development of mission-critical application that are versatile, reusable and portable across middleware while protecting IT investment and preventing vendor lock-in.

2- What is session Facade?
Session Facade is a design pattern to access the Entity bean through local interface than accessing directly. It increases the performance over the network. In this case we call session bean which on turn call entity bean.

3- What is EJB role in J2EE?
EJB technology is the core of J2EE. It enables developers to write reusable and portable server-side business logic for the J2EE platform.

4- What is the difference between EJB and Java beans?
EJB is a specification for J2EE server, not a product; Java beans may be a graphical component in IDE

5- What are the key features of the EJB technology?
1. EJB components are server-side components written entirely in the Java programming language
2. EJB components contain business logic only – no system-level programming & services, such as transactions, security, life-cycle, threading, persistence, etc. are automatically managed for the EJB component by the EJB server.
3. EJB architecture is inherently transactional, distributed, portable multi-tier, scalable and secure.
4. EJB components are fully portable across any EJB server and any OS.
5. EJB architecture is wire-protocol neutral–any protocol can be utilized like IIOP,JRMP, HTTP, DCOM,etc

6- Are enterprise beans allowed to use Thread.sleep()?
Enterprise beans make use of the services provided by the EJB container, such as life-cycle management. To avoid conflicts with these services, enterprise beans are restricted from performing certain operations: Managing or synchronizing threads

7- Is it possible to write two EJB’s that share the same Remote and Home interfaces, and have different bean classes? if so, what are the advantages/disadvantages?
It’s certainly possible. In fact, there is  an example that ships with the Enterprise Application Server of an Account interface with separate implementations for Checking Account and Savings Account, one of which was CMP and one of which was BMP

8- Is it possible to specify multiple JNDI names when deploying an EJB?
No. To achieve this you have to deploy your EJB multiple times each specifying a different JNDI name.

9- Is there any way to force an Entity Bean to store itself to the db? We can’t  wait for the container to update the db, we need to do it NOW! Is it possible?
Specify the transaction attribute of the bean as RequiresNew. Then as per section 11.6.2.4 of the EJB v 1.1 spec EJB container automatically starts a new transaction before the method call. The container also performs the commit protocol before the method result is sent to the client.
Read More