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
Solved! Go to Solution.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
I did try to use that you suggested, however cannot work and throws the same exception: the OSGI service is nullpointer.
Views
Replies
Total Likes
Which OSGI service is null?
Jörg
Views
Replies
Total Likes
private CustomerService getCustomerService() { BundleContext ctx = FrameworkUtil.getBundle(this.getClass()) .getBundleContext(); ServiceReference serviceReference = ctx .getServiceReference(CustomerService.class.getName()); return CustomerService.class.cast(ctx.getService(serviceReference)); }
Views
Replies
Total Likes
Look at this stackoverflow thread:
http://stackoverflow.com/questions/24570288/debug-osgi-component-activation-deactivation-issue
It touches upon some of the concepts here.
Views
Replies
Total Likes
Also does your OSGi service work when called in other places?
Views
Replies
Total Likes
sure, that work except JVM shutdown case.
Views
Replies
Total Likes