28
Why is OSGi dynamic ? Christian Campo, compeople AG EclipseCon Europe 2012,

Why is os gi dynamic.pptx

Embed Size (px)

DESCRIPTION

Why is OSGi dynamic talk from EclipseCon Europe 2012

Citation preview

  • 1. Why is OSGi dynamic ?Christian Campo, compeople AGEclipseCon Europe 2012,

2. Overview OSGi is... Lifecycle Services / Extensions Sample Workaround Otherscompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 2 3. OSGi is ... ... a module system and service platform for the Java programming language that implements a complete and dynamic component model. Applications or components (coming in the form of bundles for deployment) can be installed, started, stopped, updated, and uninstalled without requiring a reboot. The service registry allows bundles to detect the addition of new services, or the removal of services, and adapt accordingly. *Wikipedia 2012compeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012|3 4. OSGi is ... BundleServiceExtension started startedinstalled stopped stoppeduninstalled installed uninstalledcompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ?| 12.09.2012 | 4 5. OSGi Lifecycle ... BundleServicestoppedstartedExtensionuninstalledinstalled uninstalled stopped startedcompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 5 6. OSGi Service Usage ... BundleIService started BundleBundleServiceMyComponent ?stoppedstartedserviceRef stopped started startedcompeople AG| EclipseCon Europe 2012| Why are OSGi dynamic ?| 12.09.2012 | 6 7. ExtensionsExtPoint? BundleBundleExtensionMyComponent createExecRefuninstalledinstalled uninstalled stopped started startedcompeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 |7 8. Lazy activation BundleIService started stopped BundleBundleServiceMyComponentstoppedserviceRef stopped started stoppedWho starts this bundle so the Service can beaccessed ?compeople AG| EclipseCon Europe 2012| Why are OSGi dynamic ?| 12.09.2012 | 8 9. Which means ... A service references can become invalid anytime during runtime Services are uncoupled from their consumer (no dependency) Services need to be started before the lookup Bundles are started lazy by default you cannot rely on a bundle being activecompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 9 10. Sample BundleISearchService started BundleBundleSearchServiceMySearchstartedsearch started startedcompeople AG| EclipseCon Europe 2012| Why are OSGi dynamic ? | 12.09.2012 | 10 11. Which means ... (API approach)MySearch ref = context.getServiceReference(ISearchService.class.getName()); if (ref!=null) {ISearchService search = (ISearchService)context.getService(ref);if (search!=null) { search.findByName("Bill"); ... ... // is search still valid here ??? } context.ungetService(ref); }compeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 11 12. Which means ....(ServiceTracker)MySearch ServiceTracker tracker =new ServiceTracker(ctx, ISearchService.class.getName(), null);tracker.open();ISearchService search = ((ISearchService )tracker.getService()); if (search!=null) { search.findByName("Bill"); ... ... // is search still valid here ??? }compeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 12 13. Which means ...(Declaritive approach) Note: The declarative approach requires that DS controls the lifecyleof the target component. (MySearch).If that lifecycle is out of your control, you need an API.Both components should be DS to make sense.compeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 13 14. Which means ... (Declaritive approach) public class MySearch {public void setCustomerSearch(ISearchService search) { this.search = search; } public Customer[] listCustomer(String name) {return search.findByName(name); } public Customer findCustomer(String number) {return search.findByNumber(number); } public void unsetCustomerSearch(ISearchService search) {this.search = null; } }compeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 14 15. Which means ... (e4 approach) public class MySearch { @Inject private ISearchService search; public Customer[] listCustomer(String name) {return search.findByName(name); } public Customer findCustomer(String number) {return search.findByNumber(number); } }compeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 15 16. That things can break ... NPE when accessing service that are injected Service was not started Service was stopped No bundle in the setup implements the Service Service instance exists, but Service-Bundle is stopped Lazy activation does not work once a bundle is stopped At startup no way to check that all dependencies are there.compeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 16 17. Are you ready ... ? Is our code always ready that a reference to a service instance is no longer valid ? In your RCP application, do you often stop and start bundles ? Do you swap-in a new Service implementation at runtime ? OR do you need that dynamic ?compeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 17 18. Eclipse IDE... A typical usecase for dynamic OSGi is installation of bundles in the Eclipse IDE Did you notice that the "Apply changes" option from Eclipse 3.7 is gone in 3.8 and 4.2 ? Did it ever work for you ? Test with Eclipse IDE install bundle in 3.7 and in 4.2 (no apply in 4.2) stop a bundle in the console and see how the Eclipse IDE reactscompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 18 19. stopped egit.uicompeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 19 20. Not everyone wants/needs dynamic ... Often there is nothing reasonable to do when an injected service is not there. SearchDialog without SearchService SearchService without DatabaseConnectorService Car without BrakeServicecompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 20 21. Work-arounds Frameworks on top of OSGi implement their own way of starting bundles EAGER OSGi based servers want to start everything immediately Applications manually start all necessary bundles and service at startup and never stop themcompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 21 22. The advantage ... A missing service is not "normal", dont try to handle it. Detect missing components at startup and fail instantly Services dont appear at some time. They are either there or not. A Service cannot disappear while you are using it.compeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 22 23. There are exceptions... Some Services need the dynamic (=flexibility) i.e. change ServiceImplementation if you switch from online to offline So document that decision and add the additional checkscompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 23 24. Which means ... (API approach) ref = context.getServiceReference(ICustomerSearch.class.getName()); if (ref!=null) {ICustomerSearch search = (ICustomerSearch)context.getService(ref);// if (search!=null) {search.findByName("Bill");...... // is search is valid here !!! // } context.ungetService(ref); // ??? }compeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 24 25. Which means ... (e4 approach) public class MySearch { @Inject private ISearchService search; public Customer[] listCustomer(String name) {return search.findByName(name); } public Customer findCustomer(String number) {return search.findByNumber(number); } }compeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 25 26. Others ... OSGi !=!= no dynamic lifecycle per componentcompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 26 27. OSGi Modules as Bundles cool Bundles /w public API & private Impl cool cool OSGi Service Extensions cool Lazy Activationbe careful Start / Stop Services on condition use with care Ability to stop Bundles at Runtime avoid Ability to replace Bundles at Runtimeavoidcompeople AG| EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012| 27 28. Q&Acompeople AG | EclipseCon Europe 2012 | Why are OSGi dynamic ? | 12.09.2012 | 28