Modular architecture today

68
1

description

Devoxx 2012 University session "Modular Architecture Today" demonstrating how to apply some of the modularity patterns to build a system with a modular architecture.

Transcript of Modular architecture today

Page 1: Modular architecture today

1

Page 2: Modular architecture today

Modular Architecture Today

Kirk Knoernschild@pragkirk

2

Page 3: Modular architecture today

Java Application ArchitectureModularity Patterns with Examples Using OSGi

Forewords by Robert C. Martin and Peter Kriens

Java Application Architecture will help you

‣Design modular software that is extensible, reusable, maintainable, and adaptable

‣Design modular software today, in anticipation of platform support for modularity

‣Break large software systems into a flexible composite of collaborating modules

‣Understand where to place your architectural focus

‣Migrate large-scale monolithic applications to applications with a modular architecture

‣Articulate the advantages of modular software to your team

Visit http://modularity.kirkk.com for more information.

I’m dancing! By god I’m dancing on the walls. I’m dancing on the ceiling. I’m ecstatic. I’m overjoyed. I’m really, really pleased.”

- From the Foreword by Robert C. Martin (a.k.a. Uncle Bob)

3

Page 4: Modular architecture today

The Code

https://github.com/pragkirk/poma/network

or just Google pragkirk github

4

Page 5: Modular architecture today

Goals Today

2 Simple Goals Today

1.) Start designing modular software tomorrow!

2.) Don’t flip the bit on OSGi as too complex!

5

Page 6: Modular architecture today

Modular Architecture Today

Introducing Modularity

The Modularity Patterns

Refactor to Modularity

Introducing OSGi

OSGi-ify the App6

Page 7: Modular architecture today

Agile Architecture

Introducing Modularity

What are the benefits of modularity?

Why is modularity a necessary component of agile architecture?

What challenges face us today in designing modular software?

7

Page 8: Modular architecture today

Modularity - Not New!

1972(or a bit before)

On The Criteria To Be Used in Decomposing Systems into Modules by David Parnas at http://www.cs.umd.edu/class/spring2003/cmsc838p/Design/criteria.pdf

8

Page 9: Modular architecture today

Defining Module

- unit of reuse- unit of composition- unit of deployment- unit of management

A module system provides a runtime environment for modules

Hey, it’s a JAR file!

9

Page 10: Modular architecture today

The Facets

Programming ModelThe frameworks and technologies that allow us to create modular software

Design ParadigmThe techniques used to identify and create the right set of modules

The Design Paradigm- What’s the right granularity for a module?

- What the right weight for a module?

InfrastructureRuntime platform support helps enforce modular architecture.

demo

DevelopmentRuntime

Think about when you started using objects! Using the language constructs were easy, but creating designs was still really hard.

10

Page 11: Modular architecture today

Paradox

Increasing evolvability decreases survivability

... making everything easy to change makes the entire system very complex...

- Ralph Johnson in “Who Needs an Architect”

(Reuse, Compose,Extend,

Lightweight, Fine-Grained, ...)

(Use, Maintain, Understand, ...)

11

Page 12: Modular architecture today

Architectural Joints or Seams

Which area of the system demands more flexibility?

Here?

Here?

Here?

Here? Here?

Here?

Here?

12

Page 13: Modular architecture today

Complexity and Knowledge

Source: http://www.tensegrity.hellblazer.com/ Source: http://adaptevolve.blogspot.com/

13

Page 14: Modular architecture today

Complexity, Knowledge, & Modularity

14

Page 15: Modular architecture today

Architectural Joints or Seams

Which area of the system demands more flexibility?

Here?

Here?

Here?

Here? Here?

Here?

Here?

demo

Modularizing software affects our design in interesting ways.

15

Page 16: Modular architecture today

Benefits of Modularity

- reuse- reduce complexity- ease maintenance- increase extensibility

Increases architectural agility!

Umm...we can already do this with objects, aspects, methods, and services!

16

Page 17: Modular architecture today

All the Way Down

?

Reuse Release Equivalence: Unit of reuse is the unit of release!

17

Page 18: Modular architecture today

Agile Architecture

The Modularity Patterns

What are the modularity patterns?

18

Page 19: Modular architecture today

Base Patterns

• Manage Relationships – Design Module Relationships.

• Module Reuse – Emphasize reusability at the module level.

• Cohesive Modules – Module behavior should serve a singular purpose.

19

Page 20: Modular architecture today

Dependency Patterns

• Acyclic Relationships - Module relationships must be acyclic.

