OSGi Community Event 2013 (http://www.osgi.org/CommunityEvent2013/Schedule)
Lightning Talk
ABSTRACT
A fun look at some of the traps and pitfalls that people come across when working with OSGi, along with suggestions on how you should deal with these cases in point.
5. WTF?
OSGi Compendium, section 112.5.10 Dynamic Service
Replacement
“SCR must first bind a replacement target service and then
unbind the outgoing service” (emphasis added)
6. Beware this Pattern
@Component
public class ExampleComponent {
private MyService svc;
@Reference(dynamic = true)
public void setMyService(MyService svc) {
this.svc = svc;
}
public void unsetMyService(MyService svc) {
this.svc = null; // OOOPS!
}
}
7. Better...
@Component
public class ExampleComponent {
private MyService svc;
@Reference(dynamic = true)
public synchronized void setMyService(MyService svc) {
this.svc = svc;
}
public synchronized void unsetMyService(MyService svc) {
if (this.svc == svc)
this.svc = null;
}
}
8. Even Better...
@Component
public class ExampleComponent {
private final AtomicReference<MyService> svcRef
= new AtomicReference<>();
@Reference(dynamic = true)
public void setMyService(MyService svc) {
svcRef.set(svc);
}
public void unsetMyService(MyService svc) {
svcRef.compareAndSet(svc, null);
}
}
13. Blacklisting
EventHandlers have to return quickly
If they don’t... BLACKLIST! No more events for you.
Humans are slow and stooopid.
Turn off blacklisting, OR use printf-style debugging