How to add shutdown hook in CQ5 | Community
Skip to main content
October 16, 2015
Solved

How to add shutdown hook in CQ5

  • October 16, 2015
  • 8 replies
  • 1493 views

Hi there,

There is a case for me: i need to persist application some data before JVM shutdown. So i program as below in OSGI bundle:

protected void activate(ComponentContext ctx) { this.bundleContext = ctx.getBundleContext(); final Set<JobBean> jobs = CreateJobServlet.jobNames; jobs.addAll(getInProgressJob()); shutDownHook(jobs); } private Set<JobBean> getInProgressJob() { return getCustomerService().getInProgressJob(); } private void shutDownHook(final Set<JobBean> jobs) { logger.info("Exec shutDownHook()........."); Runtime.getRuntime().addShutdownHook(new Thread(){ public void run(){ logger.info("Running shutdown hook before JVM complete closed......"); getCustomerService().persisJob(jobs); } }); }

However, the function shutDownHook never enter and work, no any log info in background.

Anyone know how to do that? thanks a lot

 

Brian

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

Hi Brian,

Adding a shutdown o the JVM is not best practice within AEM. Because when the JVM finally shuts down, most (all?) of the bundles have been already stopped. So instead of this shutdown hook I would rather tie it to the stop of a bundle or (preferably) to the deactivate of a service.

btw: When you are dealing with sling jobs, you don't have to deal with persisting the jobs yourself...

kind regards,
Jörg

8 replies

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

Hi Brian,

Adding a shutdown o the JVM is not best practice within AEM. Because when the JVM finally shuts down, most (all?) of the bundles have been already stopped. So instead of this shutdown hook I would rather tie it to the stop of a bundle or (preferably) to the deactivate of a service.

btw: When you are dealing with sling jobs, you don't have to deal with persisting the jobs yourself...

kind regards,
Jörg

October 16, 2015

Yes, i find that log record bundle service can't cast class. See below code:

private CustomerService getCustomerService() { BundleContext ctx = FrameworkUtil.getBundle(this.getClass()) .getBundleContext(); ServiceReference serviceReference = ctx .getServiceReference(CustomerService.class.getName()); return CustomerService.class.cast(ctx.getService(serviceReference)); }

 

As a result,Do you mean to tie this task to the deactivate method as following:

protected void deactivate(ComponentContext ctx) { this.bundleContext = null; getCustomerService().persisJob(jobs); }

thanks a lot.

October 16, 2015

I did try to use that you suggested, however cannot work and throws the same exception: the OSGI service is nullpointer.

joerghoh
Adobe Employee
Adobe Employee
October 16, 2015

Which OSGI service is null?

Jörg

October 16, 2015
private CustomerService getCustomerService() { BundleContext ctx = FrameworkUtil.getBundle(this.getClass()) .getBundleContext(); ServiceReference serviceReference = ctx .getServiceReference(CustomerService.class.getName()); return CustomerService.class.cast(ctx.getService(serviceReference)); }
smacdonald2008
Level 10
October 16, 2015

Look at this stackoverflow thread:

http://stackoverflow.com/questions/24570288/debug-osgi-component-activation-deactivation-issue

It touches upon some of the concepts here.

smacdonald2008
Level 10
October 16, 2015

Also  does your OSGi service work when called in other places?

October 16, 2015

sure, that work except JVM shutdown case.