You may also experiment with using a "policy = ReferencePolicy.DYNAMIC"
property on the reference annotation.  I usually do this on a set of
bind/unbind methods to avoid all the restarting when the referenced
services come and go.  You'd just need to be a bit more careful with
synchronizing the usage of the referenced services within your component
class when the referenced service could come or go at any time.

For example, I usually use something like this:

    private List<String> svcReflist = new ArrayList<>();

    @Reference(cardinality=ReferenceCardinality.OPTIONAL, policy =
ReferencePolicy.DYNAMIC, policyOption=ReferencePolicyOption.GREEDY)
    protected void bindHandlerFactory(IHandlerFactory svc) {
        svcReflist.add(svc);
    }
    protected void unbindHandlerFactory(IHandlerFactory svc) {
        svcReflist.remove(svc);
    }

Regards,
-Eric

On Fri, Sep 21, 2018 at 6:01 PM David Daniel <david.daniel.1...@gmail.com>
wrote:

> Thank you David and Eric,
>   So what I did was set the ReferncePolicyOption to greedy so the service
> is restarted everytime a new item is added to the List.  This seems to
> match your second suggestion of adjusting the code to take account of
> dynamic referenced service appearance although it seems like a lot of
> restarting eg. if I have 50 web endpoints the component will restart 50
> times.  I did not completely understand the first suggestion.  I do know at
> buildtime the size of the list and the items in the list.  Are you saying
> that there is a way to tell configadmin the minimum number of items in the
> list before starting the component.
>
> Thanks,
>   David
>
>
> On Fri, Sep 21, 2018 at 8:34 PM David Jencks <david.a.jen...@gmail.com>
> wrote:
>
> > startlevel is not an appropriate solution for this problem.
> >
> > Your original code doesn’t show the use of the service tracker.  Your
> > replacement code appears to assume that every referenced service is
> already
> > present at activation.  In order to assure this I suggest you supply your
> > component with a mandatory configuration specifying
> > <targetname>.cardinality.minimum, or adjust your code to take account of
> > dynamic referenced service appearance and disappearance.  I don’t know
> how
> > the new R7 features work with this property but I’d be surprised if
> > constructor injection didn’’t take it into account.
> >
> > david jencks
> >
> > > On Sep 21, 2018, at 5:16 PM, David Daniel <david.daniel.1...@gmail.com
> >
> > wrote:
> > >
> > > Adding a cardinality did not seem to solve the issue.  It appears to be
> > an
> > > issue with the startlevel as stopping and starting the bundle worked
> and
> > > the only bundle that started before it was added.  Is there an example
> of
> > > the new R7 startlevel stuff in felix.  I would like to give the bundle
> I
> > > want loaded last a start level of 5 or something.
> > >
> > > Thanks,
> > >  David
> > >
> > >   45|Active     |    1|web.server (1.0.0.SNAPSHOT)|1.0.0.SNAPSHOT
> > >   46|Active     |    1|web.ui.site (1.0.0.SNAPSHOT)|1.0.0.SNAPSHOT
> > >   47|Active     |    1|web.ui.qr (1.0.0.SNAPSHOT)|1.0.0.SNAPSHOT
> > > g! stop 45
> > >
> > >        20:11:57
> > > 20:12:09.153 [pipe-stop 45] INFO  Events.Service.web.server:? -
> > > ServiceEvent UNREGISTERING [com.mobigov.web.server.HandlerBuilder]
> > > 20:12:09.164 [ixDispatchQueue] INFO  Events.Bundle.web.server:? -
> > > BundleEvent STOPPED
> > > g! start 45
> > >
> > >         20:12:09
> > > 20:12:16.266 [pipe-start 45] INFO  Events.Service.web.server:? -
> > > ServiceEvent REGISTERED [com.mobigov.web.server.HandlerBuilder]
> > > 20:12:16.285 [pipe-start 45] INFO
> > c.mobigov.web.server.HandlerBuilder:31 -
> > > Adding Handler for: /communication
> > > 20:12:16.286 [pipe-start 45] INFO
> > c.mobigov.web.server.HandlerBuilder:31 -
> > > Adding Handler for: /web.ui
> > > 20:12:16.286 [pipe-start 45] INFO
> > c.mobigov.web.server.HandlerBuilder:31 -
> > > Adding Handler for: /qr
> > > 20:12:16.287 [pipe-start 45] INFO
> > c.mobigov.web.server.UndertowServer:42 -
> > > Directory exists
> > > 20:12:16.299 [ixDispatchQueue] INFO  Events.Bundle.web.server:? -
> > > BundleEvent STARTED
> > >
> > > On Thu, Sep 20, 2018 at 11:30 PM Eric Norman <eric.d.nor...@gmail.com>
> > > wrote:
> > >
> > >> Are those the SCR annotations, or the newer official OSGi declarative
> > >> service annotations?
> > >>
> > >> In either cause, I suppose you'd need to to adjust the cardinality
> > property
> > >> on the @Reference annotation to tell it you want multiple service
> > >> references.
> > >>
> > >> Regards,
> > >> -Eric
> > >>
> > >> On Thu, Sep 20, 2018 at 7:42 PM David Daniel <
> > david.daniel.1...@gmail.com>
> > >> wrote:
> > >>
> > >>> I had switched form a servicetracker to constructor injection of a
> list
> > >>> with scr 2.1.6 . The servicetracker got all the services correctly
> but
> > >> the
> > >>> list is only getting 1.  I was wondering if I did not understand
> > >> something
> > >>> or if there was an open bug.
> > >>>
> > >>> Thanks for any help,
> > >>>  David
> > >>>
> > >>> From
> > >>>
> > >>>        try {
> > >>>            mBundleContext = bc;
> > >>>            String filterString = "(" + Constants.OBJECTCLASS + "=" +
> > >>> IHandlerFactory.class.getName() + ")";
> > >>>            Filter filter = bc.createFilter(filterString);
> > >>>            tracker = new ServiceTracker(bc, filter, this);
> > >>>            tracker.open();
> > >>>
> > >>>        } catch (InvalidSyntaxException e) {
> > >>>            if (log != null) {
> > >>>                log.log(LogService.LOG_ERROR, e.getMessage());
> > >>>            }
> > >>>        }
> > >>>
> > >>> To
> > >>>
> > >>> @Activate
> > >>> public HandlerBuilder(@Reference List<IHandlerFactory>
> > handlerFactories)
> > >> {
> > >>> pathHandler.addExactPath("/", new Forwarding());
> > >>> pathHandler.addExactPath("/manifest.json", new Manifest());
> > >>> for(IHandlerFactory handler : handlerFactories) {
> > >>> if (handler.Type().equals("path")) {
> > >>> if (handler instanceof IPathHandlerFactory) {
> > >>> logger.info("Adding Handler for: " + ((IPathHandlerFactory)
> > >>> handler).Prefix
> > >>> ());
> > >>> pathHandler.addPrefixPath(((IPathHandlerFactory) handler).Prefix(),
> > >>> handler.
> > >>> Get());
> > >>> }
> > >>> }
> > >>> }
> > >>> }
> > >>>
> > >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
> > For additional commands, e-mail: users-h...@felix.apache.org
> >
> >
>

Reply via email to