EJB 3 in Action
eBook - ePub

EJB 3 in Action

Reza Rahman, Michael Remijan, Debu Panda, Ryan Cuprak

Share book
  1. 560 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

EJB 3 in Action

Reza Rahman, Michael Remijan, Debu Panda, Ryan Cuprak

Book details
Book preview
Table of contents
Citations

About This Book

Summary Building on the bestselling first edition, EJB 3 in Action, Second Edition tackles EJB 3.2 head-on, through numerous code samples, real-life scenarios, and illustrations. This book is a fast-paced tutorial for Java EE 6 business component development using EJB 3.2, JPA 2, and CDI. Besides covering the basics of EJB 3.2, this book includes in-depth EJB 3.2 internal implementation details, best practices, design patterns, and performance tuning tips.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Book The EJB 3 framework provides a standard way to capture business logic in manageable server-side modules, making it easier to write, maintain, and extend Java EE applications. EJB 3.2 provides more enhancements and intelligent defaults and integrates more fully with other Java technologies, such as CDI, to make development even easier. EJB 3 in Action, Second Edition is a fast-paced tutorial for Java EE business component developers using EJB 3.2, JPA, and CDI. It tackles EJB head-on through numerous code samples, real-life scenarios, and illustrations. Beyond the basics, this book includes internal implementation details, best practices, design patterns, performance tuning tips, and various means of access including Web Services, REST Services, and WebSockets.Readers need to know Java. No prior experience with EJB or Java EE is assumed. What's Inside

  • Fully revised for EJB 3.2
  • POJO persistence with JPA 2.1
  • Dependency injection and bean management with CDI 1.1
  • Interactive application with WebSocket 1.0


About the Authors Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan are seasoned Java architects, developers, authors, and community leaders. Debu and Reza coauthored the first edition of EJB 3 in Action. Table of Contents
PART 1 OVERVIEW OF THE EJB LANDSCAPE

  • What's what in EJB 3
  • A first taste of EJB
  • PART 2 WORKING WITH EJB COMPONENTS
  • Building business logic with session beans
  • Messaging and developing MDBs
  • EJB runtime context, dependency injection, and crosscutting logic
  • Transactions and security
  • Scheduling and timers
  • Exposing EJBs as web services
  • PART 3 USING EJB WITH JPA AND CDI
  • JPA entities
  • Managing entities
  • JPQL
  • Using CDI with EJB 3
  • PART 4 PUTTING EJB INTO ACTION
  • Packaging EJB 3 applications
  • Using WebSockets with EJB 3
  • Testing and EJB

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is EJB 3 in Action an online PDF/ePUB?
Yes, you can access EJB 3 in Action by Reza Rahman, Michael Remijan, Debu Panda, Ryan Cuprak in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation en Java. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2014
ISBN
9781638352990

Part 1. Overview of the EJB landscape

This book is about Enterprise Java Beans (EJB) 3, and covers up to the EJB 3.2 specification. The goal of EJB 3.2 is to continue to evolve the EJB specification to be a complete solution for all Enterprise business needs and to improve the EJB architecture by reducing its complexity from the developer’s point of view.
Part 1 presents EJB 3 as a powerful, highly usable platform worthy of its place as the business component development standard for mission-critical Enterprise development. We’ll introduce the Java Persistence API (JPA 2.1), a Java EE technology that aims to standardize Java ORM and works hand-in-hand with EJB 3. We’ll also take a quick look at Contexts and Dependency Injection for Java (CDI 1.1), the next-generation generic type-safe dependency injection technology for Java EE.
In chapter 1 we introduce the pieces that make up EJB 3, touching on the unique strengths EJB has as a development platform and the new features that promote productivity and ease of use. We even throw in a “Hello World” example.
In chapter 2 we provide more realistic code samples and introduce the Action-Bazaar application, an imaginary Enterprise system developed throughout the book. We’ll try to give you a feel for how EJB 3 looks as quickly and easily as possible. Be ready for a lot of code!

Chapter 1. What’s what in EJB 3

