osgi puzzlers - neil bartlett & peter kriens
DESCRIPTION
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.TRANSCRIPT
OSGi PuzzlersNeil Bartlett and Peter Kriens
Overlapping Concerns...
What Does it Print?
NOW STOP BUNDLE P1...
P1
P2Example
ComponentMyService
MyService
CKFU!(OBVIOUSLY)
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)
Beware this Pattern@Componentpublic 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! }
}
Better...@Componentpublic 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; }
}
Even Better...@Componentpublic 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); }
}
Stooopid Hoooman...
What Does It Print?
A. 0,1,2,3,4,5,6,7,8,9,...B. 1,C. 1,2,3,4,5,6,7,8,9,....D. NullPointerException
What Does It Print?
A. 0,1,2,3,4,5,6,7,8,9,...B. 1,C. 1,2,3,4,5,6,7,8,9,....D. NullPointerException
WTF?
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
You Gotta Start Me Up...
What Does It Print?
A. “Constructed...Started.”B. “Constructed...”C. NothingD. IllegalAccessExceptionE. “Started.”
What Does It Print?
A. “Constructed...Started.”B. “Constructed...”C. NothingD. IllegalAccessExceptionE. “Started.”
WTF?
DS Services are Lazy
Not an error... just waiting for a consumer!
REMEMBER:
OSGi is Your Friend!