Callbacks and Callback Handlers

The EJB 3.0 specification defines some callbacks, and allows you to handle these by implementing your own callback handlers. The callbacks defined for each bean are shown below:

Stateless session bean callbacks

Message-driven bean callbacks

Stateful session bean callbacks

Entity bean callbacks

The callbacks are not compulsory, and you can define the ones you want to handle.

Using callbacks on the bean class

You use the callbacks listed above by either annotating methods in the bean class. The annotations live in the javax.ejb package so for example PostConstruct would be javax.ejb.PostConstruct. You can call the methods what you like, but their method signature must return a void and take no arguments. Look at the CustomerDAOBean stateless session bean and you will see that the preDestroyCallback() method has been annotated with @javax.ejb.PreDestroy.

Using callbacks and a callback listener

You can also separate out the callbacks into a separate CallbackListener class. Take a look at the Customer entity bean, and you will see that the class has been annotated with
@javax.persistence.EntityListener("org.jboss.tutorial.callback.bean.CustomerCallbackListener")
This specifies that the org.jboss.tutorial.callback.bean.CustomerCallbackListener class should be used as the callback listener class for the bean. Now open CustomerCallbackListener and you will see that the class annotates the callback methods in the same way as when defining callbacks on the bean class itself. However, one important difference is that callback methods defined in a listener class must take a single argument, which will be the bean we are working with. The parameter must be of type java.lang.Object, or the actual bean type.

You can define callback listeners on session and message-driven beans in exactly the same way, the only difference is you use the javax.ejb.CallbackListener annotation instead.

Building and Running

To build and run the example, make sure you have ejb3.deployer installed in JBoss 4.0.x and have JBoss running. See the reference manual on how to install EJB 3.0.
Unix:    $ export JBOSS_HOME=<where your jboss 4.0 distribution is>
Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is>
$ ant
$ ant run

run:
     [java] Create Bill Burke and Monica Smith
     [java] log4j:WARN No appenders could be found for logger (org.jboss.remoting.Client).
     [java] log4j:WARN Please initialize the log4j system properly.
     [java] Bill and Monica get married
     [java] Get all the Burkes
     [java] There are now 2 Burkes
     [java] Bill and Monica are moving abroad

In the jboss console window you should see:

20:06:05,596 INFO  [STDOUT] PostConstruct - Have EntityManager: true
20:06:05,596 INFO  [STDOUT] -- CustomerDAOBean.create()
20:06:05,596 INFO  [STDOUT] doPrePersist: About to create Customer: Bill Burke
20:06:05,596 INFO  [STDOUT] doPostPersist: Created Customer: Bill Burke
20:06:05,617 INFO  [STDOUT] -- CustomerDAOBean.create()
20:06:05,617 INFO  [STDOUT] doPrePersist: About to create Customer: Monica Smith
20:06:05,617 INFO  [STDOUT] doPostPersist: Created Customer: Monica Smith
20:06:05,617 INFO  [STDOUT] -- CustomerDAOBean.find()
20:06:05,627 INFO  [STDOUT] doPostLoad: Loaded Customer: Monica Smith
20:06:05,627 INFO  [STDOUT] -- CustomerDAOBean.merge()
20:06:05,637 INFO  [STDOUT] doPostLoad: Loaded Customer: Monica Smith
20:06:05,637 INFO  [STDOUT] doPreUpdate: About to update Customer: Monica Burke
20:06:05,657 INFO  [STDOUT] doPostUpdate: Updated Customer: Monica Burke
20:06:05,667 INFO  [STDOUT] -- CustomerDAOBean.findByLastName(id)
20:06:05,677 INFO  [STDOUT] doPostLoad: Loaded Customer: Bill Burke
20:06:05,677 INFO  [STDOUT] doPostLoad: Loaded Customer: Monica Burke
20:06:05,687 INFO  [STDOUT] -- CustomerDAOBean.delete()
20:06:05,687 INFO  [STDOUT] doPreRemove: About to delete Customer: Bill Burke
20:06:05,687 INFO  [STDOUT] doPreRemove: About to delete Customer: Monica Burke
20:06:05,687 INFO  [STDOUT] doPostRemove: Deleted Customer: Bill Burke
20:06:05,697 INFO  [STDOUT] doPostRemove: Deleted Customer: Monica Burke

Now if you open up JBOSS_HOME/server/all/deploy/ejb3.deployer/META-INF/hibernate.properties and add the following line