This chapter covers
  • The EJB container and its role in Enterprise applications
  • The different types of Enterprise Java Beans (EJBs)
  • Closely related technologies such as the Java Persistence API (JPA)
  • The different EJB runtime environments
  • Innovations started with EJB 3
  • New changes with EJB 3.2
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’ll like being a cow the best because I’ll 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 major 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 almost as heavy as the largest of our beloved pachyderms. The brave souls who couldn’t 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’s sacred for Hindus and respected as a mother whose milk feeds us well.
A lot of hard work from a lot of good people made EJB 3 simple and lightweight without sacrificing Enterprise-ready power. EJB components can now be Plain Old Java Objects (POJOs) and look a lot like code in a “Hello World” program. In the following chapters we’ll describe a star among frameworks with increasing industry adoption.
We’ve strived to keep this book practical without skimping on content. The book is designed to help you learn EJB 3 quickly and easily without neglecting the basics. We’ll also dive into deep waters, sharing all the amazing sights we’ve discovered and warning about any lurking dangers.
In the Java world EJB is an important and uniquely influential technology radically transformed in version 3. We’ll spend little time with EJB 2. You probably either already know earlier versions of EJB or are completely new to it. Spending too much time on previous versions is a waste of time. EJB 3 and EJB 2 have very little in common, and EJB 3.2 now makes support for EJB 2 optional. But if you’re curious about EJB 2, we encourage you to pick up one of the many good books on the previous versions of EJB.
In this chapter we’ll tell you what’s what in EJB 3, explain why you should consider using it, and outline the significant improvements the newest version offers, such as annotations, convention-over-configuration, and dependency injection. We’ll build on the momentum of this chapter by jumping into code in chapter 2. Let’s start with a broad overview of EJB.

1.1. EJB overview

The first thing that should cross your mind while evaluating any technology is what it really gives you. What’s so special about EJB? Beyond a presentation-layer technology like JavaServer Pages (JSP), JavaServer Faces (JSF), or Struts, couldn’t you create your web application using the Java language and some APIs like Java Database Connectivity (JDBC) for database access? You could—if deadlines and limited resources weren’t realities. Before anyone dreamed up EJB, this is exactly what people did. The resulting long hours proved that you’d spend a lot of time solving very common system-level problems instead of focusing on the real business solution. These experiences emphasized that there are common solutions for common development problems. This is exactly what EJB brings to the table. EJB is a collection of “canned” answers to common server application development problems, as well as a roadmap to common server component patterns. These canned solutions or services are provided by the EJB container. To access these services, you build specialized components using declarative and programmatic EJB APIs and deploy them into the container.

1.1.1. EJB as a component model

In this book, EJBs refer to server-side components that you can use to build the business component layer of your application. Some developers associate the term component with developing complex and heavyweight CORBA or 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 stay invisible until they’re needed and don’t distract from the real purpose of the component. You’ll see this firsthand throughout this book, especially starting in chapter 2.
To use EJB services, your component must be declared to be a recognized EJB component type. EJB recognizes two specific types of components: session beans and message-driven beans. Session beans are further subdivided into stateless session beans, stateful session beans, and singletons. Each component type has a specialized purpose, scope, state, lifecycle, and usage pattern in the business logic tier. We’ll discuss these component types throughout the rest of the book, particularly in part 2. For data CRUD (create, read, update, delete) operations in the persistence tier, we’ll talk about JPA entities and their relationship with EJBs in detail in part 3. As of EJB 3.1, all EJBs are managed beans. Managed beans are basically any generic Java object in a Java EE environment. Contexts and Dependency Injection (CDI) allows you to use dependency injection with all managed beans, including EJBs. We’ll explore CDI and managed beans further in part 3.

1.1.2. EJB component services

As we mentioned, the canned services are the most valuable part of EJB. Some of the services are automatically attached to recognize components because they make a lot of sense for business logic-tier components. These services include dependency injection, transactions, thread safety, and pooling. To use most services, you must declare you want them using annotations/XML or by accessing programmat...

Table of contents