Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [e4-dev] OSGi, Contexts and DI


> What's your take on consuming OSGi service using the whiteboard pattern?
> Should it inject a Iterable/Collection<MyService> each time the list
> changes?


That's a very good question, and while I'd strongly prefer the answer to be "yes", that might turn out to be not practical. Let's see what Simon comes up with.

As to why it might not be practical? Aside from CPU performance, it is rather hard for developers to live in the dynamic world. Let me show a naive example of a cost of consuming a single trivial service in a dynamic fashion.

Say, bundleA provides a logger services and bundleB uses the logger service.

In a simple non-dynamic world the bundleB would have something like:
        --- Code Sample 1 - non-dynamic ----------------------------------------------

        @Inject ILog log;

        public void logError(String msg) {
                log.error(msg);
        }
        ------------------------------------------------------------------------------
In a dynamic world the bundleA can be, for instance, updated, which will result in the ILog service implementation being:
        LoggerImpl1, then
        null, then
        LoggerImpl2

then the dynamically-aware code in bundleB would have to become something like:
        --- Code Sample 2 - dynamic --------------------------------------------------
        ILog log;
        List<String> savedErrors = new LinkedList<String>();

        @Inject
        synchronized public void setLogger(ILog newLog) {
                log = newLog;
                if (log != null && !savedErrors.isEmpty()) {
                        for(String tmp : savedErrors) {
                                logError(tmp);
                        }
                        savedErros.clear();
                }
        }


        synchronized public void logError(String msg) {
                if (log == null)
                        savedErrors.add(msg);
                log.error(msg);
        }
        ------------------------------------------------------------------------------

Note that complexity here is not injection-specific, but rather a consequence of the basic-ness of OSGi dynamics support. From what I read, OSGi's Blueprint is supposed to add dampening of dynamic services, but I don't think it can be added at the contexts/DI level as service providers have to be updated. Is there anything else readily available that can be used in the SDK stack to provide service dampening / request caching?

So, offhand, I know that I'd like the answer to be ("yes, dynamic"), but I am not sure if that turns out to be a practical answer.

What do you think? If dynamic injection of OSGi services required adding "null" checks (even for non-optional services), would that be acceptable?

Sincerely,
Oleg Besedin



From: Gunnar Wagenknecht <gunnar@xxxxxxxxxxxxxxx>
To: e4-dev@xxxxxxxxxxx
Date: 10/26/2010 06:48 AM
Subject: Re: [e4-dev] OSGi, Contexts and DI
Sent by: e4-dev-bounces@xxxxxxxxxxx





Oleg,

Am 25.10.2010 17:47, schrieb Oleg Besedin:
> In e4 injected values are dynamic; values that changed in the context
> get propagated into injected fields/methods. So, if OSGi strategy worked
> properly, the changes in the service implementations would be
> propagated. Also, we have the "@Optional" annotation which allows values
> not currently in the context to be injected as "null" and re-injected
> later when the values are added to the context.

What's your take on consuming OSGi service using the whiteboard pattern?
Should it inject a Iterable/Collection<MyService> each time the list
changes?

-Gunnar

--
Gunnar Wagenknecht
gunnar@xxxxxxxxxxxxxxx
http://wagenknecht.org/

_______________________________________________
e4-dev mailing list
e4-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/e4-dev



Back to the top