Q: I just remembered that I read somewhere that enterprise beans don t support inheritance! What s that about?

Similar documents
Enterprise JavaBeans. Layer:03. Session

UNIT-III EJB APPLICATIONS

Sharpen your pencil. entity bean synchronization

Enterprise JavaBeans. Session EJBs

Stateless Session Bean

Sharpen your pencil. Container services. EJB Object. the bean

Enterprise Java Beans

The Developer s Guide to Understanding Enterprise JavaBeans. Nova Laboratories

Enterprise JavaBeans. Layer:07. Entity

Lab2: CMP Entity Bean working with Session Bean

The Details of Writing Enterprise Java Beans

This chapter describes: AssetBean AssetMgr AssetMgrEJB AssetMgrHome AssetTagAssociationBean AssetTypeBean AssociationBean AssociationMgr

Introduction to Session beans EJB 3.0

Entity Beans 02/24/2004

SCBCD EXAM STUDY KIT. Paul Sanghera CX JAVA BUSINESS COMPONENT DEVELOPER CERTIFICATION FOR EJB MANNING. Covers all you need to pass

Oracle8i. Enterprise JavaBeans Developer s Guide and Reference. Release 3 (8.1.7) July 2000 Part No. A

Oracle Developer Day Agenda

Enterprise JavaBeans. Layer:08. Persistence

jar command Java Archive inherits from tar : Tape Archive commands: jar cvf filename jar tvf filename jar xvf filename java jar filename.

Enterprise JavaBeans: BMP and CMP Entity Beans

@jbossdeveloper. explained

Teamcenter Global Services Customization Guide. Publication Number PLM00091 J

Lab1: Stateless Session Bean for Registration Fee Calculation

EJB ENTERPRISE JAVA BEANS INTRODUCTION TO ENTERPRISE JAVA BEANS, JAVA'S SERVER SIDE COMPONENT TECHNOLOGY. EJB Enterprise Java

J2EE Application Server. EJB Overview. Java versus.net for the Enterprise. Component-Based Software Engineering. ECE493-Topic 5 Winter 2007

Life Cycle of an Entity Bean

Conception of Information Systems Lecture 8: J2EE and EJBs

Session Bean. Disclaimer & Acknowledgments. Revision History. Sang Shin Jeff Cutler

Copyright UTS Faculty of Information Technology 2002 EJB2 EJB-3. Use Design Patterns 1 to re-use well known programming techniques.

Objectives. Software Development using MacroMedia s JRun. What are EJBs? Topics for Discussion. Examples of Session beans calling entity beans

JBuilder. Getting Started Guide part II. Preface. Creating your Second Enterprise JavaBean. Container Managed Persistent Bean.

Distributed Applications (RMI/JDBC) Copyright UTS Faculty of Information Technology 2002 EJB EJB-3

Oracle9iAS Containers for J2EE

Applying Code Generation Approach in Fabrique Kirill Kalishev, JetBrains

Topexam. 一番権威的な IT 認定試験ウェブサイト 最も新たな国際 IT 認定試験問題集

8. Component Software

these methods, remote clients can access the inventory services provided by the application.

ENTERPRISE beans are the J2EE components that implement Enterprise Java-

IBM. Application Development with IBM Rational Application Developer for Web Sphere

Lab3: A J2EE Application with Stateful Session Bean and CMP Entity Beans

Word: Print Address Labels Using Mail Merge

Oracle EXAM - 1Z Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam. Buy Full Product.

8. Component Software

JAVA. Aspects (AOP) AspectJ

Exam Questions 1Z0-895

CHAPTER 3 SESSION BEANS

Practice Test. Oracle 1z Java Enterprise Edition 5 Business Component Developer Certified Professional Upgrade Exam. Version: 14.

Enterprise JavaBeans (I) K.P. Chow University of Hong Kong

The 1st Java professional open source Convention Israel 2006

Enterprise JavaBeans (EJB) security

Web Design and Applications

Chapter 1 Introducing EJB 1. What is Java EE Introduction to EJB...5 Need of EJB...6 Types of Enterprise Beans...7

Virus Scan with SAP Process Integration Using Custom EJB Adapter Module

Fast Track to EJB 3.0 and the JPA Using JBoss

<<Interface>> EntityBean (from ejb) EJBHome. <<Interface>> CountHome. (from entity) create() findbyprimarykey() <<Interface>> EJBObject.

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

BEA WebLogic Server. Enterprise JavaBeans

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

ITdumpsFree. Get free valid exam dumps and pass your exam test with confidence

Stateful Session Beans

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Entities are classes that need to be persisted, usually in a relational database. In this chapter we cover the following topics:

Chapter 6 Enterprise Java Beans

Developing CMP 2.0 Entity Beans

SDN Community Contribution

Enterprise JavaBeans

