What's what in EB 3 This chapter covers EJB as component and framework Types of EJBs EJB containers and persistence provider Reasons for choosing EJB 3
3 What’s what in EJB 3 This chapter covers ■ EJB as component and framework ■ Types of EJBs ■ EJB containers and persistence provider ■ Reasons for choosing EJB 3
CHAPTER 1 What's what in elb 3 One day,when God was looking over his creatures,he noticed a boy named Sadhu whose humor and cleverness pleased him.God felt generous that day and granted Sadhu three wishes.Sadhu asked for three reincarnations-one as a lady- bug,one as an elephant,and the last as a cow.Surprised by these wishes,God asked Sadhu to explain himself.The boy replied,"I want to be a ladybug so that everyone in the world will admire me for my beauty and forgive the fact that I do no work.Being an elephant will be fun because I can gobble down enormous amounts of food without being ridiculed.I will like being a cow the best because I will be loved by all and useful to mankind."God was charmed by these answers and allowed Sadhu to live through the three incarnations.He then made Sadhu a morning star for his service to humankind as a cow. EJB too has lived through three incarnations.When it was first released,the industry was dazzled by its innovations.But like the ladybug,EJB I had limited functionality.The second EJB incarnation was just about as heavy as the largest of our beloved pachyderms.The brave souls who could not do without its elephant power had to tame the awesome complexity of EJB 2.And finally in its third incarnation,EJB has become much more useful to the huddled masses,just like the gentle bovine that is sacred for Hindus and respected as a mother whose milk feeds us well. Many people have put in a lot of hard work to make EJB 3 as simple and light- weight as possible without sacrificing enterprise-ready power.EJB components are now little more than plain old Java objects(POJOs)that look a lot like code in a Hello World program.We hope you agree with us as you read through the next chapters that EJB 3 has all the makings of a star. We've strived to keep this book as practical as possible without skimping on content.The book is designed to help you learn EJB 3 as quickly and easily as possible.At the same time,we won't neglect the basics where needed.We'll also dive into deep waters with you where we can,share with you all the amazing sights we've discovered.and warn you about any lurking dangers. This book is about the radical transformation of an important and uniquely influential technology in the Java world.We suspect you are not picking this book up to learn too much about EJB 2.You probably either already know EJB or are completely new to the world of EJB.In either case,spending too much time on previous versions is a waste of your time-you won't be surprised to learn that EJB 3 and EJB 2 have very little in common.If you are curious about the journey that brought us to the current point,we encourage you to pick up one of the many good books on the previous versions of EIB
4 CHAPTER 1 What’s what in EJB 3 One day, when God was looking over his creatures, he noticed a boy named Sadhu whose humor and cleverness pleased him. God felt generous that day and granted Sadhu three wishes. Sadhu asked for three reincarnations—one as a ladybug, one as an elephant, and the last as a cow. Surprised by these wishes, God asked Sadhu to explain himself. The boy replied, “I want to be a ladybug so that everyone in the world will admire me for my beauty and forgive the fact that I do no work. Being an elephant will be fun because I can gobble down enormous amounts of food without being ridiculed. I will like being a cow the best because I will be loved by all and useful to mankind.” God was charmed by these answers and allowed Sadhu to live through the three incarnations. He then made Sadhu a morning star for his service to humankind as a cow. EJB too has lived through three incarnations. When it was first released, the industry was dazzled by its innovations. But like the ladybug, EJB 1 had limited functionality. The second EJB incarnation was just about as heavy as the largest of our beloved pachyderms. The brave souls who could not do without its elephant power had to tame the awesome complexity of EJB 2. And finally, in its third incarnation, EJB has become much more useful to the huddled masses, just like the gentle bovine that is sacred for Hindus and respected as a mother whose milk feeds us well. Many people have put in a lot of hard work to make EJB 3 as simple and lightweight as possible without sacrificing enterprise-ready power. EJB components are now little more than plain old Java objects (POJOs) that look a lot like code in a Hello World program. We hope you agree with us as you read through the next chapters that EJB 3 has all the makings of a star. We’ve strived to keep this book as practical as possible without skimping on content. The book is designed to help you learn EJB 3 as quickly and easily as possible. At the same time, we won’t neglect the basics where needed. We’ll also dive into deep waters with you where we can, share with you all the amazing sights we’ve discovered, and warn you about any lurking dangers. This book is about the radical transformation of an important and uniquely influential technology in the Java world. We suspect you are not picking this book up to learn too much about EJB 2. You probably either already know EJB or are completely new to the world of EJB. In either case, spending too much time on previous versions is a waste of your time—you won’t be surprised to learn that EJB 3 and EJB 2 have very little in common. If you are curious about the journey that brought us to the current point, we encourage you to pick up one of the many good books on the previous versions of EJB
EJB overview Our goal in this chapter is to tell you what's what in EJB 3.explain why you should consider using it,and,for EJB 2 veterans,outline the significant improve ments the newest version offers.We'll then jump right into code in the next chap- ter to build on the momentum of this one.With these goals in mind,let's now start with a broad overview of EJB. 1.1 EJB overview In very straightforward terms,Enterprise JavaBeans(EJB)is a platform for build- ing portable,reusable,and scalable business applications using the Java program- ming language.Since its initial incarnation,EJB has been touted asa component model or framework that lets you build enterprise Java applications without hav ing to reinvent services such as transactions,security,automated persistence,and soon that you may need for building an application.EJB allows application devel- opers to focus on building business logic without having to spend time on build- ing infrastructure code. From a developer's point ofview,an EJB is a piece of Java code that executes in a specialized runtime environment called the E/B container,which provides a number of component services.The persistence services are provided by a spe- cialized framework called the persistence provider.We'll talk more about the EJB container,persistence provider,and services in section 1.3. In this section,you'll learn how EJB functions as both a component and a framework.We'll also examine how EJB lends itself to building layered applica- tions.We'll round off this section by listing a few reasons why EJB might be right for you. 1.1.1 EJB as a component In this book,when we talk about EJBs,we are referring to the server-side compo nents that you can use to build parts of your application,such as the business logic or persistence code.Many of us tend to associate the term component with devel- oping complex and heavyweight CORBA,Microsoft COM+code.In the brave new world of EIB 3.a component is what it ought to be-nothing more than a POIO with some special powers.More importantly,these powers remain invisible until they are needed and don't distract from the real purpose of the component.You will see this firsthand throughout this book,especially starting with chapter2. The real idea behind a component is that it should effectively encapsulate application behavior.The users of a component aren't required to know its inner workings.All they need to know is what to pass in and what to expect back
EJB overview 5 Our goal in this chapter is to tell you what’s what in EJB 3, explain why you should consider using it, and, for EJB 2 veterans, outline the significant improvements the newest version offers. We’ll then jump right into code in the next chapter to build on the momentum of this one. With these goals in mind, let’s now start with a broad overview of EJB. 1.1 EJB overview In very straightforward terms, Enterprise JavaBeans (EJB) is a platform for building portable, reusable, and scalable business applications using the Java programming language. Since its initial incarnation, EJB has been touted as a component model or framework that lets you build enterprise Java applications without having to reinvent services such as transactions, security, automated persistence, and so on that you may need for building an application. EJB allows application developers to focus on building business logic without having to spend time on building infrastructure code. From a developer’s point of view, an EJB is a piece of Java code that executes in a specialized runtime environment called the EJB container, which provides a number of component services. The persistence services are provided by a specialized framework called the persistence provider. We’ll talk more about the EJB container, persistence provider, and services in section 1.3. In this section, you’ll learn how EJB functions as both a component and a framework. We’ll also examine how EJB lends itself to building layered applications. We’ll round off this section by listing a few reasons why EJB might be right for you. 1.1.1 EJB as a component In this book, when we talk about EJBs, we are referring to the server-side components that you can use to build parts of your application, such as the business logic or persistence code. Many of us tend to associate the term component with developing complex and heavyweight CORBA, Microsoft COM+ code. In the brave new world of EJB 3, a component is what it ought to be—nothing more than a POJO with some special powers. More importantly, these powers remain invisible until they are needed and don’t distract from the real purpose of the component. You will see this firsthand throughout this book, especially starting with chapter 2. The real idea behind a component is that it should effectively encapsulate application behavior. The users of a component aren’t required to know its inner workings. All they need to know is what to pass in and what to expect back
CHAPTER 1 What's what in elb 3 There are three types of EJB components:session beans,message-driven beans. and entities.Session beans and message-driven beans are used to implement business logic in an EJB application,and entities are used for persistence. Components can be reusable.For instance,suppose you're in charge of build- ing a website for an online merchant that sells technology books.You implement a module tocharge the credit card as part of a regular Java object.Your company does fairly well,and you move on to greener pastures.The company then decides to diversify and begins developing a website for selling CDs and DVDs.Since the deployment environment for the new site is different,it can't be located on the same server as your module.The person building the new site is forced to dupli- module as an EJB component as shown in figure 1.1(or as a web service),it would have been much easier for the new person to access it by simply making a call to it when she needed that functionality.She could have reused it without having to understand its implementation. Given that,building a reusable component requires careful planning because across enterprise applications within an organization,very little of the business logic may be reusable.Therefore,you may not care about the reusability of EJB components,but EJB still has much to offer as a framework,as you'll discover in the next section. ning Music Online reusable components.For you ca m dult the B component that may be CreditCardEJB accessed by multiple applications
6 CHAPTER 1 What’s what in EJB 3 There are three types of EJB components: session beans, message-driven beans, and entities. Session beans and message-driven beans are used to implement business logic in an EJB application, and entities are used for persistence. Components can be reusable. For instance, suppose you’re in charge of building a website for an online merchant that sells technology books. You implement a module to charge the credit card as part of a regular Java object. Your company does fairly well, and you move on to greener pastures. The company then decides to diversify and begins developing a website for selling CDs and DVDs. Since the deployment environment for the new site is different, it can’t be located on the same server as your module. The person building the new site is forced to duplicate your credit card module in the new website because there’s no easy way to access your module. If you had instead implemented the credit card–charging module as an EJB component as shown in figure 1.1 (or as a web service), it would have been much easier for the new person to access it by simply making a call to it when she needed that functionality. She could have reused it without having to understand its implementation. Given that, building a reusable component requires careful planning because, across enterprise applications within an organization, very little of the business logic may be reusable. Therefore, you may not care about the reusability of EJB components, but EJB still has much to offer as a framework, as you’ll discover in the next section. Figure 1.1 EJB allows development of reusable components. For example, you can implement the credit card–charging module as an EJB component that may be accessed by multiple applications
EJB overview 1.1.2 EJB as a framework As we mentioned,EJB components live in a container.Together,the components, or EJBs,and the container can be viewed as a framework that provides valuable services for enterprise application development. Although many people think EJBs are overkill for developing relatively simple web applications of moderate size,nothing could be further from the truth.When you build a house,you don't build everything from scratch.Instead,you buy materials or even the services of a contractor as you need it.It isn't too practical to build an enterprise application from scratch either.Most server-side applications have a lot in common,including churning business logic,managing application state,storing and retrieving information from a relational database,managing transactions,implementing security,performing asynchronous processing,inte- grating systems,and so on. As a framework,the EJB container provides these kinds of common function- ality as out-of-the-box services so that your EJB components can use them in your applications without reinventing the wheel.For instance,let's say that when you built the credit card module in your web application,you wrote a lot of complex and error-prone code to manage transactions and security access control.You could have avoided that by using the declarative transaction and security services provided by the EJB container.These services,as well as many others you'll learn about in section 1.3,are available to the EJB components when they are deployed in the EJB container,as you can see in figure 1.2.This means writing high-quality, feature-rich applications much faster than you might think. The container provides the services to the EJB components in a rather elegant new way:metadata annotations are used to preconfigure the EJBs by specifying the type of services to add when the container deploys the ElBs.Java 5 introduced EJB Services Transaction CreditCardEJE State Management Messaging Figure 12 as a ork provide
EJB overview 7 1.1.2 EJB as a framework As we mentioned, EJB components live in a container. Together, the components, or EJBs, and the container can be viewed as a framework that provides valuable services for enterprise application development. Although many people think EJBs are overkill for developing relatively simple web applications of moderate size, nothing could be further from the truth. When you build a house, you don’t build everything from scratch. Instead, you buy materials or even the services of a contractor as you need it. It isn’t too practical to build an enterprise application from scratch either. Most server-side applications have a lot in common, including churning business logic, managing application state, storing and retrieving information from a relational database, managing transactions, implementing security, performing asynchronous processing, integrating systems, and so on. As a framework, the EJB container provides these kinds of common functionality as out-of-the-box services so that your EJB components can use them in your applications without reinventing the wheel. For instance, let’s say that when you built the credit card module in your web application, you wrote a lot of complex and error-prone code to manage transactions and security access control. You could have avoided that by using the declarative transaction and security services provided by the EJB container. These services, as well as many others you’ll learn about in section 1.3, are available to the EJB components when they are deployed in the EJB container, as you can see in figure 1.2. This means writing high-quality, feature-rich applications much faster than you might think. The container provides the services to the EJB components in a rather elegant new way: metadata annotations are used to preconfigure the EJBs by specifying the type of services to add when the container deploys the EJBs. Java 5 introduced Figure 1.2 EJB as a framework provides services to EJB components