Thông tin tài liệu
www.it-ebooks.info
EJB 3.1 Cookbook
Build real world EJB solutions with a collection of simple
but incredibly effective recipes
Richard M. Reese
P U B L I S H I N G
professional expertise distilled
BIRMINGHAM - MUMBAI
www.it-ebooks.info
EJB 3.1 Cookbook
Copyright © 2011 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without war-
ranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and
distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.
First published: June 2011
Production Reference: 1030611
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849682-38-1
www.packtpub.com
Cover Image by Dan Anderson (Dan@CAndersonAssociates.com)
www.it-ebooks.info
Credits
Author
Richard M. Reese
Reviewers
Krum Bakalsky
Andrey Gotchalk
Deepak Vohra
Acquisition Editor
Amey Kanse
Development Editor
Chris Rodrigues
Technical Editor
Arun Nadar
Project Coordinator
Vishal Bodwani
Proofreader
Mario Cecere
Indexer
Monica Ajmera Mehta
Graphics
Geetanjali Sawant
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
www.it-ebooks.info
About the Author
Richard Reese is an Associate Professor teaching Computer Science at Tarleton
State University in Stephenville, Texas. Previously, he worked in the aerospace and
telephony industries for over 16 years. He earned his Ph.D. in Computer Science from
Texas A&M University. He also served four years in the Air Force primarily in the eld of
communication intelligence.
Outside of classroom, he enjoys tending his vegetable garden, maintaining his aquariums,
and running with his dog, Zoey. He also enjoys relaxing with an episode of Firey and is ever
hopeful for the return of the epic series.
Dr. Reese has written numerous publications and contributed to Turbo Pascal: Advanced
Applications.
No book can be written without the help from others. To this end I am
thankful for my wife Karla and daughter Jennifer whose patience, support,
and reviews have made this effort possible. In addition, I would like to
thank the editorial staff of Packt and my reviewers for their input which
has resulted in a much better book than it might otherwise have been.
Lastly, I am indebted to my doctorial committee chairman, Dr. Sallie
Sheppard, who years ago spent countless hours helping me to learn
how to write.
www.it-ebooks.info
About the Reviewers
Krum Bakalsky has nished his MSc studies in theoretical computer science from Soa
University. Afterwards he joined SAP, where he is currently part of the Java server team. He
drives different EJB related topics, and is responsible for JPA tasks as well. He is SCJP6,
SCBCD5, and SCWCD5 certied, and is very enthusiastic about the new Java EE 6 platform,
hoping that it will gain great adoption and will receive good popularity. His professional
interests include popular open source frameworks, like Spring, Hibernate, and Quartz. He has
some basic involvement in several tooling projects in the Eclipse family, and is interested in
cloud computing topics as well.
Being an amateur mathematician, in his spare time Krum likes to enjoy different math
activities, often related to his great math library, that he continues to maintain and expand.
Krum is a great koala lover and donator. His dream is to live one day a peaceful idyllic life in
his own house, far from civilization and surrounded by several koalas.
Andrey Gotchalk has more than 12 years of experience in software development. He
is certied by Sun Microsystems and Microsoft. He has worked for multiple multilingual
international software companies in Europe and North America, where has served in different
roles as senior software developer, team leader, and project manager. He speaks four
languages and he has lived and traveled at many places of the world. Currently he lives
and works in Montreal, Canada.
He has strong OOA/OOD and RDBMS skills, extensive experience in various technologies as
Java/JEE, PHP5, X++, Object Pascal, PL/SQL, Web development, ERP systems, and so on. But
his last preferences are JEE and mostly standard solutions like EJB, JPA, JSP, JSF, and much
more. He is also interested in analyzing and using various JEE open source projects. You can
reach him at a.gotchalk@gmail.com.
www.it-ebooks.info
Deepak Vohra is a consultant and a principal member of the NuBean.com software
company. Deepak is a Sun Certied Java Programmer and Web Component Developer, and
has worked in the elds of XML and Java programming and J2EE for over ve years. Deepak
is the co-author of the Apress book, Pro XML Development with Java Technology and was
the technical reviewer for the O’Reilly book, WebLogic: The Denitive Guide. Deepak was
also the technical reviewer for the Course Technology PTR book, Ruby Programming for the
Absolute Beginner, and the technical editor for the Manning Publications book, Prototype
and Scriptaculous in Action. Deepak is also the author of the Packt Publishing books JDBC
4.0 and Oracle JDeveloper for J2EE Development, and Processing XML Documents with
Oracle JDeveloper 11g.
www.it-ebooks.info
www.PacktPub.com
Support files, eBooks, discount offers
and more
You might want to visit www.PacktPub.com for support les and downloads related to
your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub les
available? You can upgrade to the eBook version at www.PacktPub.com and, as a print book
customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@
packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range
of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library.
Here, you can access, read, and search across Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib
today and view nine entirely free books. Simply use your login credentials for immediate access.
Instant updates on new Packt books
Get notied! Find out when new books are published by following @PacktEnterprise on Twitter,
or the Packt Enterprise Facebook page.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: Getting Started With EJBs 7
Introduction 8
Creating a simple session EJB 9
Accessing a session bean using dependency injection 11
Accessing the session bean using JNDI 14
Creating a simple message-driven bean 17
Sending a message to a message-driven bean 20
Accessing an EJB from a web service (JAX-WS) 23
Accessing an EJB from a web service (JAX-RS) 27
Accessing an EJB from an Applet 30
Accessing an EJB from JSP 35
Calling an EJB from JSF 38
Accessing an EJB from a Java Application using JNDI 40
Accessing an EJB from a Java Application using an embeddable container 42
Accessing the EJB container 44
Chapter 2: Session Beans 47
Introduction 47
Creating a stateless session bean 50
Creating a stateful session bean 54
Creating a singleton bean 58
Using multiple singleton beans 61
Using container managed concurrency 64
Using bean managed concurrency 67
Controlling the initialization process 70
Using session beans with more than one business interface 72
Understanding parameter behavior and granularity 76
Using an asynchronous method to create a background process 81
www.it-ebooks.info
[...]... portable applications based on Java technology Creating and using EJBs can be challenging and rewarding Among the challenges are learning the EJB technology itself, learning how to use the development environment you have chosen for EJB development, and the testing of the EJBs EJB 3.1 Cookbook addresses all these challenges and covers the new 3.1 features, along with an explanation of useful features retained... SalutationApplication -ejb The name of the bean was the last name Names are automatically generated for EJBs Prior to EJB 3.1, a JNDI name was server-specific and limited the portability of EJBS With EJB 3.1 this problem goes away and we can create more portable and maintainable applications There's more There are two features of JNDI needing further scrutiny Portable JNDI naming syntax EJBS supporting... and then seeing how they call and use EJBs EJBs can be used within a number of different clients including servlets, JSP, JSF, applets, Java SE type applications, and other EJBs From the client perspective, it is accessing an EJB as if the EJB existed in the same Java Virtual Machine (JVM) Depending on the client, this may be true Regardless, EJBs are managed by an EJB container that provides support... an EJB from JSP Calling an EJB from JSF Accessing an EJB from a Java Application using JNDI Accessing an EJB from a Java Application using an embeddable container Accessing the EJB container www.it-ebooks.info Getting Started With EJBs Introduction Creating and using Enterprise Java Beans (EJBs) can be challenging and rewarding Among the challenges are learning the EJB technology itself, learning... With EJBs In this chapter, we will cover: Creating a simple session EJB Accessing a session bean using dependency injection Accessing the session bean using JNDI Creating a simple message-driven bean Sending a message to a message-driven bean Accessing an EJB from a web service (JAX-WS) Accessing an EJB from a web service (JAX-RS) Accessing an EJB from an Applet Accessing an EJB from... a supporting EJB for the functionality of the service 2 Create a class annotated with the @WebService annotation to provide the service Creating the EJB and web service are both straightforward How to do it Create a new web application called TODService Within the application we will create a web service and a stateless EJB Next, create the singleton EJB in a package called ejb Add an EJB called TimeOfDay... essential steps to access a session EJB using dependency injection include: 1 Inject the EJB using the @EJB annotation 2 Access its methods as needed First we need a session bean To keep things simple, we will use the Salutation session EJB developed in the previous recipe We will add our servlet to the SalutationApplication 11 www.it-ebooks.info Getting Started With EJBs How to do it We will be developing... Holmes will be displayed in the browser How it works To provide a reference to an EJB in a servlet we used the @EJB annotation to inject the bean However, before we could use the annotation two import statements were required The first one was for the annotation and the second one is for the Salutation EJB import javax .ejb. EJB; import packt.Salutation; The declaration of the servlet began with @WebServlet... extends HttpServlet { The salutation variable was declared as a field of the servlet The @EJB annotation immediately preceded the variable declaration and effected dependency injection This allows the EJB container to support the EJB @EJB private Salutation salutation; 13 www.it-ebooks.info Getting Started With EJBs HTTP Servlets typically respond to doGet and doPost HTTP commands The doGet and doPost... EJB 3.1 In addition, Windows 7 Professional 64 bit edition, service pack 1, was used to develop the applications The Mozilla Firefox v3.6.15 browser was used to display web pages EJBs can be used in many different contexts and called from many different types of applications In presenting EJBs, a fundamental question is this: how do we go about illustrating the use of an EJB before we know what an EJB . descriptors for security 36 9
Chapter 12 : EJB Techniques 37 3
Introduction 37 3
Exception handling and EJBs 37 5
Using logging within an EJB 38 0
Using an interceptor. JAX-WS 32 8
Creating an EJB- based web service using JAX-RS 33 4
Using an MDB as part of a web service 34 0
Chapter 11 : Packaging the EJB 34 5
Introduction 34 5
Understanding
Ngày đăng: 16/03/2014, 20:20
Xem thêm: EJB 3.1 Cookbook pptx, EJB 3.1 Cookbook pptx