32
Service Oriented Web Development with OSGi Carsten Ziegeler | [email protected] 1 OSGi Community Event 2014

Service Oriented Web Development with OSGi - C Ziegeler

Embed Size (px)

DESCRIPTION

OSGi Community Event 2014 Abstract: OSGi is complicated, complex and requires too much coding? Whether you think that's true or whether you are interested in seeing live how easy it is to develop a web application with OSGi, this session is the place to go to. During the talk a web application based on OSGi services will be developed leveraging the latest OSGi specifications, the Http Whiteboard Service (RFC 189) and updates to Declarative Services (RFC 190, RFC 208 and RFC 212). The used implementations are well known projects from The Apache Software Foundation. Learn how to leverage the annotation based development which is very easy to use but also very powerful. Apart from developing the application, open source tooling based on the Eclipse IDE will be demonstrated making the life of a developer much easier. Of course, important topics like configuration and management of the web application are covered as well. Speaker Bio: Carsten Ziegeler is senior developer at Adobe Research Switzerland and spends most of his time on architectural and infrastructure topics. Working for over 25 years in open source projects, Carsten is a member of the Apache Software Foundation and heavily participates in several Apache communities including Sling, Felix and ACE. He is a frequent speaker on technology and open source conferences and participates in the OSGi Core Platform and Enterprise expert groups.

Citation preview

Page 1: Service Oriented Web Development with OSGi - C Ziegeler

Service Oriented Web Development with OSGi Carsten Ziegeler | [email protected]

1

OSGi Community Event 2014

Page 2: Service Oriented Web Development with OSGi - C Ziegeler

About [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 - C Ziegeler

OSGi Preconceptions

3

No POJOs

Too slow

No dependency injection

Not suitable for the enterprise

No

tool

ing ?!?

Page 4: Service Oriented Web Development with OSGi - C Ziegeler

4

Page 5: Service Oriented Web Development with OSGi - C Ziegeler

The Next Big Thing

5

Page 6: Service Oriented Web Development with OSGi - C Ziegeler

Building Blocks

§  Module aka Bundle

§  Services

§  Components

6

Page 7: Service Oriented Web Development with OSGi - C Ziegeler

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 - C Ziegeler

Implementation

8

@Componentpublic class GameControllerImpl implements GameController { ...

Page 9: Service Oriented Web Development with OSGi - C Ziegeler

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 - C Ziegeler

10

private Config configuration;@Activateprotected void activate(final Config config) {

this.configuration = config;}

Page 11: Service Oriented Web Development with OSGi - C Ziegeler

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 - C Ziegeler

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 - C Ziegeler

13

public class GameServlet extends HttpServlet {@FieldReferenceprivate GameController controller;

Page 14: Service Oriented Web Development with OSGi - C Ziegeler

14

Page 15: Service Oriented Web Development with OSGi - C Ziegeler

15

No POJOs

Too slow

No dependency injection

Not suitable for the enterprise

No

tool

ing ✔

Page 16: Service Oriented Web Development with OSGi - C Ziegeler

Recipe

§  OSGi Declarative Services (Compendium Chapter 112) §  + RFC 190 Declarative Services Enhancements (OSGi R6) §  + RFC 212 Field Injection for Declarative Services (OSGi R6)

§  OSGi 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 - C Ziegeler

Management

17

Page 18: Service Oriented Web Development with OSGi - C Ziegeler

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 - C Ziegeler

Metatype

19

@Component@Designate( ocd = Config.class )public class GameControllerImpl implements GameController {

Page 20: Service Oriented Web Development with OSGi - C Ziegeler

Component Container Interaction

20

OSGi Service Registry

Declarative Services Blueprint

iPojo, Dependency

Manager, ….

Framework API

Page 21: Service Oriented Web Development with OSGi - C Ziegeler

Service Scopes

•  Singleton •  Bundle

•  Prototype

21

Page 22: Service Oriented Web Development with OSGi - C Ziegeler

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 - C Ziegeler

Dynamics

§  Lazy instantiation

§  Reference policy and cardinality

§  Reconfiguration

23

Page 24: Service Oriented Web Development with OSGi - C Ziegeler

Unary References

24

@FieldReferenceprivate GameController controller;

@FieldReference(

cardinality=ReferenceCardinality.OPTIONAL policy=ReferencePolicy.DYNAMIC)private volatile GameStatistics stats;

Page 25: Service Oriented Web Development with OSGi - C Ziegeler

Multiple References

25

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

Page 26: Service Oriented Web Development with OSGi - C Ziegeler

Multiple References

26

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

Page 27: Service Oriented Web Development with OSGi - C Ziegeler

Reconfiguration

27

private volatile Config configuration;

@Activate@Modifiedprotected void activate(final Config config) {

this.configuration = config;}

Page 28: Service Oriented Web Development with OSGi - C Ziegeler

Web Contexts

28

Servlet B /foo

Servlet A /game

Servlet C /bar

Servlet Filter

Servlet Context /play

Authentication

Servlet X /foo

Servlet Context /fooapp

Authentication

Page 29: Service Oriented Web Development with OSGi - C Ziegeler

Web Contexts

29

@Component( service = Servlet.class , property={"osgi.http.whiteboard.servlet.pattern=/foo", "osgi.http.whiteboard.context.select=mygame"}public class ServletB extends HttpServlet {

@Component( service = Servlet.class , property={"osgi.http.whiteboard.servlet.pattern=/bar", "osgi.http.whiteboard.context.select=game"}public class ServletC extends HttpServlet {

Page 30: Service Oriented Web Development with OSGi - C Ziegeler

Try it out today!

§  HTTP Whiteboard Service

§  Servlet contexts (grouping, authentication)

§  Servlets

§  Filters

§  Listeners

30

Page 31: Service Oriented Web Development with OSGi - C Ziegeler

Try it out today!

§  Declarative Services

§  Easy too use

§  Pojos

§  DI with handling dynamics

§  Tooling

§  Open Source Solutions

§  Building large scale enterprise apps

31

Page 32: Service Oriented Web Development with OSGi - C Ziegeler

QnA

32