maximize the power of osgi

41
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Condential. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Condential. Maximize the power of OSGi Carsten Ziegeler | David Bosschaert | Adobe R&D

Upload: david-bosschaert

Post on 14-Feb-2017

393 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Maximize the power of OSGi Carsten Ziegeler | David Bosschaert | Adobe R&D

Page 2: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

About David Bosschaert [email protected] @davidbosschaert

•  R&D Adobe Ireland •  Co-chair OSGi Enterprise Expert Group •  ISO/IEC JTC1 SC38 Cloud Computing committee member for Ireland •  Apache Felix, Aries PMC member and committer •  … other opensource projects

•  Cloud and embedded computing enthusiast

2

Page 3: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

About Carsten Ziegeler [email protected] @cziegeler

•  RnD Adobe Research Switzerland •  Member of the Apache Software Foundation •  VP of Apache Felix and Apache Sling •  OSGi Core Platform, OSGi Enterprise, OSGi IoT Expert Groups •  Member on the board of the OSGi Alliance •  Book / article author, technical reviewer, conference speaker

3

Page 4: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Today’s contents

§  Declarative Services

§  with Configuration Admin

§  HTTP Whiteboard

§  Coordinator

§  Subsystems

4

Image by Joadl: Lyme_Regis_harbour_02b on wikipedia

Page 5: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Declarative Services

Page 6: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Component Container Interaction

6

OSGi Service Registry

Declarative Services Blueprint

iPojo, Dependency

Manager, ….

Framework API

Page 7: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 7

No POJOs

Too slow

Dynamics not manageable

No dependency injection

Not suitable for the enterprise

No

tool

ing

OSGi Preconceptions

✔ ?!

Page 8: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

The Next Big Thing

8

Page 9: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Building Blocks

§  Components §  Services §  Module aka Bundle

9

Page 10: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Game Design

10

Game Servlet

GET

POST

HTML

Game Controller

Game Bundle

Page 11: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Game Design - API

11

public enum Level {

EASY,MEDIUM,HARD

}

public interface GameController {

Game startGame(final String name, final Level level); int nextGuess(final Game status, final int guess);

int getMax(final Level level);}

