Friday, February 23, 2018

Enterprise Beans

Enterprise beans run in the EJB container:

  • the EJB container is a runtime environment within a Java application server
  • the EJB container provides system-level services to enterprise beans such as transactions and security

Content:

  1. What Is an Enterprise Bean?
  2. What Is a Session Bean?
  3. What Is a Message-Driven Bean?

1 What Is an Enterprise Bean?

An enterprise bean is a server-side component that contains the application's business logic

  • for example, in an auction application, the AuctionManager enterprise bean implements the business logic in the methods named createAuction, placeBid, closeAuction.
1.1 Benefits of Enterprise Beans

Enterprise beans simplify the development of large applications:

  • the EJB container, not the developer, is responsible for system level services such as transaction management and security authorization
  • the beans not the clients contain the business logic, the clients only contain the presentation. As a consequence, application clients are thinner
  • enterprise beans are portable, they are portable accross any compliant Java EE server and developers can build new applications from existing beans
1.2 When to Use Enterprise Beans

You should consider using enterprise beans if your application has any of the following requirements:

  • the application must be scalable to support more users and you need to distribute application's components to multiple machines
  • transactions must ensure data consistency
  • application will be accessed by a variety of clients
1.3 Types of Enterprise Beans

There are two types of enterprise beans, which fulfill different purposes:

  • Session: a session bean performs a task for a client
  • Message-driven: a message-driven bean acts as a listener for messages

2 What Is a Session Bean?

A session bean contains business logic that can be invoked by a client on local, remote or web service views

  • the client accesses an application deployed on a server by invoking the session bean's methods
  • the session bean performs the work for its client, hiding the complexity of execution
  • a session bean is not persistent: its data is not saved to database
2.1 Types of Session Beans

There are three types of session beans: stateful, stateless and singleton.

2.1.1 Stateful Session Beans

When a client and a java server start a session of work the server use a stateful session bean to represent the interaction with the client: this state is called a conversational state.

The values of the instance variables of an object constitute the state of an object: the instance variables of a stateful session bean represents the state of a unique client-server session.

A session bean is similar to an interactive session:

  • a session bean can have only one client and is not shared among many clients
  • when the client terminates, its session bean is no longer associated with the client

When the client terminates, the java server does not retain the state of the client-bean session any longer: the state is said to be transient.

2.1.2 Stateless Session Beans

A stateless session bean does not maintain the state of the conversation with its client.

  • when a client invokes a method of a stateless bean, the instance variables of the bean may contain a state relative to the client, but only for the duration of the method's invocation.
  • after the client's call terminates, the client's state should not be retained. Anyway, stateless beans are pooled, instance variables are not reset to default values and the state is maintained until the next invocation.
  • except during method invocation, all instances of a stateless bean are considered equivalent, the EJB container may assign an instance to any client. The state of a stateless session bean should be employed for all clients

Stateless session beans offer better scalability:

  • an application requires fewer stateless session beans than stateful session beans to support the same number of clients

A staless session bean can implement a web service but a stateful session bean cannot.

2.1.3 Singleton Session Beans

A singleton session bean is a instatiated only one per application and exists for the entire lifecycle of the application

  • a singleton session bean is suitable for situations where a single enterprise bean instance is shared across multiple clients that concurrently access the enterprise bean.

Singleton session beans provide similar functionality to stateless session beans.

  • singleton session beans differ from stateless session beans, because there is only one singleton session bean per application instead of a pool of stateless session beans.
  • both singleton and stateless session beans can implement web service endpoints.

Singleton session beans may maintain their state across client invocations but are not required to maintain their state across server shutdowns.

Applications using a singleton session bean may indicate that the bean should be instantiated on application startup.

2.2 When to Use Session Beans
session bean type usage

stateful

  • the state of the enterprise bean represents the interaction between client and server
  • the enterprise bean works as an intermediary part between the client and other application components

stateless

  • the bean does not have to maintain a state for the client
  • the bean implements a web service

singleton

  • the bean keeps a state to be shared across the application
  • the bean implements a web service

3 What Is a Message-Driven Bean?

a message-driven bean is an enterprise bean which allows Java applications to process messages asynchronously:

  • an message-driven bean usually works as a JMS message listener
  • JMS messages are sent by another Java component or by a JMS application
  • message-driven beans can process JMS messages or other types of messages
3.1 What Makes Message-Driven Beans Different from Session Beans?

One difference between message-driven and session beans is that clients do not access message-driven beans through interfaces:

  • unlike session beans, a message driven bean has only a bean class.

A message-driven bean resembles a stateless session bean in many respects

  • a message-driven bean does not retain a state for a client
  • all instances of a message-driven beans are equivalent
  • a single message-driven bean can process messages from multiple clients

How to invoke a message-driven bean

  • clients components invoke methods of a message-driven bean by sending messages to a JMS message destination with a specific name
  • the message-driven bean class implements the MessageListener interface for the message destination with that specific name

message-driven bean characteristics:

  • they execute upon receipt of a client message
  • they are invoked asynchronously
  • they are stateless and short-lived
  • they can be transaction-aware and can access database

What does it happen when a message arrive?

  • the container calls the onMessage method of the message-driven bean to process the message

Message-driven bean and transactions

  • a message-driven bean can receive a message within a transaction context
  • all the operations performed within the onMessage method are part of a single transaction
3.2 Synchronous and Asynchronous JMS Messages

Message-driven beans allow to consume JMS messages asynchronously, while session beans allow to send and receive JMS messages synchronously.

  • in general, JMS messages should be used asynchronously
  • synchronous JMS receives in session bean components may tie up server resources: use message-driven beans instead

No comments :

Post a Comment