Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that.

Similar presentations


Presentation on theme: "1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that."— Presentation transcript:

1 1 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. 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. 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.

2 2 Architectural Overview Java Persistence also makes it fairly easy for developers to write objects that can be persisted to a relational database. 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. Writing a persistent entity bean can be as easy as using a few strategically placed annotations with little to no information about the database.

3 3 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. 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. Unlike other EJB types, entities can be allocated, serialized, and sent across the network like any other POJO.

4 4 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: 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:

5 5 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. 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.

6 6 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. 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.

7 7 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. 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. In Java Persistence, entity beans are not components like their EJB 2.1 counterparts are.

8 8 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. 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.

9 9 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. 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.

10 10 The Entity Bean Class To define an entity bean, all we'll need to do is define the bean class and annotate it: 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;

11 11 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 } }

12 12 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) 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)

13 13 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. 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. 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. 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.

14 14 This information is stored in an XML deployment descriptor named persistence.xml and is a required artifact. 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. 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. XML Deployment Descriptors and JAR Files

15 15 The Enterprise Bean Component Enterprise JavaBeans server-side components come in two fundamentally different types: session beans and message- driven beans. 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. 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. Message-driven beans process messages asynchronously from systems like the JMS, legacy systems, and web services.

16 16 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. 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 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. The session and message-driven beans do not represent things in the database.

17 17 Classes and Interfaces A good way to understand the design of enterprise beans is to look at how you'd go about implementing one. 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: To implement session and message- driven enterprise beans, you need to define their component interfaces, and a bean class:

18 18 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 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. 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.

19 19 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. 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.

20 20 Local interface It allows beans to interact without the overhead of a distributed object protocol, which improves their performance. 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. 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.

21 21 Endpoint interface The endpoint interface defines business methods that can be accessed from applications outside the EJB container via SOAP. 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 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. The endpoint interface is a plain Java interface that is annotated with the @javax.jws.WebService annotation.

22 22 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. 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.

23 23 Bean class The session bean class contains business logic and must have at least one remote, local, or endpoint interface. 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. 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. 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. The EJB container usually determines whether a session bean is remote and/or local by the interfaces it implements.

24 24 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 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.

25 25 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. 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. 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.

26 26 The remote interface We'll define the remote interface for the Calculator bean using the CalculatorRemote interface, which defines arithmetic operations. 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: Remote interfaces are denoted by the @javax.ejb.Remote annotation:

27 27 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. 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. This is a big change in the EJB 3.0 specification compared to older versions.

28 28 The bean class Now let's look at an actual stateless session 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: Here's the code for the CalculatorBean class; it's a sparse implementation, but it shows how the pieces fit together:

29 29 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; }

30 30 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. 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. 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.

31 31 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. 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. 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.

32 32 Message-Driven Beans Java applications or legacy systems that need to access EJB applications can send messages to message-driven beans via JMS. 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. These beans process those messages and perform the required tasks using other entity and session beans.

33 33 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. 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. However, support for JMS-based message-driven beans (JMS-MDBs) in EJB 3.0 is mandatory.

34 34 Stateful and statelessl session beans Session beans can be either stateful or stateless. Session beans can be either stateful or stateless. Stateful session beans maintain conversational state when used by a client. 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. 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.

35 35 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. 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. Stateless session beans do not maintain any conversational state. Each method is completely independent and uses only data passed in its parameters.

36 36 The Bean-Container Contract The environment that surrounds the beans on the EJB server is often called the container. 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. 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. It manages the EJB objects and helps these constructs manage bean resources and provide services such as transactions, security, concurrency, and naming at runtime.

37 37 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. 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.


Download ppt "1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that."

Similar presentations


Ads by Google