Show TOC

Procedure documentationBinding the Life Cycle of Application and Business Components Locate this document in the navigation structure

 

The EJB Container creates and manages the beans' instances. If your application logic needs to be aware of the life cycle events, you have to provide callback methods so that the EJB Container calls them at the appropriate moment. In this case the life cycle events of the beans do not depend on the life cycle of the application.

According to the Java EE specifications, the life cycle of the business components is not bound to the application life cycle and there is no standard way to notify a business component about a life cycle event of the application. However, there are scenarios that need specific life cycle management to be executed at application start and/or stop. That is why the SAP implementation of the EJB specification introduces the EJB Startup and Shutdown Hooks. This feature provides a means of executing business methods of stateless session beans at the start and/or stop of the application.

To activate the hooks, you have to use the corresponding annotation: com.sap.ejb.annotations.AppStartup and com.sap.ejb.annotations.AppShutdown. The EJB Container automatically invokes the annotated business methods of the bean at the start and/or stop of the application:

  • In the startup case, the hook is executed just before the application becomes available for clients.

  • In the shutdown case, the hook is executed just after making all the EJB components within the application unavailable.

The annotated methods behave as normal business methods according to the EJB 3.0 specification.

  • In such business methods of session beans, other components and resources can be looked up using the standard way with a string starting with: java:comp/env.

  • You can specify a container-managed transaction using any of the EJB 3.0 transaction attributes, except MANDATORY. When you have Startup or Shutdown Hooks, there is no client's transaction and, according to the EJB specification, if the MANDATORY attribute is used, an exception is thrown.

  • You can use the security context and it is set to the default one, unless the run-as property is set for the bean.

Procedure

Setting EJB Hooks using annotations

To hook a business method to be executed at the start and/or stop of the application, use the @AppStartup or @AppShutdown annotation respectively. You can apply these annotations to only one business method each for a single bean including the methods of its superclasses and the overwritten methods. The annotated methods have to be public, do not have to accept any arguments, and have to be of type void.

Syntax Syntax

  1. @Stateless
    public class MyBean implements MyInterface {
    
    	@AppStartup
    	public void startupHook() {
    //business functionality at application startup
    }
    	@AppShutdown
    	public void shutdownHook() {
    //business functionality at application stop
    }
            …
    }
    
End of the code.
Setting EJB Hooks using the ejb-j2ee-engine.xml

You can also specify an EJB hook using the ejb-j2ee-engine.xml deployment descriptor:

Syntax Syntax

  1. <enterprise-beans>
    	<enterprise-bean>
    		...
    		<app-startup-method>
    			<method>methodStartName</method>
    		</app-startup-method>
    		<app-shutdown-method>
    			<method>methodStopName</method>
    		</app-shutdown-method>
    		...
End of the code.

Note Note

The settings made in the ejb-j2ee-engine.xml file overwrite the annotated methods.

End of the note.