hibernate.show_sql=true

and restart JBoss before running the example again, the output should be:

20:22:10,013 INFO  [STDOUT] PostConstruct - Have EntityManager: true
20:22:10,033 INFO  [STDOUT] -- CustomerDAOBean.create()
20:22:10,103 INFO  [STDOUT] doPrePersist: About to create Customer: Bill Burke
20:22:10,123 INFO  [STDOUT] Hibernate: insert into CUSTOMER (STATE, FIRST, LAST, STREET, CITY, ZIP, id) values (?, ?, ?, ?, ?, ?, null)
20:22:10,133 INFO  [STDOUT] Hibernate: call identity()
20:22:10,143 INFO  [STDOUT] doPostPersist: Created Customer: Bill Burke
20:22:10,194 INFO  [STDOUT] -- CustomerDAOBean.create()
20:22:10,194 INFO  [STDOUT] doPrePersist: About to create Customer: Monica Smith
20:22:10,194 INFO  [STDOUT] Hibernate: insert into CUSTOMER (STATE, FIRST, LAST, STREET, CITY, ZIP, id) values (?, ?, ?, ?, ?, ?, null)
20:22:10,194 INFO  [STDOUT] Hibernate: call identity()
20:22:10,194 INFO  [STDOUT] doPostPersist: Created Customer: Monica Smith
20:22:10,264 INFO  [STDOUT] -- CustomerDAOBean.find()
20:22:10,274 INFO  [STDOUT] Hibernate: select customer0_.id as id0_, customer0_.STATE as STATE0_0_, customer0_.FIRST as FIRST0_0_, customer0_.LAST as LAST0_0_, customer0_.STREET as STREET0_0_, customer0_.CITY as CITY0_0_, customer0_.ZIP as ZIP0_0_ from CUSTOMER customer0_ where customer0_.id=?
20:22:10,284 INFO  [STDOUT] doPostLoad: Loaded Customer: Monica Smith
20:22:10,294 INFO  [STDOUT] -- CustomerDAOBean.merge()
20:22:10,304 INFO  [STDOUT] Hibernate: select customer0_.id as id0_, customer0_.STATE as STATE0_0_, customer0_.FIRST as FIRST0_0_, customer0_.LAST as LAST0_0_, customer0_.STREET as STREET0_0_, customer0_.CITY as CITY0_0_, customer0_.ZIP as ZIP0_0_ from CUSTOMER customer0_ where customer0_.id=?
20:22:10,304 INFO  [STDOUT] doPostLoad: Loaded Customer: Monica Smith
20:22:10,304 INFO  [STDOUT] doPreUpdate: About to update Customer: Monica Burke
20:22:10,314 INFO  [STDOUT] Hibernate: update CUSTOMER set STATE=?, FIRST=?, LAST=?, STREET=?, CITY=?, ZIP=? where id=?
20:22:10,314 INFO  [STDOUT] doPostUpdate: Updated Customer: Monica Burke
20:22:10,324 INFO  [STDOUT] -- CustomerDAOBean.findByLastName(id)
20:22:10,604 INFO  [STDOUT] Hibernate: select customer0_.id as id, customer0_.STATE as STATE0_, customer0_.FIRST as FIRST0_, customer0_.LAST as LAST0_
, customer0_.STREET as STREET0_, customer0_.CITY as CITY0_, customer0_.ZIP as ZIP0_ from CUSTOMER customer0_ where (customer0_.LAST=? )
20:22:10,604 INFO  [STDOUT] doPostLoad: Loaded Customer: Bill Burke
20:22:10,604 INFO  [STDOUT] doPostLoad: Loaded Customer: Monica Burke
20:22:10,624 INFO  [STDOUT] -- CustomerDAOBean.delete()
20:22:10,634 INFO  [STDOUT] doPreRemove: About to delete Customer: Bill Burke
20:22:10,644 INFO  [STDOUT] doPreRemove: About to delete Customer: Monica Burke
20:22:10,644 INFO  [STDOUT] Hibernate: delete from CUSTOMER where id=?
20:22:10,644 INFO  [STDOUT] doPostRemove: Deleted Customer: Bill Burke
20:22:10,644 INFO  [STDOUT] Hibernate: delete from CUSTOMER where id=?
20:22:10,644 INFO  [STDOUT] doPostRemove: Deleted Customer: Monica Burke

Thus you can see how the callbacks on the entity bean wrap the interaction with the database.