Hi Henrik,

You are confusing yourself by asking DS for a mixture of field injection, 
bind/unbind methods and having non-thread-safe code for a dynamic reference.

By applying the annotation to the field you are telling DS to inject references 
directly into your component's field. You are also telling it to use 
bind/unbind methods, which it is also doing. The volatile setting is likely 
changing the result of a race condition that was happening before, where DS 
injects a one-element collection then starts calling the bind method.

In your scenario I would strongly recommend not using field injection (you have 
bind/unbind methods) by moving the annotation to the bind method. You can then 
remove the custom bind attribute value and ensure thread safety by using a 
thread safe collection, or with appropriate synchronisation wherever the list 
is used.

I hope this helps. 

Regards,

Tim

Sent from my iPhone

> On 20 May 2017, at 21:22, Henrik Niehaus <henrik.nieh...@gmx.de> wrote:
> 
> When I use a Set, bndtools complains, that there is no unbind method,
> even though it's specified. But, in my opinion, using a Set only fixes
> the symptoms, but not the source of the problem...
> 
> Using CopyOnWriteArrayList does not make any difference.
> 
>> Am 20.05.2017 um 19:42 schrieb Ferry Huberts:
>> 
>> 
>>> On 20/05/17 18:48, Henrik Niehaus wrote:
>>> I noticed a behavior with SCR, which I don't understand. I'm using a
>>> multiple dynamic reference for a list like this:
>>> 
>>>     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy =
>>> ReferencePolicy.DYNAMIC, bind="addService", unbind="removeService")
>>>     private List<WeatherService> services = new ArrayList<>();
>> 
>> Use a Set<> i.s.o. a List<>.
>> even better would probably be CopyOnWriteArraySet, depending on your
>> usage scenario.
>> 
>>> 
>>> This works as I expect it. All 5 instances get added to the list.
>>> 
>>> At startup SCR tells me, that it would be a good idea to make this list
>>> volatile, which makes sense. So I tried that and ended up with 6
>>> instances in my list, which I don't understand. When I set a breakpoint
>>> in the bind method "addService", it turns out, that the list already
>>> contains an element when it is called the first time. One service is
>>> contained twice in the list. And it is always the service, which is
>>> added at last. Is this intended? Am I doing something wrong?
>>> 
>>> - Henrik
>>> 
>>> 
>>> _______________________________________________
>>> 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
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to