12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8 ■ CALLBACKS AND INTERCEPTORSTable 8-1. Life-Cycle Callback AnnotationsAnnotation@PostConstruct@PreDestroy@PrePassivate@PostActivateDescriptionMarks a method to be invoked immediately after a bean instance is created anddependency injection is done by the container. This annotation is often used toperform any initialization.Marks a method to be invoked immediately before the bean instance is destroyed bythe container. The method annotated <strong>with</strong> @PreDestroy is often used to releaseresources that had been previously initialized. With stateful beans, this happens aftera method annotated <strong>with</strong> @Remove has been completed.Marks a method to be invoked before the container passivates the instance. It usuallygives the bean the time to prepare for serialization and to release resources thatcannot be serialized (e.g., it closes connections to a database, a message broker, anetwork socket, etc.).Marks a method to be invoked immediately after the container reactivates theinstance. Gives the bean a chance to reinitialize resources that had been closedduring passivation.■ Note @PrePassivate and @PostActivate annotations are defined in the javax.ejb package and the EJB 3.1specification (JSR 318). They are related to the EJB life cycle and can happen many times during the EJB lifetime.@PostConstruct and @PreDestroy are part of the Common Annotations 1.1 specification (JSR 250) and comefrom the javax.annotation package (like @Resource or other security annotations that you’ll see in the nextchapter). They are related to <strong>Java</strong> classes’ life cycle and happen only once during the class lifetime (at creationand at destruction).A callback method is required to have the following signature:void ();The following rules apply to a callback method:• The method must not have any parameters and must return void.• The method must not throw a checked exception but can throw runtimeexceptions. Throwing a runtime exception will roll back the transaction if oneexists (as explained in the next chapter).241

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!