MANNING SECOND EDITION Debu Panda Reza Rahman Ryan Cuprak Michael Remijan www.it-ebooks.info Praise for the First Edition This is the EJB book to read! Don’t miss its practical advice —Jeanne Boyarsky, JavaRanch.com A technical book that is surprisingly entertaining —King Y Wang, Oracle Canada Great book—covers everything relating to EJB —Awais Bajwa, Expert Group Member JSR 243 Java Data Objects Well-written, easy, and fun —Patrick Dennis, Management Dynamics Inc Written with a wide audience in mind … not just a recitation of the EJB specification … includes a lot of practical advice Has a light, humorous, and accessible style of writing and all the concepts are illustrated with examples —One Minute Review from javalobby.org Broad coverage of EJB with a very simple and excellently crafted case study The book starts lightly on this complex subject and slowly dives into the details of advanced concepts like interceptors, transactions, security, JPA, and performance issues, developing each scenario in the case study Overall, a very good book and a very smooth read —Amazon.com reader www.it-ebooks.info www.it-ebooks.info EJB in Action Second Edition DEBU PANDA REZA RAHMAN RYAN CUPRAK MICHAEL REMIJAN MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com The publisher offers discounts on this book when ordered in quantity For more information, please contact Special Sales Department Manning Publications Co 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2014 by Manning Publications Co All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine Manning Publications Co 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Development editor: Copyeditor and project editor: Proofreaders: Technical proofreader: Typesetter: Cover designer: ISBN: 9781935182993 Printed in the United States of America 10 – EBM – 19 18 17 16 15 14 www.it-ebooks.info Nermina Miller Jodie Allen Linda Recktenwald, Melody Dolab Deepak Vohra Dennis Dalinnik Marija Tudor brief contents PART PART PART OVERVIEW OF THE EJB LANDSCAPE 1 ■ What’s what in EJB ■ A first taste of EJB 25 WORKING WITH EJB COMPONENTS 47 ■ Building business logic with session beans 49 ■ Messaging and developing MDBs 93 ■ EJB runtime context, dependency injection, and crosscutting logic 117 ■ Transactions and security ■ Scheduling and timers 196 ■ Exposing EJBs as web services 160 214 USING EJB WITH JPA AND CDI 251 ■ JPA entities 253 10 ■ Managing entities 294 11 ■ JPQL 321 12 ■ Using CDI with EJB 359 v www.it-ebooks.info vi PART BRIEF CONTENTS PUTTING EJB INTO ACTION .395 13 ■ Packaging EJB applications 397 14 ■ Using WebSockets with EJB 427 15 ■ Testing and EJB 458 www.it-ebooks.info contents preface xv acknowledgments xvii about this book xx about the authors xxiv about the cover illustration xxvi PART OVERVIEW OF THE EJB LANDSCAPE 1 What’s what in EJB 3 1.1 EJB overview EJB as a component model EJB component services Layered architectures and EJB Why choose EJB 3? ■ ■ 1.2 Understanding EJB types 11 Session beans 11 1.3 ■ Message-driven beans Related specifications 12 Entities and the Java Persistence API 12 dependency injection for Java EE 13 1.4 EJB runtimes 12 ■ Contexts and 14 Application servers 14 EJB Lite 15 Embeddable containers 16 Using EJB in Tomcat 16 ■ ■ vii www.it-ebooks.info ■ CONTENTS viii 1.5 Brave new innovations 17 “Hello User” example 17 Annotations versus XML 18 Intelligent defaults versus explicit configuration 19 Dependency injection versus JNDI lookup 19 CDI versus EJB injection 20 Testable POJO components 20 ■ ■ ■ 1.6 Changes in EJB 3.2 21 Previous EJB features now optional 21 Enhancements to message-driven beans 21 Enhancements to stateful session beans 22 Simplifying local interfaces for stateless beans 23 Enhancements in TimerService API 23 Enhancements in EJBContainer API 23 EJB API groups 23 ■ ■ ■ ■ ■ 1.7 Summary 24 A first taste of EJB 25 2.1 Introducing the ActionBazaar application Starting with the architecture 2.2 27 Using CDI with EJB ■ 29 Using stateful beans Using JPA with EJB ■ Using CDI with EJB 41 Mapping JPA entities to the database 42 EntityManager 44 2.5 31 37 Using CDI with JSF and EJB 37 and JPA 40 2.4 26 An EJB 3–based solution 28 Building business logic with EJB Using stateless session beans 29 Unit testing EJB 36 2.3 ■ ■ Using the Summary 45 PART WORKING WITH EJB COMPONENTS 47 Building business logic with session beans 49 3.1 Getting to know session beans When to use session beans session bean types 52 3.2 Stateless session beans 51 ■ 50 Component state and 55 When to use stateless session beans 55 Stateless session bean pooling 56 BidService example 57 Using the @Stateless annotation 60 Bean business interfaces 60 Lifecycle callbacks 63 Using stateless session beans effectively 65 ■ ■ ■ ■ ■ www.it-ebooks.info CONTENTS 3.3 Stateful session beans ix 66 When to use stateful session beans 67 Stateful session bean passivation 68 Stateful session bean clustering 68 Bidder account creator bean example 69 Using the @Stateful annotation 72 Bean business interfaces 72 Lifecycle callbacks 73 Using stateful session beans effectively 75 ■ ■ ■ ■ ■ ■ 3.4 Singleton session beans 76 When to use singleton session beans 76 ActionBazaar featured item example 78 Using the @Singleton annotation 79 Singleton bean concurrency control 80 Bean business interface 83 Lifecycle callbacks 83 @Startup annotation 85 Using stateful singleton session beans effectively 85 ■ ■ ■ ■ 3.5 ■ Asynchronous session beans 87 Basics of asynchronous invocation 87 When to use asynchronous session beans 88 ProcessOrder bean example 88 Using the @Asynchronous annotation 90 Using the Future interface 91 Using asynchronous session beans effectively 91 ■ ■ ■ ■ 3.6 Summary 92 Messaging and developing MDBs 4.1 Messaging concepts 93 94 Message-oriented middleware 94 Messaging in ActionBazaar 95 Messaging models 97 ■ ■ 4.2 Introducing JMS 99 JMS Message interface 100 4.3 Working with MDBs 102 When to use messaging and MDBs 103 Why use MDBs? 103 Developing a message consumer with MDB 104 Using the @MessageDriven annotation 106 Implementing the MessageListener 106 Using ActivationConfigProperty 107 Using bean lifecycle callbacks 110 Sending JMS messages from MDBs 112 Managing MDB transactions 113 ■ ■ ■ ■ ■ ■ 4.4 4.5 MDB best practices Summary 115 113 EJB runtime context, dependency injection, and crosscutting logic 117 5.1 EJB context 118 Basics of EJB context 118 EJB context interfaces 119 Accessing the container environment through the EJB context 120 ■ www.it-ebooks.info ... Injecting events 38 3 Using conversations 38 6 Using CDI effectively with EJB Summary 39 2 39 1 PART PUTTING EJB INTO ACTION 39 5 13 Packaging EJB applications 13. 1 39 7 Packaging your applications 39 8... using @EJB 128 When to use EJB injection 129 @EJB annotation in action 129 Resource injection using @Resource 132 When to use resource injection 133 @Resource annotation in action 133 Looking... ■ Managing entities 294 11 ■ JPQL 32 1 12 ■ Using CDI with EJB 35 9 v www.it-ebooks.info vi PART BRIEF CONTENTS PUTTING EJB INTO ACTION .39 5 13 ■ Packaging EJB applications 39 7 14 ■ Using WebSockets