• Levelize Modules – Module relationships should be levelized.

• Physical Layers - Module relationships should not violate the conceptual layers.

• Container Independence - Modules should be independent of the runtime container.

• Independent Deployment - Modules should be independently deployable units.

20

Page 21: Modular architecture today

Usability Patterns

• Published Interface - Make a module’s published interface well known.

• External Configuration – Modules should be externally configurable.

• Default Implementation - Provide modules with a default implementation.

• Module Facade – Create a facade serving as a coarse-grained entry point to another fine-grained module’s underlying implementation.

21

Page 22: Modular architecture today

Extensibility Patterns

• Abstract Modules - Depend upon the abstract elements of a module.

• Implementation Factory - Use factories to create a module’s implementation classes.

• Separate Abstractions - Place abstractions and the classes that implement them in separate modules.

22

Page 23: Modular architecture today

Utility Patterns

• Colocate Exceptions: Exceptions should be close to the class or interface that throws them.

• Levelize Build – Execute the build in accordance with module levelization.

• Test Module – Each module should have a corresponding test module.

23

Page 24: Modular architecture today

Agile Architecture

Patterns Applied

How can I use the modularity patterns?

How do they help accommodate architectural shifts?

This is the Design Paradigm

24

Page 25: Modular architecture today

The System

Design a system to handle payment and auditing of various types of bills. The system must integrate with 3rd party auditing software, and a legacy financials system that must be fed payment information for reconciliation.

25

Page 26: Modular architecture today

Note the bi-directional associations!

The Class Model

26

Page 27: Modular architecture today

Initial Systems Modules

If I layer conceptually but not physically, then am I realizing the advantages of layering? Why do I layer?

demo

27

Page 28: Modular architecture today

Physical LayersModule relationships should not violate the conceptual layers.

28

Page 29: Modular architecture today

Abstract Modules

- “Inject” the implementation into Client.- “Lookup” the implementation within Client.

Depend upon the abstract elements of a module.

package client;import service.*;public class Client { Service service;}

package service;

public interface Service { public void doService();}

package service;

public class ServiceImpl implements Service { public void doService() { .... };}

29

Page 30: Modular architecture today

Abstract Modules

What if Bill must be able to use different auditing systems?

30

Page 31: Modular architecture today

Abstract Modules

AuditFacade1 is injected into Bill as an AuditFacade type.

31

Page 32: Modular architecture today

Abstract Modules

32

Page 33: Modular architecture today

Acyclic RelationshipsModule relationships must be acyclic

33

Page 34: Modular architecture today

Recall - Abstract Modules

AuditFacade1 is injected into Bill as an AuditFacade type.

Same problem here

34

Page 35: Modular architecture today

Recall - Abstract Modules

35

Page 36: Modular architecture today

Uni-Directional

36

Page 37: Modular architecture today

Acyclic Relationshipsdemo

37

Page 38: Modular architecture today

Separate Abtractions

How do I integrate with another auditing system? Where does AuditFacade2 live?

Separate abstractions from the classes that realize them.

38

Page 39: Modular architecture today

Separate Abstractions

Should I put AuditFacade2 in audit.jar?

39

Page 40: Modular architecture today

Separate Abstractions

40

Page 41: Modular architecture today

Colocate Exceptions

AuditFacade throws the AuditException.

Exceptions should be close to the classes that throw them

Exception goes here.

41

Page 42: Modular architecture today

Independent Deployment

How do I reuse bill.jar without financial.jar? Like in a batch application?

Modules should be independently deployable units.

42

Page 43: Modular architecture today

Independent Deployment

43

Page 44: Modular architecture today

Independent Deployment

1.) PayAction invokes Bill.pay() and passes BillPayAdapter as a BillPayer.2.) Bill.pay() invokes BillPayer.generateDraft()3.)BillPayAdapeter.generateDraft() invokes Payment.generateDraft() passing itself as a Payable.4.) Payment.generateDraft() invokes Payable.getAmount()

44

Page 45: Modular architecture today

Independent Deployment

45

Page 46: Modular architecture today

Implementation FactoryUse factories to create a modules implementation classes.

46

Page 47: Modular architecture today

The Final Structuredemo

47

Page 48: Modular architecture today

Opportunities

48

Page 49: Modular architecture today

Modular Architecture Today

Introducing OSGi?

What is OSGi?

Is OSGi new? Who is using OSGi?

This is the Infrastructure & Programming Model

49

Page 50: Modular architecture today

Modularity is coming to the Java platform!

OpenJDK

