java - Delaying service registration in OSGi. How to do it properly -
when working declarative services in osgi, encountered special usecase:
sometimes want ds component register service long time operation has completed. not problem since services registered after activate method has finished. however, necessary open thread inside activate method (in order avoid thread blocking) , register service once thread has finished work, e.g.
@component public class myclass implements myservice { private executorservice executor = executors.newcachedthreadpool(); @activate public void start(final bundlecontext context) { executor.execute(new runnable() { @override public void run() { ... // doing long context.registerservice(myservice.class, myclass.this, null); } }); } }
we came util class doing (and covering issues component being stopped while thread still running etc.). there better way such thing? if not, wouldn't convenient add such util class framework?
best regards, mike
ds not support use-case directly. solution using bundlecontext
register service explicitly best available.
you need careful prevent ds automatically registering service activate method completes. should setting provide
attribute on @component
annotation empty array:
@component(provide = {})
alternatively, don't have component class directly implement service interface; use inner class.
Comments
Post a Comment