cschneider / Karaf-Tutorial

http://cschneider.github.io/Karaf-Tutorial/
Apache License 2.0
271 stars 305 forks source link

Karaf Tutorial Part 1 - Is it possible to run tasklist-ui bundle independently from tasklist-persistence bundle? #42

Closed laurentkvb closed 6 years ago

laurentkvb commented 6 years ago

Hello, I would like to develop an application using the Apache Karaf container combined with a OSGi based application. I would like to know if it is possible to run the "TaskListServlet.java" that is located in the Tasklist-ui module independently from the TaskService, which is from the tasklist-persistence module. For instance, I want to be able to run the TaskListServlet when the persistence is not available. Instead it would show you a custom page or error message.

I don't know how it would look like, but maybe like initializing the 'taskService" only when the tasklist-persistence is available as bundle.

Thank you in advance.

laurentkvb commented 6 years ago

Currently, I have managed to get it working in the following way: BundleContext ctx = FrameworkUtil.getBundle(getClass()).getBundleContext(); // @Inject // @OsgiService TaskService taskService; -> global variable .... ServiceReference<?>[] refs = null; try { refs = ctx.getServiceReferences(TaskService.class.getName(), null); } catch (InvalidSyntaxException invalidSyntaxException) {

    }

    if (refs != null) {
        TaskService provider = null;
        for (ServiceReference ref : refs) {
            if (ctx.getService(ref) instanceof TaskService) {
                provider = (TaskService) ctx.getService(ref);
                taskService = provider;
            }
        }
    } else {
        taskService = null;
        writer.println("<h1>Disconnect from the tasklist-persistence plugin </h1>");
    }

Is this the usual way to use a bundle from another plugin? (tasklist-persistence -> using tasklist-ui)

cschneider commented 6 years ago

I think you can use an optional service reference in blueprint. This way your servlet also comes up when the service is not present and can then decide what to do. Try @OsgiService(required=false)

Btw. I just updated the example to the newest version of the blueprint maven plugin and switched to the new annotations.

laurentkvb commented 6 years ago

Thanks for your response. I have switched over to the newest version of the project and managed to get it working with @Reference(availability = Availability.OPTIONAL) annotation. So, it is currently coded that the "task-persistence ui" bundle is an optional service. The call to the "TaskService" method call is now surrounded with a try-catch to handle the ServiceUnavailableException.

Is there a better way to handle the detection of the service that unavailable instead of catching the exception?

cschneider commented 6 years ago

Not sure it this is possible with blueprint. In my newer projects I always use declarative services(DS). There an optional dependency is simply null if it is not present.

Btw. DS is also a lot more dynamic. It starts your component only if all mandatory services are available and stops it again when one disappears. So your bundles are never stuck in graceperiod.

Christian

2018-03-22 12:24 GMT+01:00 Laurent Kleering vb notifications@github.com:

Thanks for your response. I have switched over to the newest version of the project and managed to get it working with @reference https://github.com/reference(availability = Availability.OPTIONAL) annotation. So, it is currently coded that the "task-persistence ui" bundle is an optional service. The call to the "TaskService" method call is now surrounded with a try-catch to handle the ServiceUnavailableException.

Is there a better way to handle the detection of the service that unavailable instead of catching the exception?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/cschneider/Karaf-Tutorial/issues/42#issuecomment-375270465, or mute the thread https://github.com/notifications/unsubscribe-auth/AAdk6IR7H1i61RqEvKBd2KdbEONDY2iMks5tg4oJgaJpZM4S0FBI .

-- -- Christian Schneider http://www.liquid-reality.de

Computer Scientist http://www.adobe.com

laurentkvb commented 6 years ago

Thank for the information, it has given me more understanding of Services & Components. This issue can be closed. :+1: