Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OSGi fundamentals

OSGi fundamentals

Slides from my Liferay Symposium France 2016 talk aiming to explain the fundamental concepts behind OSGi (structured around Modularity Maturity Model) and what makes it the only true modular technology in Java world.

Avatar for Milen Dyankov

Milen Dyankov

June 02, 2016
Tweet

More Decks by Milen Dyankov

Other Decks in Programming

Transcript

  1. " When I use a word," Humpty Dumpty said, in

    rather a scornful tone, " it means just what I choose it to mean - neither more nor less."
  2. Modularity Maturity Model proposed by Dr Graham Charters at the

    OSGi Community Event 2011 Level 1 Ad Hoc nothing Level 2 Modules decoupled from artifact Level 3 Modularity decoupled from identity Level 4 Loose-Coupling decoupled from implementation Level 5 Devolution decoupled from ownership Level 6 Dynamism decoupled from time
  3. Level 1 Ad Hoc nothing Level 2 Modules decoupled from

    artifact Level 3 Modularity decoupled from identity Level 4 Loose-Coupling decoupled from implementation Level 5 Devolution decoupled from ownership Level 6 Dynamism decoupled from time Level 7 Peter Kriens only available to people who are Peter Kriens Modularity Maturity Model proposed by Dr Graham Charters at the OSGi Community Event 2011
  4. Modularity Maturity Model proposed by Peter Kriens in foreword to

    “Java Application Architecture” Level 1 Ad Hoc Level 2 Modules Level 3 Modularity Level 4 Loose-Coupling Level 5 Devolution Level 6 Dynamism Unmanaged / chaos Managing dependencies Proper isolation Minimize coupling Service-oriented architecture
  5. Level 1 Monolith Level 2 Composite Level 3 Containers Level

    4 Discovery Level 5 JuServices Buzzword compliant Modularity Maturity Model
  6. Level 1 Monolith Unaware of own dependencies Level 2 Composite

    Aware of infrastructural dependencies Level 3 Containers Aware of functional dependencies Level 4 Discovery Aware of functional requirements Level 5 Adapts to changing requirements JuServices Buzzword compliant Modularity Maturity Model
  7. Level 1 Monolith Level 2 Composite Level 3 Containers Level

    4 Discovery Level 5 JuServices Buzzword compliant Modularity Maturity Model
  8. Level 1 Monolith Level 2 Composite Level 3 Containers Level

    4 Discovery Level 5 JuServices Buzzword compliant Modularity Maturity Model
  9. Buzzword compliant Modularity Maturity Model JuServices Level 1 Monolith Level

    2 Composite Level 3 Containers Level 4 Discovery Level 5
  10. Buzzword compliant Modularity Maturity Model JuServices Level 1 Monolith Level

    2 Composite Level 3 Containers Level 4 Discovery Level 5 OSGi
  11. Product Entity Entity Entity Entity Entity Entity Offer Offer Offer

    Offer Offer Offer Offer Offer Offer Offer Offer
  12. Application Artifact Artifact Artifact Artifact Artifact Artifact Export Export Export

    Export Export Export Export Export Export Export Export
  13. Foo Level 2 decoupled from artifact Manifest-Version: 1.0 Bundle-ManifestVersion: 2

    Bundle-Name: Foo Bundle-SymbolicName: com.foo Bundle-Vendor: Foo, Inc. Bundle-Version: 1.0.0 . . .
  14. Buzzword compliant Modularity Maturity Model JuServices OSGi Level 1 Monolith

    Level 2 Composite Level 3 Containers Level 4 Discovery Level 5 OK!
  15. Level 3 decoupled from identity Export-Package ::= export ( ','

    export)* List of packages (Java package + version) within this jar file that external code is allowed to access! Foo Me
  16. Level 3 decoupled from identity Export-Package:\ com.standard.power.plugs;\ version=”1.0.0”,\ com.foo.power.plugs.shop;\ version=”2.3.0”;\

    uses:=”com.standard.power.plugs”,\ ... Export-Package ::= export ( ',' export)* List of packages (Java package + version) within this jar file that external code is allowed to access! Foo Me
  17. Level 3 decoupled from identity Import-Package ::= import ( ','

    import )* List of packages (Java package + version-range) that classes in this jar file expect other bundles to provide! Foo Me
  18. Level 3 decoupled from identity Import-Package:\ com.standard.power.plugs;\ version=”[1,2)”,\ com.foo.power.plugs.shop;\ version=”2.3.0”,\

    com.other.power.plugs.shop;\ version=”1.5.0”,\ ... Foo Me Import-Package ::= import ( ',' import )* List of packages (Java package + version-range) that classes in this jar file expect other bundles to provide!
  19. Buzzword compliant Modularity Maturity Model JuServices OSGi Level 1 Monolith

    Level 2 Composite Level 3 Containers Level 4 Discovery Level 5 OK! OK!
  20. Level 4 decoupled from implementation Artifact Artifact Requirement Need to

    connect device to power outlet! Capability Can connect device to power outlet! RESOLVER
  21. Level 4 decoupled from implementation Artifact Artifact Requirement Need to

    connect device to power outlet! Capability Can connect device to power outlet! RESOLVER Require-Capability: power.supply;\ filter:=”(& (voltage>110)\ (voltage<230))”,\ electrician;\ filter:=”(& (price<50)\ (certified=yes))”
  22. Level 4 decoupled from implementation Artifact Artifact Requirement Need to

    connect device to power outlet! Capability Can connect device to power outlet! RESOLVER Require-Capability: power.supply;\ filter:=”(& (voltage>110)\ (voltage<230))”,\ electrician;\ filter:=”(& (price<50)\ (certified=yes))” org.osgi.framework.system.capabilities.extra= power.supply;voltage:Long=220
  23. Level 4 decoupled from implementation Artifact Artifact Requirement Need to

    connect device to power outlet! Capability Can connect device to power outlet! RESOLVER Require-Capability: power.supply;\ filter:=”(& (voltage>110)\ (voltage<230))”,\ electrician;\ filter:=”(& (price<50)\ (certified=yes))” org.osgi.framework.system.capabilities.extra= power.supply;voltage:Long=220 Provide-Capability: electrician;price:Long=10;certified=yes
  24. Buzzword compliant Modularity Maturity Model JuServices OSGi Level 1 Monolith

    Level 2 Composite Level 3 Containers Level 4 Discovery Level 5 OK! OK! OK!
  25. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service manifest manifest OSGi service registry REGISTER REGISTER manifest Interface Service 1 . . . Service N
  26. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service manifest manifest OSGi service registry REGISTER USE manifest Interface Service 1 . . . Service N REGISTER
  27. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service OSGi service registry SERVLET SERVLET GET GET REGISTER REGISTER HTTPServiceImpl HTTP Service HTTPServiceImpl REGISTER manifest
  28. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service manifest OSGi service registry HTTP Servlet REGISTER SERVLET SERVLET GET (when needed) HTTPServiceImpl Servlet1 (path=”/1”) Servlet2 (path=”/2”) REGISTER
  29. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service Register OSGI service (manually) SINGLETON PROPERTIES CONTRACT
  30. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service Use an OSGI service (manually) DONE WITH THE SERVICE START USING THE SERVCE FIND A SERVCE
  31. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service Declarative services and Service Component Runtime (SCR)
  32. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service /OSGI-INF/MyComponent.xml file: <?xml version="1.0" encoding="UTF-8"?> <scr:component name=”MyComponent” xmlns:scr=”http://www.osgi.org/xmlns/scr/v1.2.0”> <implementation class=”com.liferay.MyComponent”/> </scr:component> Manifest header: Service-Component: OSGI-INF/MyComponent.xml
  33. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service @Component( configurationPid=”hello.component.pid”, configurationPolicy=ConfigurationPolicy.OPTIONAL, enabled=true, immediate=true, name=”HelloComponent”, property={ ”vendor.name=Liferay”, ”other.property=value” }, service={HelloService.class, OtherService.class} ) public class HelloComponent implements MultiService { . . .
  34. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service @Activate void open(Map<String,?> properties) { } @Deactivate void close() { } @Modified void modified(Map<String,?> properties) { }
  35. Level 5 decoupled from ownership & time JuServices Artifact Artifact

    REGISTRY Service Service Service Service @Reference( cardinality=ReferenceCardinality.MANDATORY, name = “LogService”, policy = ReferencePolicy.STATIC, policyOption=ReferencePolicyOption.RELUCTANT, service = LogService.class, target = “(vendor.name=Liferay)”, unbind = “unsetLog”, updated = “updatedLog” ) void setLog( LogService log) { } void unsetLog( LogService log) { } void updatedLog( Map<String,?> ref ) { }
  36. Buzzword compliant Modularity Maturity Model JuServices OSGi Level 1 Monolith

    Level 2 Composite Level 3 Containers Level 4 Discovery Level 5 OK! OK! OK! OK!
  37. Which results in application Agility The essence of modularity is

    Not knowing Which enforces optimization for Predictability