Service oriented web development with OSGi

34
Service Oriented Web Development with OSGi Carsten Ziegeler | [email protected] 1 OSGi User Forum Germany 2015

Transcript of Service oriented web development with OSGi

Page 1: Service oriented web development with OSGi

Service Oriented Web Development with OSGiCarsten Ziegeler | [email protected]

1

OSGi User Forum Germany 2015

Page 2: Service oriented web development with OSGi

[email protected] @cziegeler

• RnD Team at Adobe Research Switzerland

• Member of the Apache Software Foundation

• Apache Felix and Apache Sling (PMC and committer)

• And other Apache projects

• OSGi Core Platform and Enterprise Expert Groups

• Member of the OSGi Board

• Book / article author, technical reviewer, conference speaker

2

Page 3: Service oriented web development with OSGi

OSGi Preconceptions

3

?!

Page 4: Service oriented web development with OSGi

4

Page 5: Service oriented web development with OSGi

The Next Big Thing

5

Page 6: Service oriented web development with OSGi

Building Blocks

§ Module aka Bundle

§ Services

§ Components

6

Page 7: Service oriented web development with OSGi

Game Design

7

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);}

Page 8: Service oriented web development with OSGi

Implementation

8

@Componentpublic class GameControllerImpl implements GameController {

...

Page 9: Service oriented web development with OSGi

Configuration

9

public @interface Config {

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

}

Page 10: Service oriented web development with OSGi

10

private Config configuration;

@Activateprotected void activate(final Config config) {

this.configuration = config;}

Page 11: Service oriented web development with OSGi

11

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 12: Service oriented web development with OSGi

Web?

12

@Component( service = Servlet.class ,

property="osgi.http.whiteboard.servlet.pattern=/game")

public class GameServlet extends HttpServlet {

Page 13: Service oriented web development with OSGi

13

public class GameServlet extends HttpServlet {

@Referenceprivate GameController controller;

Page 14: Service oriented web development with OSGi

14

Page 15: Service oriented web development with OSGi

15

Page 16: Service oriented web development with OSGi

Recipe

§ OSGi Declarative Services (Compendium Chapter 112)

§ + RFC 190 Declarative Services Enhancements (OSGi R6)

§ + RFC 212 Field Injection for Declarative Services (OSGi R6)

§ OSGi Http Whiteboard Service

§ + RFC 189 (OSGi R6)

§ OSGi Configuration Admin (Compendium Chapter 104)

§ OSGi Metatype Service (Compendium Chapter 105)

§ + RFC 208 Metatype Annotations

16

Page 17: Service oriented web development with OSGi

Management

17

Page 18: Service oriented web development with OSGi

Metatype

18

@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 19: Service oriented web development with OSGi

Metatype

19

@Component@Designate( ocd = Config.class )

public class GameControllerImplimplements GameController {

Page 20: Service oriented web development with OSGi

Component Container Interaction

20

OSGi Service Registry

Declarative Services

BlueprintiPojo,

Dependency Manager,

….

Framework API

Page 21: Service oriented web development with OSGi

Service Scopes

• Singleton

• Bundle

• Prototype

21

Page 22: Service oriented web development with OSGi

Servlets

22

@Component( service = Servlet.class ,

scope=ServiceScope.PROTOTYPE,

property="osgi.http.whiteboard.servlet.pattern=/game")

public class GameServlet extends HttpServlet {

public void init() {...}public void destroy() {...}

Page 23: Service oriented web development with OSGi

Dynamics

§ Lazy instantiation

§ Reconfiguration

§ Reference policy and cardinality

23

Page 24: Service oriented web development with OSGi

Unary References

24

@Referenceprivate GameController controller;

@Reference( cardinality=ReferenceCardinality.OPTIONALpolicy=ReferencePolicy.DYNAMIC)

private volatile GameStatistics stats;

Page 25: Service oriented web development with OSGi

Multiple References

25

@Reference( cardinality=ReferenceCardinality.MULTIPLE)

private volatile List<Highscore> highscores;

Page 26: Service oriented web development with OSGi

Multiple References

26

@Reference

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

Page 27: Service oriented web development with OSGi

Reconfiguration

27

private volatile Config configuration;

@Activate@Modified

protected void activate(final Config config) {

this.configuration = config;}

Page 28: Service oriented web development with OSGi

Granularity

§ Components

§ Services

§ Bundles

§ Servlet/Filter/Listener

§ Web app

28

Page 29: Service oriented web development with OSGi

Web Contexts

29

Servlet B/score

Servlet A/game

Servlet Filter

Servlet Context/play

Authentication

Servlet X/foo

Servlet Context/fooapp

Authentication

Page 30: Service oriented web development with OSGi

Web Contexts

30

@Component( service = Servlet.class ,

property={"osgi.http.whiteboard.servlet.pattern=/game","osgi.http.whiteboard.context.select=mygame"}

public class ServletA extends HttpServlet {

@Component( service = Servlet.class ,

property={"osgi.http.whiteboard.servlet.pattern=/score","osgi.http.whiteboard.context.select=mygame"}

public class ServletB extends HttpServlet {

Page 31: Service oriented web development with OSGi

Try it out today!

§ HTTP Whiteboard Service

§ Servlet contexts (grouping, authentication)

§ Servlets

§ Filters

§ Listeners

31

Page 32: Service oriented web development with OSGi

Try it out today!

§ Declarative Services

§ Easy too use

§ Pojos

§ DI with handling dynamics

32

Page 33: Service oriented web development with OSGi

Try it out today!

§ Tooling

§ Open Source Solutions

§ Building large scale enterprise apps

33

Page 34: Service oriented web development with OSGi

QnA

34