50

Page 51: Modular architecture today

A Bit of History

• JSR 8 - Open Services Gateway specification in 1999 (JSR 232 or JSR 291)

• Gateway for providing services to home devices

• lifecycle management, dependencies, installing, versioning, configuration

• OSGi Alliance formed in 1999 and delivered V1.0 in 2000

• Very popular on the desktop; driven by Eclipse 3.0 adoption in 2003

• Today, just about all Java middleware products use OSGi

• WAS, WebLogic, GlassFish, Paremus Service Fabric, JBoss, Geronimo, ...

51

Page 52: Modular architecture today

Introducing OSGiDynamic Module System for Java

ClassLoaders- Each bundle has it’s own classloader and it’s own lifecycle

µServices- Bundle exposes it’sbehavior via well-defined interface

Bundles- Bundle is a JAR file with manifest

MODULE

52

Page 53: Modular architecture today

JAR

A Valid BundleManifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: Scala CalculatorBundle-SymbolicName: scala calculatorBundle-Version: 1.0.0Import-Package: com.extensiblejava.loan,scala.annotation.unchecked;uses:="scala.reflect,scala" ;version="2.8.0.final"

+

myjar.jar Manifest.mf

<osgi:service id="LoanCalculator" ref="loanCalculator" interface="com.extensiblejava.loan.LoanCalculator"/><osgi:reference id="paymentFactory" interface="com.extensiblejava.loan.PaymentFactory"/>

Register µServices

µService Reference

53

Page 54: Modular architecture today

The Basic Workings

JAR is a module

Private packages are implementation

Importedpackages

Exportedpackages

Classloader restricts visibility

Classloader gives each module its own

lifecycle

Publishes the interface, ideally as

µServices

DYNAMICITY

54

Page 55: Modular architecture today

Modules as First Class Citizens

INSTALLED

RESOLVED

UNINSTALLED

ACTIVE

STOPPING

STARTING

start

stop

Because bundles have their own classloader, they can be

managed independently. Hence, a very dynamic

environment.

55

Page 56: Modular architecture today

The Runtime

- Dynamic deployment- Multiple versions- Enforce dependencies- Encapsulation

demo

56

Page 57: Modular architecture today

Too Complex?

“OSGi provides little value and is too complex as demonstrated by our failed attempt to make modularity invisible when porting our legacy system to it with over 150 third-party JARs.-- http://blogs.mulesoft.org/osgi-no-thanks

“OSGi is a great solution for complex applications with stringent modularity requirements.”-- http://www.theserverside.com/news/thread.tss?thread_id=62590

demo

57

Page 58: Modular architecture today

A Modular System TODAY!

58

Page 59: Modular architecture today

Standard Java Lack Encapsulation

Everything is still visible; no dynamics!

59

Page 60: Modular architecture today

No Encapsulation

60

Page 61: Modular architecture today

Type Visibility

Any public class in any JAR on the classpath can be seen by any other class on the classpath!

With OSGi, you have control who sees what!

Cookie Jar images courtesy of Richard Hall

61

Page 62: Modular architecture today

Not Visible

62

Page 63: Modular architecture today

Encapsulation

Nothing can reach this class!

demo

63

Page 64: Modular architecture today

Modularity Today

Why aren’t we designing more modular software?

Platforms discourage modularity!

64

Page 65: Modular architecture today

Modularity TomorrowThis is the next generation application platform!

- Dynamic deployment- Multiple versions- Explicit dependencies

demo

65

Page 66: Modular architecture today

Modular Architecture Today

OSGi-ify the App

How do I build applications using OSGi?

Is it invasive to my code?

Is it really too complex?

66

Page 67: Modular architecture today

Java Application ArchitectureModularity Patterns with Examples Using OSGi

Forewords by Robert C. Martin and Peter Kriens

Java Application Architecture will help you

‣Design modular software that is extensible, reusable, maintainable, and adaptable

‣Design modular software today, in anticipation of platform support for modularity

‣Break large software systems into a flexible composite of collaborating modules

‣Understand where to place your architectural focus

‣Migrate large-scale monolithic applications to applications with a modular architecture

‣Articulate the advantages of modular software to your team

Visit http://modularity.kirkk.com for more information.

I’m dancing! By god I’m dancing on the walls. I’m dancing on the ceiling. I’m ecstatic. I’m overjoyed. I’m really, really pleased.”

- From the Foreword by Robert C. Martin (a.k.a. Uncle Bob)

67

Page 68: Modular architecture today

Q&A

68