EJB overview 3 sensible defaults,and IPA.all of which you will be learning about in this book. Although the number of EJB services is significant,you'll find them very intui- tive.For the most part,EJB 3 has a practical outlook and doesn't demand that eena getting the job done and go home at the end of the day knowing you accom- plished something. Integrated solution stack EJB 3 offers a complete stack of server solutions,including persistence,messag- ing,lightweight scheduling,remoting,web services,dependency injection (DI). and interceptors.This means that you won't have to spend a lot of time looking for third-party tools to integrate into your application.In addition,EJB 3 provides seamless integration with other Java EE technologies,such as JDBC,JavaMail, Java Transaction API JTA (TA),Java Messaging Service (JMS),Java Authentication and Authorization Service (JAAS),Java Naming and Directory Interface (JNDI), Java Remote Method Invocation(RMI),and so on.EJB is also guaranteed to seam lessly integrate with presentation-tier technologies like JavaServer Pages (JSP), servlets,JavaServer Faces(JSF),and Swing. Open Java EE standard EJB is a critical part of the Java EE standard.This is an extremely important con- cept to grasp if you are to adoptEB3.has an open,public API specifica- tion,which organizations are encouraged to use to create a container or persistence provider implementation.The EJB 3 standard is developed by the Java Community Process (JCP),consisting of a nonexclusive group of individuals driving the Java standard.The open standard leads to broader vendor support for EJB 3,which means you don't have to depend on a proprietary solution. Broad vendor support EJB is supported by a large and diverse variety of independent organizations This includes the technology world's largest,most respected,and most financially strong names,such as Oracle and IBM,as well as passionate and energetic open source groups like JBoss and Geronimo. Wide vendor support translates to three important advantages for you.First, you are not at the mercy of the ups and downs of a particular company or group of people.Second,a lot of people have concrete long-term interests to keep the technology as competitive as possible.You can essentially count on being able to
EJB overview 13 sensible defaults, and JPA, all of which you will be learning about in this book. Although the number of EJB services is significant, you’ll find them very intuitive. For the most part, EJB 3 has a practical outlook and doesn’t demand that you understand the theoretical intricacies. In fact, most EJB services are designed to give you a break from this mode of thinking so you can focus on getting the job done and go home at the end of the day knowing you accomplished something. Integrated solution stack EJB 3 offers a complete stack of server solutions, including persistence, messaging, lightweight scheduling, remoting, web services, dependency injection (DI), and interceptors. This means that you won’t have to spend a lot of time looking for third-party tools to integrate into your application. In addition, EJB 3 provides seamless integration with other Java EE technologies, such as JDBC, JavaMail, Java Transaction API JTA (JTA), Java Messaging Service (JMS), Java Authentication and Authorization Service (JAAS), Java Naming and Directory Interface (JNDI), Java Remote Method Invocation (RMI), and so on. EJB is also guaranteed to seamlessly integrate with presentation-tier technologies like JavaServer Pages (JSP), servlets, JavaServer Faces (JSF), and Swing. Open Java EE standard EJB is a critical part of the Java EE standard. This is an extremely important concept to grasp if you are to adopt EJB 3. EJB 3 has an open, public API specification, which organizations are encouraged to use to create a container or persistence provider implementation. The EJB 3 standard is developed by the Java Community Process (JCP), consisting of a nonexclusive group of individuals driving the Java standard. The open standard leads to broader vendor support for EJB 3, which means you don’t have to depend on a proprietary solution. Broad vendor support EJB is supported by a large and diverse variety of independent organizations. This includes the technology world’s largest, most respected, and most financially strong names, such as Oracle and IBM, as well as passionate and energetic open source groups like JBoss and Geronimo. Wide vendor support translates to three important advantages for you. First, you are not at the mercy of the ups and downs of a particular company or group of people. Second, a lot of people have concrete long-term interests to keep the technology as competitive as possible. You can essentially count on being able to
14 CHAPTER 1 What's what in elb 3 take advantage of the best-of-breed technologies both in and outside the Java world in a competitive timeframe.Third.vendors have historically competed against one another by providing value-added nonstandard features.All of these factors help keep EJB on the track of continuous healthy evolution. Stable,high-quality code base Although EJB 3 is a groundbreaking step,most application server implementa- tions will still benefit from a relatively stable code base that has lived through some of the most demanding enterprise environments over a prolonged period of time.Most persistence provider solutions like JDO,Hibernate,and TopLink are also stable products that are being used in many mission-critical production environments.This means that although EJB 3 is very new,you can expect stable implementations relatively quickly.Also,because of the very nature of standards- based development,the quality of EJB 3 container implementations is generally not taken lightly by vendors.To some degree,this helps ensure a healthy level of inherent implementation quality. Clustering,load balancing,and failover Features historically added by most application server vendors are robust support for clustering,load balancing,and fail ver.EJB application servers have a proven track record of supporting some of the largest high-performance computing (HPC)-enabled server farm environments.More importantly,you can leverage such support with no changes to code,no third-party tool integration,and rela tively simple configuration (beyond the inherent work in setting up a hardware cluster).This means that you can rely on hardware clustering to scale up your application with EJB 3 if you need to. EJB 3 is a compelling option for building enterprise applications.In the fol- lowing sections,we explain more about EJB types and how to use them.We also discuss containers and persistence providers and explore the services they pro- vide.By the time you finish reading sections 1.2 and 1.3,you'll have a good idea of what EJBs are and where they run,and what services they offer.So let's get started! 1.2 Understanding EJB types If you're like most developers,you always have a tight deadline to meet.Most of us try to beg.borrow.or steal reusable code to make our lives easier.Gone are those days when developers had the luxury to create their own infrastructure
14 CHAPTER 1 What’s what in EJB 3 take advantage of the best-of-breed technologies both in and outside the Java world in a competitive timeframe. Third, vendors have historically competed against one another by providing value-added nonstandard features. All of these factors help keep EJB on the track of continuous healthy evolution. Stable, high-quality code base Although EJB 3 is a groundbreaking step, most application server implementations will still benefit from a relatively stable code base that has lived through some of the most demanding enterprise environments over a prolonged period of time. Most persistence provider solutions like JDO, Hibernate, and TopLink are also stable products that are being used in many mission-critical production environments. This means that although EJB 3 is very new, you can expect stable implementations relatively quickly. Also, because of the very nature of standardsbased development, the quality of EJB 3 container implementations is generally not taken lightly by vendors. To some degree, this helps ensure a healthy level of inherent implementation quality. Clustering, load balancing, and failover Features historically added by most application server vendors are robust support for clustering, load balancing, and failover. EJB application servers have a proven track record of supporting some of the largest high-performance computing (HPC)-enabled server farm environments. More importantly, you can leverage such support with no changes to code, no third-party tool integration, and relatively simple configuration (beyond the inherent work in setting up a hardware cluster). This means that you can rely on hardware clustering to scale up your application with EJB 3 if you need to. EJB 3 is a compelling option for building enterprise applications. In the following sections, we explain more about EJB types and how to use them. We also discuss containers and persistence providers and explore the services they provide. By the time you finish reading sections 1.2 and 1.3, you’ll have a good idea of what EJBs are and where they run, and what services they offer. So let’s get started! 1.2 Understanding EJB types If you’re like most developers, you always have a tight deadline to meet. Most of us try to beg, borrow, or steal reusable code to make our lives easier. Gone are those days when developers had the luxury to create their own infrastructure
Understanding EJB types when building a commercial application.While several commercial and open source frameworks are available that can simplify application development,EjB is a compelling framework that has a lot to offer. We expect that by now you're getting excited about EJB and you're eager to learn more.So let's jump right in and see how you can use EJB as a framework to build your business logic and persistence tier of your applications,starting with the beans. In EJB-speak,a component is a"bean."If your manager doesn't find the Java- "coffee bean"play on words cute either,blame Sun's marketing department.Hey, at least we get to hear people in suits use the words"enterprise 'and"bean”in close sequence as if it were perfectly normal... As we mentioned,EJB classifies beans into three types,based on what they are used for: ■Session beans Message-driven beans Entities Each bean type serves a purpose and can use a specific subset of EJB services.The real purpose of bean types is to safeguard against overloading them with services that cro wires.This is akin to making sure the accountant in the horn-rimmed glasses doesn't get too curious about what happens when you touch both ends of a car battery terminal at the same time.Bean classification also helps you under- stand and organize an application in a sensible way:for example,bean types help you develop applications based on a layered architecture. As we've briefly mentioned,session beans and message-driven beans(MDBs) are used to build business logic,and they live in the container,which manages these beans and provides services to them.Entities are used to model the persis- tence part of an application.Like the container,it is the persistence provider that manages entities.A persistence provider is pluggable within the container and is abstracted behind the lava Persistence API(IPA).This organization of the EJB 3 API is shown in figure 1.6. We'll discuss the container and the persistence provider in section 1.3.For the time being,all you need to know is that these are separate parts of an EJB imple- mentation,each of which provide support for different EJB component types. Let's start digging a little deeper into the various EJB component types,start- ing with session beans
Understanding EJB types 15 when building a commercial application. While several commercial and open source frameworks are available that can simplify application development, EJB is a compelling framework that has a lot to offer. We expect that by now you’re getting excited about EJB and you’re eager to learn more. So let’s jump right in and see how you can use EJB as a framework to build your business logic and persistence tier of your applications, starting with the beans. In EJB-speak, a component is a “bean.” If your manager doesn’t find the Java- “coffee bean” play on words cute either, blame Sun’s marketing department. Hey, at least we get to hear people in suits use the words “enterprise” and “bean” in close sequence as if it were perfectly normal… As we mentioned, EJB classifies beans into three types, based on what they are used for: ■ Session beans ■ Message-driven beans ■ Entities Each bean type serves a purpose and can use a specific subset of EJB services. The real purpose of bean types is to safeguard against overloading them with services that cross wires. This is akin to making sure the accountant in the horn-rimmed glasses doesn’t get too curious about what happens when you touch both ends of a car battery terminal at the same time. Bean classification also helps you understand and organize an application in a sensible way; for example, bean types help you develop applications based on a layered architecture. As we’ve briefly mentioned, session beans and message-driven beans (MDBs) are used to build business logic, and they live in the container, which manages these beans and provides services to them. Entities are used to model the persistence part of an application. Like the container, it is the persistence provider that manages entities. A persistence provider is pluggable within the container and is abstracted behind the Java Persistence API (JPA). This organization of the EJB 3 API is shown in figure 1.6. We’ll discuss the container and the persistence provider in section 1.3. For the time being, all you need to know is that these are separate parts of an EJB implementation, each of which provide support for different EJB component types. Let’s start digging a little deeper into the various EJB component types, starting with session beans
16 I CHAPTER 1 What's what in elb 3 Meoegn EJB 3 Session bean JPA Entity a ence obl int s are ent provider through the 1.2.1 Session beans A session bean is invoked by a client for the purpose of performing a specific busi- ness operation,such as checking the credit history for a customer.The name session implies that a bean instance is available for the duration of a"unit of work"and does not survive a server crash or shutdown.A session bean can model any appli- cation logic functionality.There are two types of session beans:stateful and stateless. A stateful session bean automatically saves bean state between client invoca tions without your having to write any additional code.A typical example of a state-aware process is the shopping cart for a web merchant like Amazon.In con- trast,stateless session beans do not maintain any state and model application ser- vices that can be completed in a single client invocation.You could build stateless session beans for implementing business processes such as charging a credit card or checking customer credit history. A session bean can be invoked either locally or remotely using Java RMI.A stateless session bean can be exposed as a web service 1.2.2 Message-driven beans Like session beans,MDBs process business logic.However,MDBs are different in one important way:clients never invoke MDB methods directly.Instead,MDBs are triggered by messages sent to a messaging server,which enables sending asyn- chronous messages between system comp onents.Some typical examples of mes- saging servers are IBM WebSphere MQ,SonicMQ,Oracle Advanced Queueing, and TIBCO.MDBs are typically used for robust system integration or asynchro- nous processing.An example of messaging is sending an inventory-restocking
16 CHAPTER 1 What’s what in EJB 3 1.2.1 Session beans A session bean is invoked by a client for the purpose of performing a specific business operation, such as checking the credit history for a customer. The name session implies that a bean instance is available for the duration of a “unit of work” and does not survive a server crash or shutdown. A session bean can model any application logic functionality. There are two types of session beans: stateful and stateless. A stateful session bean automatically saves bean state between client invocations without your having to write any additional code. A typical example of a state-aware process is the shopping cart for a web merchant like Amazon. In contrast, stateless session beans do not maintain any state and model application services that can be completed in a single client invocation. You could build stateless session beans for implementing business processes such as charging a credit card or checking customer credit history. A session bean can be invoked either locally or remotely using Java RMI. A stateless session bean can be exposed as a web service. 1.2.2 Message-driven beans Like session beans, MDBs process business logic. However, MDBs are different in one important way: clients never invoke MDB methods directly. Instead, MDBs are triggered by messages sent to a messaging server, which enables sending asynchronous messages between system components. Some typical examples of messaging servers are IBM WebSphere MQ, SonicMQ, Oracle Advanced Queueing, and TIBCO. MDBs are typically used for robust system integration or asynchronous processing. An example of messaging is sending an inventory-restocking Figure 1.6 Overall organization of the EJB 3 API. The Java persistence API is completely separable from the EJB 3 container. The business logic processing is carried out by through two component types: session beans and message-driven beans. Both components are managed by the container. Persistence objects are called entities, which are managed by the persistent provider through the EntityManager interface
Understanding EJB types request from an automated retail system to a supply-chain management system. Don't wo orry too much about messaging right now;we'll get to the details later in this book. Next we'll explain the concept of persistence and describe how object-rela- tional frameworks help enable automated persistence. 1.2.3 Entities and the Java Persistence API One of the exciting new features of EJB 3 is the way it handles persistence.We briefly mentioned persistence providers and the JPA earlier,but now let's delve into the details Persistence is the ability to have data contained in Java objects automatically stored into a relational database like Oracle,SQL Server,and DB2.Persistence in EJB 3 is managed by the JPA.It automatically persists the Java objects using a technique called object-relational mapping (ORM).ORM is essentially the process of mapping data held in Java objects to database tables using configuration.It relieves you of the task of writing low-level,boring,and complex JDBC code to persist objects into a database. The frameworks that provide ORM capability to perform automated persis tence are known as ORM frameworks.As the name implies,an ORM framework performs transparent persistence by making use of object-relational mapping metadata that defines how objects are mapped to database tables.ORM is not a new concept and has been around for a while.Oracle TopLink is probably the oldest ORM framework in the market;open source framework JBoss Hibernate popularized ORM concepts among the mainstream developer community. In EJB 3 terms,a persistence provider is essentially an ORM framework that supports the EJB 3 Java Persistence API (JPA).The JPA defines a standard for The creation of ORM configuration metadata for mapping entities to rela- tional tables The Entity Manager API-a standard API for performing CRUD (create read,update,and delete)/persistence operations for entities The Java Persistence Query Language (JPQL),for searching and retrieving persisted application data Since JPA standardizes ORM frameworks for the Java platform,you can plug in ORM products like JBoss Hibernate,Oracle TopLink,or BEA Kodo as the under- lying JPA"persistence provider"for your application
Understanding EJB types 17 request from an automated retail system to a supply-chain management system. Don’t worry too much about messaging right now; we’ll get to the details later in this book. Next we’ll explain the concept of persistence and describe how object-relational frameworks help enable automated persistence. 1.2.3 Entities and the Java Persistence API One of the exciting new features of EJB 3 is the way it handles persistence. We briefly mentioned persistence providers and the JPA earlier, but now let’s delve into the details. Persistence is the ability to have data contained in Java objects automatically stored into a relational database like Oracle, SQL Server, and DB2. Persistence in EJB 3 is managed by the JPA. It automatically persists the Java objects using a technique called object-relational mapping (ORM). ORM is essentially the process of mapping data held in Java objects to database tables using configuration. It relieves you of the task of writing low-level, boring, and complex JDBC code to persist objects into a database. The frameworks that provide ORM capability to perform automated persistence are known as ORM frameworks. As the name implies, an ORM framework performs transparent persistence by making use of object-relational mapping metadata that defines how objects are mapped to database tables. ORM is not a new concept and has been around for a while. Oracle TopLink is probably the oldest ORM framework in the market; open source framework JBoss Hibernate popularized ORM concepts among the mainstream developer community. In EJB 3 terms, a persistence provider is essentially an ORM framework that supports the EJB 3 Java Persistence API (JPA). The JPA defines a standard for ■ The creation of ORM configuration metadata for mapping entities to relational tables ■ The EntityManager API—a standard API for performing CRUD (create, read, update, and delete)/persistence operations for entities ■ The Java Persistence Query Language (JPQL), for searching and retrieving persisted application data Since JPA standardizes ORM frameworks for the Java platform, you can plug in ORM products like JBoss Hibernate, Oracle TopLink, or BEA Kodo as the underlying JPA “persistence provider” for your application