enterprise java bean

31
EJB

Transcript of enterprise java bean

Page 1: enterprise java bean

EJB

Page 2: enterprise java bean

Enterprise Java Bean

• EJB (Enterprise Java Bean) is used to develop scalable, robust and secured enterprise applications in java.

• Unlike RMI, middleware services such as security, transaction management etc. are provided by EJB Container to all EJB applications.

• The current version of EJB is EJB 3.2. The development of EJB 3 is faster than EJB 2 because of simplicity and annotations such as @EJB, @Stateless, @Stateful, @ModelDriven, @PreDestroy, @PostConstruct etc.

Page 3: enterprise java bean

What is EJB

• EJB is an acronym for enterprise java bean. It is a specification provided by Sun Microsystems to develop secured, robust and scalable distributed applications.

• To get information about distributed applications, visit RMI Tutorial first.

• To run EJB application, you need an application server (EJB Container) such as Jboss, Glassfish, Weblogic, Websphere etc.

Page 4: enterprise java bean

What is EJB

• It performs:• life cycle management,• security,• transaction management, and• object pooling.• EJB application is deployed on the server, so it is called

server side component also.• EJB is like COM (Component Object Model) provided by

Microsoft. But, it is different from Java Bean, RMI and Web Services.

Page 5: enterprise java bean

When use Enterprise Java Bean?

• Application needs Remote Access. In other words, it is distributed.

• Application needs to be scalable. EJB applications supports load balancing, clustering and fail-over.

• Application needs encapsulated business logic. EJB application is separated from presentation and persistent layer.

Page 6: enterprise java bean

Types of Enterprise Java Bean• There are 3 types of enterprise bean in java.• Session Bean• Session bean contains business logic that can be invoked by

local, remote or webservice client.• Message Driven Bean• Like Session Bean, it contains the business logic but it is

invoked by passing message.• Entity Bean• It encapsulates the state that can be persisted in the database.

It is deprecated. Now, it is replaced with JPA (Java Persistent API).

Page 7: enterprise java bean

Difference between RMI and EJBRMI EJB

In RMI, middleware services such as security, transaction management, object pooling etc. need to be done by the java programmer.

In EJB, middleware services are provided by EJB Container automatically.

RMI is not a server-side component. It is not required to be deployed on the server.

EJB is a server-side component, it is required to be deployed on the server.

RMI is built on the top of socket programming.

EJB technology is built on the top of RMI.

Page 8: enterprise java bean

EJB and Webservice

• In EJB, bean component and bean client both must be written in java language.

• If bean client need to be written in other language such as .net, php etc, we need to go with webservices (SOAP or REST). So EJB with web service will be better option.

Page 9: enterprise java bean

Disadvantages of EJB

• Requires application server• Requires only java client. For other language

client, you need to go for webservice.• Complex to understand and develop ejb

applications.

Page 10: enterprise java bean

Session Bean

• Session bean encapsulates business logic only, it can be invoked by local, remote and webservice client.

• It can be used for calculations, database access etc.

• The life cycle of session bean is maintained by the application server (EJB Container).

Page 11: enterprise java bean

Types of Session Bean

• There are 3 types of session bean.• 1) Stateless Session Bean: It doesn't maintain

state of a client between multiple method calls.• 2) Stateful Session Bean: It maintains state of a

client across multiple requests.• 3) Singleton Session Bean: One instance per

application, it is shared between clients and supports concurrent access.

Page 12: enterprise java bean

Stateless Session Bean• Stateless Session bean is a business object that

represents business logic only. It doesn't have state (data).• In other words, conversational state between multiple

method calls is not maintained by the container in case of stateless session bean.

• The stateless bean objects are pooled by the EJB container to service the request on demand.

• It can be accessed by one client at a time. In case of concurrent access, EJB container routes each request to different instance.

Page 13: enterprise java bean

Annotations used in Stateless Session Bean

• There are 3 important annotations used in stateless session bean:

• @Stateless• @PostConstruct• @PreDestroy

Page 14: enterprise java bean

Life cycle of Stateless Session Bean• There is only two states of stateless session bean: does not

exist and ready. It is explained by the figure given below.

Page 15: enterprise java bean

Life cycle of Stateless Session Bean

EJB Container creates and maintains a pool of session bean first. It injects the dependency if 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.

Page 16: enterprise java bean

Stateful Session Bean

• Stateful Session bean is a business object that represents business logic like stateless session bean. But, it maintains state (data).

• In other words, conversational state between multiple method calls is maintained by the container in stateful session bean.

Page 17: enterprise java bean

Annotations used in Stateful Session Bean

There are 5 important annotations used in stateful session bean:• @Stateful• @PostConstruct• @PreDestroy• @PrePassivate• @PostActivate

Page 18: enterprise java bean

JMS

• JMS (Java Message Service) is an API that provides the facility to create, send and read messages. It provides loosely coupled, reliable and asynchronous communication.

• JMS is also known as a messaging service.

Page 19: enterprise java bean

Understanding Messaging

• Messaging is a technique to communicate applications or software components.

• JMS is mainly used to send and receive message from one application to another.

Page 20: enterprise java bean

Requirement of JMS

• Generally, user sends message to application. But, if we want to send message from one application to another, we need to use JMS API.

• Consider a scenario, one application A is running in INDIA and another application B is running in USA. To send message from A application to B, we need to use JMS.

Page 21: enterprise java bean

Advantage of JMS

• 1) Asynchronous: To receive the message, client is not required to send request. Message will arrive automatically to the client.

• 2) Reliable: It provides assurance that message is delivered.

Page 22: enterprise java bean

Messaging Domains

• There are two types of messaging domains in JMS.

• Point-to-Point Messaging Domain• Publisher/Subscriber Messaging Domain

Page 23: enterprise java bean

Point-to-Point (PTP) Messaging Domain

• In PTP model, one message is delivered to one receiver only. Here, Queue is used as a message oriented middleware (MOM).

• The Queue is responsible to hold the message until receiver is ready.

• In PTP model, there is no timing dependency between sender and receiver.

Page 24: enterprise java bean
Page 25: enterprise java bean

Publisher/Subscriber (Pub/Sub) Messaging Domain

• In Pub/Sub model, one message is delivered to all the subscribers. It is like broadcasting. Here, Topic is used as a message oriented middleware that is responsible to hold and deliver messages.

• In PTP model, there is timing dependency between publisher and subscriber.

Page 26: enterprise java bean
Page 27: enterprise java bean

JMS Programming Model

Page 28: enterprise java bean

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.

MDB asynchronously receives the message and processes it.

A message driven bean receives message from queue or topic, so you must have the knowledge of JMS API.

A message driven bean is like stateless session bean that encapsulates the business logic and doesn't maintain state.

Page 29: enterprise java bean
Page 30: enterprise java bean

Entity Bean in EJB 3.x• Entity bean represents the persistent data stored in the

database. It is a server-side component.• In EJB 2.x, there was two types of entity beans: bean

managed persistence (BMP) and container managed persistence (CMP).

• Since EJB 3.x, it is deprecated and replaced by JPA (Java Persistence API) that is covered in the hibernate tutorial.

• In hibernate tutorial, there are given hibernate with annotation examples where we are using JPA annotations. The JPA with Hibernate is widely used today.

Page 31: enterprise java bean