370 likes | 497 Views
Architectural Overview. For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that often plague three-tier development efforts.
E N D
Architectural Overview • For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that often plague three-tier development efforts. • While EJB makes the process easier for application developers, it also provides EJB server developers with a great deal of flexibility in how they support the EJB specification.
Architectural Overview • Java Persistence also makes it fairly easy for developers to write objects that can be persisted to a relational database. • Writing a persistent entity bean can be as easy as using a few strategically placed annotations with little to no information about the database.
The Entity Bean • Entity beans in the Java Persistence 1.0 specification are available only as plain old Java objects (POJOs), and are mapped to tables in a relational database. • Unlike other EJB types, entities can be allocated, serialized, and sent across the network like any other POJO.
The Entity Bean • To implement an entity bean, you need to define a bean class and decide what field you will use as the identifier (primary key) of that bean class:
Primary key • The primary key is something that provides a pointer into the database. It gives the entity bean class's identity both in memory as an object and in the database as a row in a table. The primary key can be a class or a primitive type.
Bean class • The bean class is a POJO that does not have to implement any interface or even be serializable. It must be tagged with the @javax.persistence.Entity annotation and have at least one field or getter method that is designated as the primary key of the entity bean class in the database.
Bean class • This is usually done with the @javax.persistence.Id annotation. Entities have other annotations available to define a full object-relational database mapping as well. • In Java Persistence, entity beans are not components like their EJB 2.1 counterparts are.
Bean class • Application code works directly with the entity bean classes and does not interact with the entity through a component interface, as you would with an EJB session bean. To interact with entity beans, Java Persistence provides a new service called the EntityManager .
Bean class • All access to an entity goes through this service. It provides a query API and life cycle methods for the entity. No magic. No bytecode manipulation. No special proxies. Just plain Java.
The Entity Bean Class • To define an entity bean, all we'll need to do is define the bean class and annotate it: import javax.persistence.* ; @Entity @Table(name="CABIN") public class Cabin { private int id; private String name; private int deckLevel;
The Entity Bean Class @Id @GeneratedValue @Column(name="CABIN_ID") public int getId( ) { return id; } public void setId(int pk) { this.id = pk; } @Column(name="CABIN_NAME") public String getName( ) { return name; } public void setName(String str) { this.name = str; } @Column(name="CABIN_DECK_LEVEL") public int getDeckLevel( ) { return deckLevel; } public void setDeckLevel(int level) { this.deckLevel = level } }
The Entity Bean Class • Unlike EJB 2.1, in which entity beans were implemented as abstract classes, Java Persistence entity beans are real objects. Java Persistence defines a relatively complete object-to-relational database mapping (ORM)
XML Deployment Descriptors and JAR Files • Once you have written your entity bean classes, you need to be able to deploy them in your environment. Entity beans are grouped into a finite set of classes called a persistence unit . • There is an EntityManager service that manages a persistence unit, and it must be identified so that it can be referenced in application code. • Also, each persistence unit must be associated to a particular database so that the persistence provider knows where, how, and with what kind of database it is interacting.
XML Deployment Descriptors and JAR Files • This information is stored in an XML deployment descriptor named persistence.xml and is a required artifact. • Once you have defined your XML deployment descriptors and entity bean classes, you must package them all in a Java Archive (JAR) file. The JAR file is used as both a library at runtime and a holder for deployment information.
The Enterprise Bean Component • Enterprise JavaBeans server-side components come in two fundamentally different types: session beans and message-driven beans . • Session beans are server-side components that can be accessed using a variety of distributed object protocols. • Message-driven beans process messages asynchronously from systems like the JMS, legacy systems, and web services.
The Enterprise Bean Component • All EJB servers must at least support a JMS-based message-driven bean, but they may also support other types of message-driven beans. • The activity that a session or message-driven bean represents is fundamentally transient: you start making a reservation, you do a bunch of work, and then it's finished. • The session and message-driven beans do not represent things in the database.
Classes and Interfaces • A good way to understand the design of enterprise beans is to look at how you'd go about implementing one. • To implement session and message-driven enterprise beans, you need to define their component interfaces, and a bean class:
Remote interface • The remote interface defines a session bean's business methods, which can be accessed from applications outside the EJB container: the business methods a bean presents to the outside world to do its work. • The remote interface is a plain Java interface. It is tagged with the @javax.ejb.Remote annotation to identify that it is a remote interface.
Local interface • The local interface defines a session bean's business methods that can be used by other beans in the same EJB container: the business methods a bean presents to other beans running in the same JVM.
Local interface • It allows beans to interact without the overhead of a distributed object protocol, which improves their performance. • The local interface is a plain Java interface. It is tagged with the @javax.ejb.Local annotation to identify that it is a local interface.
Endpoint interface • The endpoint interface defines business methods that can be accessed from applications outside the EJB container via SOAP. • The endpoint interface is based on Java API for XML-RPC (JAX-RPC) and is designed to adhere to the SOAP and WSDL standards. • The endpoint interface is a plain Java interface that is annotated with the @javax.jws.WebService annotation.
Message interface • Message-driven beans implement the message interface, which defines the methods by which messaging systems, such as the JMS, can deliver messages to the bean.
Bean class • The session bean class contains business logic and must have at least one remote, local, or endpoint interface. • It usually implements these interfaces, but it is not required to. • A bean class may also have more than one interface of a given type. • The EJB container usually determines whether a session bean is remote and/or local by the interfaces it implements.
Bean class • The session bean class must also be tagged with the @javax.ejb.Stateful or @javax.ejb.Stateless annotation so that the EJB container knows what session bean type it is.
The remote interface • Having introduced the machinery, let's look at how to build a very simple stateless session bean with a remote component interface. • Now we will examine the Calculator EJB, a session bean that exposes basic calculator functions as a service. Let's start with its remote interface.
The remote interface • We'll define the remote interface for the Calculator bean using the CalculatorRemote interface, which defines arithmetic operations. • Remote interfaces are denoted by the @javax.ejb.Remote annotation:
The remote interface import javax.ejb.Remote; @Remote public interface CalculatorRemote { public int add(int x, int y); public int subtract(int x, int y); } • You'll notice that even though this is the remote interface of the EJB, there is no reference to Java RMI interfaces or APIs. • This is a big change in the EJB 3.0 specification compared to older versions.
The bean class • Now let's look at an actual stateless session bean class. • Here's the code for the CalculatorBean class; it's a sparse implementation, but it shows how the pieces fit together:
The bean class import javax.ejb.*; @Stateless public class CalculatorBean implements CalculatorRemote { public int add(int x, int y) { return x + y; } public int subtract(int x, int y) { return x - y; } }
The bean class • The CalculatorBean class is required to implement at least one remote or local interface and to be annotated with @javax.ejb.Stateless . • Those familiar with earlier EJB specifications will notice that session beans do not have to implement an EJB-specific interface or any of the callback notifications.
Message-Driven Beans • Message-driven beans also have a bean class that implements a message interface; they don't implement remote, local, or endpoint interfaces. • This bean class is annotated using the @javax.ejb.MessageDriven annotation. Message-driven beans are integration points for other applications interested in working with EJB applications.
Message-Driven Beans • Java applications or legacy systems that need to access EJB applications can send messages to message-driven beans via JMS. • These beans process those messages and perform the required tasks using other entity and session beans.
Message-Driven Beans • EJB 3.0 is not limited to JMS-based message-driven beans: message-driven beans can support any messaging system that implements the correct JCA 1.5 contracts. • However, support for JMS-based message-driven beans (JMS-MDBs) in EJB 3.0 is mandatory.
Stateful and statelessl session beans • Session beans can be either stateful or stateless. • Stateful session beans maintain conversational state when used by a client. • Conversational state is not written to a database; it's information that is kept in memory while a client carries on a conversation with an enterprise bean, and it is lost when the conversation ends or the EJB container crashes.
Stateful and statelessl session beans • Stateful session beans are not shared among clients; they are dedicated to the same client for the life of the enterprise bean. • Stateless session beans do not maintain any conversational state. Each method is completely independent and uses only data passed in its parameters.
The Bean-Container Contract • The environment that surrounds the beans on the EJB server is often called the container. • The container is more a concept than a physical construct. It acts as an intermediary between the bean and the EJB server. • It manages the EJB objects and helps these constructs manage bean resources and provide services such as transactions, security, concurrency, and naming at runtime.
The Bean-Container Contract • The distinction between the container and the server is not clearly defined, but the EJB specification defines the component model in terms of the container's responsibilities, so we will follow that convention here.