EJB Call backs - EJB(Enterprise JavaBeans)

What is an EJB Call back?

Call back is a instrument by which the life cycle of an enterprise bean can be intercept. EJB 3.0 specification has specified call-backs for which call-back handler method is formed. EJB Container calls these call backs. We can describe call back methods in the EJB class itself or in a divide class. EJB 3.0 has provided many annotations for call backs.

Following is the list of call back annotations for stateless bean −

Annotation Description
@PostConstruct Invoked when a bean is created for the first time.
@PreDestroy Invoked when a bean is removed from the bean pool or is destroyed.

Following is the list of call back annotations for tasteful bean –

Annotation Description
@PostConstruct Invoked when a bean is created for the first time.
@PreDestroy Invoked when a bean is removed from the bean pool or is destroyed.
@PostActivate Invoked when a bean is loaded to be used.
@PrePassivate Invoked when a bean is put back to bean pool.

Following is the list of call back annotations for message driven bean –

Annotation Description
@PostConstruct Invoked when a bean is created for the first time.
@PreDestroy Invoked when a bean is removed from the bean pool or is destroyed.

Following is the list of call back annotations for entity bean –

Annotation Description
@PrePersist Invoked when an entity is created in database.
@PostPersist Invoked after an entity is created in database.
@PreRemove Invoked when an entity is deleted from the database.
@PostRemove Invoked after an entity is deleted from the database.
@PreUpdate Invoked before an entity is to be updated in the database.
@PostLoad Invoked when a record is fetched from database and loaded into the entity.

Example Application

Let us create a test EJB application to test various call backs in EJB.

Step Description
1 Create a project with a nameEjbComponentunder a packagecom.wisdomjobs.statelessas explained in theEJB - Create Applicationchapter. You can also use the project created inEJB - Persistencechapter as such for this chapter to add various callbacks to EJB.
2 CreateLibrarySessionBean.javaandLibrarySessionBeanRemoteas explained in theEJB - Create Applicationchapter. Keep rest of the files unchanged.
3 Use Beans created in theEJB - Persistencechapter. Add callback methods as shown below. Keep rest of the files unchanged.
4 Create a java classBookCallbackListenerunder packagecom.wisdomjobs.callback. This class will demonstrates the separation of callback methods.
5 Clean and Build the application to make sure business logic is working as per the requirements.
6 Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.
7 Now create the EJB client, a console based application in the same way as explained in theEJB - Create Applicationchapter under topicCreate Client to access EJB.

EJBComponent (EJB Module)

BookCallbackListener.java

Book.java

LibraryStatefulSessionBean.java

LibraryStatefulSessionBeanRemote.java

LibraryPersistentBean.java

LibraryPersistentBeanRemote.java

  • As soon as you deploy the EjbComponent project on JBOSS, notice the jobs log.
  • Jobs have manually created a JNDI entry for our session bean - LibraryPersistentBean/remote.
  • We will be using this lookup string to get remote business object of type -com.wisdomjobs.stateless.LibraryPersistentBeanRemote

Jobs Application Server Log Output

EJBTester (EJB Client)

jndi.properties

  • These properties are used to initialize the Initial Context object of java naming service.
  • Initial Context object will be used to lookup stateless session bean.

EJBTester.java

EJBTester performs the following tasks −

  • Load properties from jndi.properties and initialize the Initial Context object.
  • In testStatelessEjb () method, jade lookup is done with the name - "LibrarySessionBean/remote" to obtain the remote business object (stateless EJB).
  • Then user is shown a library store User Interface and he/she is asked to enter a choice.
  • If the user enters 1, the system asks for book name and saves the book using stateless session bean add Book () method. Session Bean is storing the book in the database.
  • If user enters 2, system retrieves books using stateless session bean get Books () method and exits.

Run Client to Access EJB

Locate EJBTester.java in scheme explorer. Right click on EJBTester class and select run file.

Verify the following output in Net beans console.

Jobs Application Server Log Output

You can locate the following call back entries in Jobs log

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

EJB(Enterprise JavaBeans) Topics