Hi Scott,

The SCR annotations are interpreted based on the context of where they are 
applied. This is described in the JavaDoc for the annotations, and the DS spec. 

Because you have declared your SCR reference as a final field this means that 
the SCR will be told to update your collection using add/remove rather than 
injecting a populated collection. It is therefore up to you to ensure that the 
collection is thread safe (so in this case ArrayList is a bad choice!). My 
default in this scenario is to use a CopyOnWriteArrayList. 

If instead you make the field non final then you do not need to initialise it. 
SCR will then be configured to inject a fully populated thread-safe collection 
and you can use it without fear of invalid state or 
ConcurrentModificationException. 

Another option would be to declare bind/unbind methods and to make the changes 
to the collection yourself. 

I hope this helps. 

Tim

Sent from my iPhone

> On 20 Jul 2017, at 21:49, Leschke, Scott <slesc...@medline.com> wrote:
> 
> If I have a reference in an SCR component like the following,
>  
> @Reference(policy = ReferencePolicy.DYNAMIC)
> private final Collection<MyServiceType> factories = new 
> java.util.ArrayList<>();
>  
> Since the collection is updated by the runtime as services come and go, I 
> assume it’s prudent to synchronize
> on the collection when iterating over it whereas in Blueprint I got the 
> impression this wasn’t necessary.
>  
> Would that be correct?
>  
> Scott Leschke
> _______________________________________________
> OSGi Developer Mail List
> osgi-dev@mail.osgi.org
> https://mail.osgi.org/mailman/listinfo/osgi-dev
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to