public class Game {

// game status}

Page 12: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Implementation

12

@Componentpublic class GameControllerImpl implements GameController {

...

import org.osgi.service.component.annotations.Component;

Page 13: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Configuration

13

public @interface Config {

int easy_max() default 10;int medium_max() default 50;int hard_max() default 100;

}

Range from 1 to a configurable max value per level

Configuration (Dictionary) easy.max = "8" medium.max = 40L

Page 14: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 14

private Config configuration;

@Activateprotected void activate(final Config config) {

this.configuration = config;}

public @interface Config {

int easy_max() default 10;int medium_max() default 50;int hard_max() default 100;

}

Page 15: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 15

public int getMax(final Level level) {

int max = 0;

switch (level) {case EASY : max = configuration.easy_max(); break;case MEDIUM : max = configuration.medium_max(); break;case HARD : max = configuration.hard_max(); break;

} return max;

}

Page 16: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Game Design

16

Game Servlet

GET

POST

HTML

Game Controller

Game Bundle

Page 17: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Web?

17

@Component( service = Servlet.class , property="osgi.http.whiteboard.servlet.pattern=/game")

public class GameServlet extends HttpServlet {

@Referenceprivate GameController controller;

Page 18: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Unary References

18

@Referenceprivate GameController controller;

@Reference(cardinality=ReferenceCardinality.OPTIONAL policy=ReferencePolicy.DYNAMIC)

private volatile GameStatistics stats;

Page 19: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Multiple References

19

@Reference(cardinality=ReferenceCardinality.MULTIPLE)private volatile List<Highscore> highscores;

@Referenceprivate final Set<Highscore> highscores = new ConcurrentSkipListSet<Highscore>();

Page 20: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Game Design

20

Game Servlet

GET

POST

HTML

Game Controller

Game Bundle

Page 21: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Management

21

Page 22: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Component Management

22

Page 23: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Metatype I

23

@ObjectClassDefinition(name = "Game Configuration",description = "The configuration for the guessing game.")

public @interface Config {

@AttributeDefinition(name="Easy", description="Maximum value for easy")

int easy_max() default 10;

Page 24: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Metatype II

24

@Component@Designate( ocd = Config.class )

public class GameControllerImpl implements GameController {

Page 25: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Declarative Services

§  Easy too use §  Pojos §  DI with handling dynamics §  Integrates with Configuration Admin and Metatype

25

Page 26: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Try it out TODAY

§  Tooling *is* available §  Official open source implementations available

26

Page 27: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

OSGi Coordinator

Page 28: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

OSGi Coordinator

§  Sometimes you want to group things together

§  set multiple configuration values

-> afterwards apply

§  multiple database access calls -> then close the connection

§  perform related operations -> then flush the cache

§  Combining these is better

§  Use the OSGi Coordinator Service (Chapter 130 of OSGi Enterprise specs)

28

“Red arrows in apollo formation cotswoldairshow 2010 arp” by Arpingstone from wikipedia.org

Page 29: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Coordinator Service

§  Coordination Initiator §  Start a coordination implicit/explicit §  Knows the ‘higher level’ task §  Normally ends the coordination too

§  Participants

§  don’t have the bigger picture §  but can add themselves as ‘interested’ §  and get notified on completion or failure

§  Both the initiator and participants can fail the coordination

§  Similar to transactions

§  but then light

29

For implementations see: https://en.wikipedia.org/wiki/OSGi_Specification_Implementations

Page 30: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Optimization using Coordinator Service – without API change

30

public void setConfigurations() { for (int i=0; i<10; i++) { setConfig(i); }}

private void setConfig(int i) { System.out.println("Setting config data " + i); System.out.println("Flush configuration");}

setConfigurationsWithCoordinator()

Setting config data 0 Setting config data 1 Setting config data 2 Setting config data 3 Setting config data 4 Setting config data 5 Setting config data 6 Setting config data 7 Setting config data 8 Setting config data 9 Flush configuration

setConfigurations()

Setting config data 0 Flush configuration Setting config data 1 Flush configuration Setting config data 2 Flush configuration Setting config data 3 Flush configuration Setting config data 4 Flush configuration Setting config data 5 Flush configuration Setting config data 6 Flush configuration Setting config data 7 Flush configuration Setting config data 8 Flush configuration Setting config data 9 Flush configuration

private Coordinator coordinator = ... // From Service Registry

public void setConfigurationsWithCoordinator() {Coordination c = coordinator.begin("coord.name", 0);try { setConfigurations(); } catch (Exception ex) { c.fail(ex); } finally { c.end(); }}public void setConfigurations() { for (int i=0; i<10; i++) { setConfig(i); }}

private void setConfig(int i) { System.out.println("Setting config data " + i); if (!coordinator.addParticipant(this)) System.out.println("Flush configuration");}

@Overridepublic void ended(Coordination c) throws Exception { System.out.println("Flush configuration");}@Override public void failed(Coordination c) throws Exception {}

Page 31: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

OSGi Subsystems

Page 32: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Package your app for deployment

§  Most applications are composed of multiple bundles

§  Need a neat deployment package

§  Previously: proprietary solutions

32

"Airdrop pallets" by Senior Airman Ricky J. Best - defenselink.mil (search for "airdrop"). Licensed under Public Domain via Wikimedia Commons

Page 33: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

OSGi Subsystems

§  Chapter 134 of Enterprise spec

§  Packaging of multi-bundle deployments

§  in .esa file (a zip file)

§  Optional isolation models

§  Bundles either in:

§  .esa file

§  OSGi Repository

33

Jack Kennard TSA 3-1-1 rule https://www.flickr.com/photos/javajoba/4013349543

Page 34: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Subsystem types

§  Features – everything shared

§  Applications – isolated

§  for ‘friendly’ multi-tenancy – keeps bundles from interfering with each other

§  Composites – configurable isolation

§  generally useful for larger infrastructure components

34

Chiot's Run A Few Evenings of Work flickr.com

Page 35: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Feature Subsystems

Deployment artifacts

35

Feature

api bundle

svc bundle use bundle

feature bundle

Feature 2

api bundle

svc bundle use bundle

feature bundle2

Runtime Framework

feature bundle

svc bundle

feature bundle2

use bundle

api bundle

Page 36: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Application Subsystems

Deployment artifacts

36

Top-level Application

Application

api bundle

svc bundle use bundle

Application 2

api bundle

svc bundle use bundle

Runtime Framework

svc bundle

svc bundle2

api bundle

use bundle

api bundle

use bundle

Page 37: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Build a subsystem

§  Use maven: <project> <artifactId>mysubsystem</artifactId> <packaging>esa</packaging> <dependencies> <!– the bundles you want in your subsystem --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.aries</groupId> <artifactId>esa-maven-plugin</artifactId> <extensions>true</extensions> <configuration> <generateManifest>true</generateManifest> <instructions> <Subsystem-Type>osgi.subsystem.feature </Subsystem-Type> </instructions> </configuration> </plugin>

to produce mysubsystem.esa 37

"Sausage making-H-1" by László Szalai (Beyond silence) - Own work. Licensed under Public Domain via Wikimedia Commons

Page 38: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Deploy

38

https://svn.apache.org/repos/asf/felix/trunk/webconsole-plugins/subsystems https://svn.apache.org/repos/asf/aries/trunk/subsystem/subsystem-gogo-command

…or use the Subsystem Gogo Command g! subsystem:list 0 ACTIVE org.osgi.service.subsystem.root 1.0.0 g! subsystem:install …/feature1.esa ... Subsystem successfully installed: feature1; id: 1

Page 39: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

More info on creating a subsystem

§  OSGi Enterprise R6 spec: http://www.osgi.org/Download/Release6

§  Apache Aries website http://aries.apache.org/modules/subsystems.html

§  esa-maven-plugin http://aries.apache.org/modules/esamavenpluginproject.html

§  For examples see: https://github.com/coderthoughts/subsystem-examples

39

Page 40: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Recipe – OSGi R6 Compendium

40

§  OSGi Declarative Services (Chapter 112)

§  OSGi Http Whiteboard Service (Chapter 140)

§  OSGi Configuration Admin (Chapter 104)

§  OSGi Metatype Service (Chapter 105)

§  OSGi Coordinator (Chapter 130)

§  OSGi Subsystems (Chapter 134)

Page 41: Maximize the power of OSGi

© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 41

Q&(A)