Payara Glassfish Lifecycle Handlers

I am a newbie and wanting to implement lifecycle handler for an application that will be deployed in payara.

The application I am working on is using maven for dependency resolution, I have searched many articles online but couldn’t find any resource which would point to basic example of implementing lifecycle handlers.

I am looking to handle lifecycle events to free up resources allocated by the app as well as free up database connections on application undeploy/disable events.

Any help in this regard would be appreciated.

Thanks

Hi @neerajpayara,

An EJB with the @Singleton and @Startup annotations would allow you to execute code after the application has been deployed and just before it is undeployed, which sounds like it would meet the requirements of your problem.

Here is an example of a startup EJB:

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Startup
@Singleton
public class StartupBean {
 
  @PostConstruct
  public void setup() {
    // Add code here to execute when the application is deployed
  }

  @PreDestroy
  public void cleanup() {
     // Add code here to execute when the application is undeployed
  }
  
}

Best Regards,
James

1 Like

Thanks @JamesHillyard for the reply and a quick example, I will try it out and revert if I need anything

It worked like a charm @JamesHillyard, thank you again!

1 Like