Annotation Hammer Venkat Subramaniam (Also published at

Describing the functionality of EJB using the behavior protocols

Understanding and Designing with EJB

133 July 23, :01 pm

Using JNDI from J2EE components

1Z Oracle. Java Enterprise Edition 5 Business Component Developer Certified Professional Upgrade

J2EE Web Development 13/1/ Application Servers. Application Servers. Agenda. In the beginning, there was darkness and cold.

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Topics. Advanced Java Programming. Transaction Definition. Background. Transaction basics. Transaction properties

More on Objects in JAVA TM

SUN Sun Cert Bus Component Developer Java EE Platform 5, Upgrade. Download Full Version :

Java EE 7: Back-End Server Application Development

18 Final Submission and Essay

XP: Backup Your Important Files for Safety

Upgrading Your Geant4 Release

~ Ian Hunneybell: CBSD Revision Notes (07/06/2006) ~

CS 167 Final Exam Solutions

Enterprise JavaBeans TM

Touring the Mac S e s s i o n 4 : S A V E, P R I N T, C L O S E & Q U I T

03. DECORATOR PATTERN. Design Eye for the Inheritance Guy

Developing Portable Applications for the Java 2 Platform, Enterprise Edition (J2EE )

Oracle Containers for J2EE

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Erik Dörnenburg JAOO 2003

Classes, interfaces, & documentation. Review of basic building blocks

Container Managed Persistent in EJB 2.0

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Chapter 5 Object-Oriented Programming

CSE 143 Lecture 12 Inheritance

Create Modules for the J2EE Adapter Engine

Code generation with XDoclet. It s so beautifully arranged on the plate you know someone s fingers have been all over it.

CO Java EE 7: Back-End Server Application Development

Enterprise JavaBeans 2.1

Transcription:

session beans there are no Dumb Questions Q: If it s so common to leave the methods empty, why don t they have adapter classes like they have for event handlers that implement all the methods from the interface? Is there any reason why your bean class can t extend a class that implements the SessionBean interface? A: The API doesn t have adapter classes for SessionBean implementations (i.e. a class that implements all of the methods). But there s no reason you can t make one yourself. Keep in mind, though, that with real-world beans you probably will have code in one or more of the methods. And you might even be working with a bean-aware IDE that puts the methods in for you anyway. Still, it might be handy to make yourself a generic bean that you typically extend from, that has all of the methods from SessionBean. With stateless beans, especially, you have to implement ejbactivate() and ejbpassivate(), even though they ll never be called! (Stateless beans are never passivated; you ll see more on that later in the chapter.) Q: I just remembered that I read somewhere that enterprise beans don t support inheritance! What s that about? For the exam, you have to know exactly which methods are in the SessionBean interface, so now is a good time to start memorizing them. See if you can remember the name of the method that matches the behavior described. We ve included some pretty big hints here because it s your first time, but the mock exam questions will be much less obvious... 1. This method is called when the client tells the Container that he s done using a stateful session bean. The bean is NOT happy: ejbremove() A: Ah... a common misconception. Well, sort of. EJB supports regular Java class inheritance, but has no concept of bean inheritance. And now you re asking, What the heck is the difference? You already know what class inheritance is, it s the thing you do in Java when one class extends another. And you can do that with a bean, just like any other class. But bean inheritance (if it were supported) would mean that a bean class could extend another bean class and inherit not just the class inheritable members, but also its beanness. What kind of beanness might be inheritable? (Just in case they do decide to support this in the future, which is a possibility. Regular old non-enterprise beans do support bean inheritance.) One idea might be to have your bean subclass inherit some of the deployment descriptor settings of its superclass, and then override the ones it wants to change with a much smaller, incomplete deployment descriptor. That might be cool; we re not sure. But right now, it s just our little fantasy. In the meantime, go ahead and let your bean extend another class, if it makes sense for your OO design. 2. This method is called when the bean is put to sleep to temporarily conserve resources: ejbpassivate() 3. This method is called when the previouslysleeping bean is called back to active duty to service a business method: ejbactivate() 4. This method is called near the beginning of the bean s life, when the Container hands the bean a reference to the bean s special link to the Container: setsessioncontext(sessioncontext sc) 186 Chapter 4

session bean lifecycle You re responsible for making sure that when ejbpassivate() completes, your instance variables are in one of the states we listed a couple of pages ago. Don t look now! See if you can work out which of these will be safely passivated... q reference to a java.net.socket object You can t passivate a non-serializable, non-null value... so that means no Socket, and no Connection. q reference to a javax.sql.datasource q reference to a bean s Remote component interface q reference to a bean s JNDI context q reference to a java.sql.connection q reference to a javax.ejb.sessioncontext object A transient non-null value is fine for passivation, but there is no guarantee that when it comes BACK from activation it will have default values. In other words, JUSt using the transient modifier without also setting the value to null might passivate OK, and activate OK, but the value after activation might be weird! (Moral: go ahead and use transient, but also set the values to null in ejbpassivate()) Big explosion here (well, technically, one of the things the Container IS required to passivate might not actually be Serializable, but that s not your concern. The Container is still required to save those special things (like bean references, JNDI context, SessionContext, etc.) as if they WERE Serializable. q a transient variable with a null value q a non-transient, Serializable variable with a null value q a transient variable with a non-null value q a non-transient, non-serializable variable with a null value q a non-transient, non-serializable variable with a non-null value* you are here 4 205

session beans Do the interfaces have to change when it goes from stateless to stateful? Look at the two interfaces below, for the stateless version of the Advice bean. If needed, make any adjustments to the code in either or both of the interfaces, for what (if anything) needs to change to make this work with the revised stateful version of the bean. package headfirst; public interface AdviceHome extends EJBHome { public Advice create() throws CreateException, RemoteException; public Advice create(string name) throws CreateException, RemoteException; package headfirst; public interface Advice extends EJBObject { public String getadvice() throws RemoteException; Nothing changes with the business method interface (even though the implementation will change) 220 Chapter 4

writing session beans I have a system when I sit down to write a Session bean... I always have to put in three kinds of things: home stuff, business methods, and the SessionBean methods. Writing a Session Bean: your job as Bean Provider You put THREE kinds of methods in the bean class: create() AdviceHome 1 HOME things: ejbcreate() methods Write an ejbcreate() method in the bean to match each create() method in the home interface. Advice getadvice() 2 COMPONENT things: business methods Write a business method in the bean to match each method in your bean s component interface. SessionBean setsessioncontext() ejbpassivate() ejbactivate() ejbremove() 3 SESSION BEAN things: container callbacks from the SessionBean interface Implement each of the four methods from the SessionBean interface, which your bean must implement in the official Java way (i.e. using the implements SessionBean declaration either in your bean class or one of its superclasses) Of the three types of methods you put in your bean, check off the ones the compiler cares about. Compiler-checked? q Methods to match the Home interface q Methods to match the Component interface q Methods from the SessionBean interface 230 Chapter 4

session bean lifecycle Given the following interfaces, write the bean class code (you can leave the method empty) at the bottom of the page. Pay special attention to the Home create method... what does it take to match this in the bean? Will it have the same return type? Hints are at the bottom of the page. public interface KennelHome extends EJBHome { public Kennel create(string custid) throws CreateException, RemoteException; public interface Kennel extends EJBObject { public KennelLease placepet(pet p) throws RemoteException; public void renewlease(klease lease) throws RemoteException, ExpiredException; public Pet getpet(klease lease) throws RemoteException, DeadPetException; Write the bean class here: public class KennelBean extends javax.ejb.ejbobject { public void ejbcreate(string custid) throws CreateException { public void ejbactivate() { public void ejbpassivate() { public void setsessioncontext(sessioncontext ctx) { We don t throw RemoteException, but it s a good idea to declare CreateException on your create... public KennelLease placepet(pet p) { return new KennelLease(); public void renewlease(klease lease) throws ExpiredException { public Pet getpet(klease lease) throws DeadPetException { We don t throw RemoteException, but we re assuming that if you really implemented this class, you d declare and use these other application-specific exceptions. But according to plain old Java rules, you don t have to declare the exceptions declared in the interface, unless your method really DOES potentially throw that exception... you are here 4 231

session bean lifecycle Who does What? From the list of words below, arrange them in the appropriate lists according to whether it s a responsibility of the Bean Provider, the Container, or the Client. Bean Provider Client Container implementing the ejbactivate() method implementing the ejbcreate() method implementing SessionBean creating the home interface invoking a business method on the component interface invoking create() invoking ebjpassivate() implementing the create() method implementing the EJBObject class invoking setsessioncontext() implementing the Handle class invoking ejbcreate() creating the Home object class implementing the SessionContext class invoking ejbremove() you are here 4 239

entity beans For the four database operations (SQL commands) a client might want to do with an entity bean, list the methods in the bean s interface(s) that are related to those database operations. No, you don t have to know SQL, but you definitely have to understand the implications of the four database operations, and you must know how they correspond to methods in the bean class. From the list of the methods in the interfaces, fill in the method or methods that correspond with the database operation. INSERT: create on the home DELETE: remove on the home or component interface UPDATE: the two setters in the component interface SELECT: the two finders in the home, and whatever else loads the bean with data. (You might include the getters here, although a SELECT isn t necessarily done each time a getter is called (depends on the transaction) CustomerHome create(string last, String first) findbyprimarykey(string key) findbycity(string city) getejbmetadata() gethomehandle() remove(handle h) remove(object key) Customer getlastname() setlastname(string s) getfirstname() setfirstname(string s) getprimarykey() getejbhome() gethandle() remove() isidentical() 280 Chapter 5