Application Startup / Shutdown Callbacks

Developers often need to execute code at the point that an application is initializing or shutting down. The Servlet spec has supported this case for many years via ServletContextListener notifications. However, the EJB Specification has never offered equivalent functionality. The addition of Singleton EJB 3.1 components allows us to fill that gap by leveraging the Singleton life cycle to receive application startup and shutdown callbacks.

Here's a simple example :

   1:  @Startup
   2:  @Singleton
   3:  public class FooBean {
   4:   
   5:    @PostConstruct 
   6:    void atStartup() { ... }
   7:   
   8:    @PreDestroy
   9:    void atShutdown() { ... }
  10:   
  11:  }

The @Startup annotation forces Singleton initialization to occur eagerly during the application startup sequence. After any injection has taken place, the container calls the bean's @PostConstruct method. Singleton @PostConstruct methods have access to the full set of container services : other EJB components, JPA EntityManagers, the TimerService, etc. This allows a rich set of business logic to be performed during initialization. In addition, unlike other EJB component types, any container-managed transaction behavior for the bean applies to a Singleton's @PostConstruct method.

Independent of whether a Singleton is initialized eagerly or lazily, the bean instance can request a shutdown notification by defining a @PreDestroy method. Since the Singleton bean instance life cycle is tied to the container's life cycle, this method will be called when the application is shutting down.

Comments:

Thanks

Posted by James on January 19, 2010 at 12:26 AM EST #

Hello Ken,
It is a nice blog. Thanks for the information you share with us. I am wondering do you plan to add some vendor independant way (by annotations preferred) to defined stateles session bean pool parameters?

Yours sincerely

Posted by Serdar Serpen on February 16, 2010 at 01:50 AM EST #

Hi Serdar,

Thanks for your comment. Pool/Cache configuration is still an area that is left up to vendors to differentiate their implementations. It's possible a future version of the specification could attempt to standardize this area, but this tuning doesn't lend itself as well to an annotation-based approach since it's typically a post-deployment activity that shouldn't be dependent on recompilation.

Posted by Ken Saks on February 16, 2010 at 05:16 AM EST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Ken Saks is the Spec Lead for Enterprise JavaBeans (EJB) 3.1 and a Senior Staff Engineer in the Java Platform, Enterprise Edition team at SUN.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today