SlideShare a Scribd company logo
component library
https://github.com/stuartsierra/component
https://github.com/jordillonch/component-example
@jordillonch July 2015
Agenda
• component library introduction
• example
Introduction
Tiny Clojure framework for
managing the lifecycle of
software components which have
runtime state
Real-world applications
need to manage state
It can be seen as a style of
dependency injection using
immutable data structures
Components
and
Systems
Components
A component is a collection
of functions which share
some runtime state
A component is similar in spirit
to the definition of an object in
Object-Oriented Programming
A component knows how to start
and stop the application pieces
that have to manage state
Some examples
• Database access: database connection
• External API service: HTTP connection pool
• Web server: a session store
• In-memory cache: a Clojure Atom or Ref
Systems
Components are
composed into systems
A system is a component
which knows how to start
and stop other components
It is also responsible for
injecting dependencies into the
components which need them
Advantages of the
Component Model
Large applications often consist of many stateful
processes which must be started and stopped in a
particular order.
The component model makes those relationships
explicit and declarative
Each component receives
references only to the things it
needs, avoiding unnecessary
shared state
Easy to swap in "stub" or "mock" implementations of
a component for testing purposes, without relying on
time-dependent constructs, such as with-redefs
or binding, which are often subject to race
conditions in multi-threaded code
Having all state reachable via a single
"system" object makes it easy to reach in
and inspect any part of the application from
the REPL
Having a coherent way to set up and tear
down all the state associated with an
application enables rapid development
cycles without restarting the JVM
It can also make unit tests faster and
more independent, since the cost of
creating and starting a system is low
enough that every test can create a new
instance of the system
Disadvantages of the
Component Model
It is not easy to retrofit the
component model to an existing
application without major
refactoring
For small applications, declaring the
dependency relationships among
components may actually be more work
than manually starting all the components
in the correct order
The system map is too
large to inspect visually
The code cannot
discover relationships
automatically
Cyclic dependencies are
forbidden among
components
Some code
(defrecord Database [host port connection]
;; Implement the Lifecycle protocol
component/Lifecycle
(start [component]
(println ";; Starting database")
;; In the 'start' method, initialize this component
;; and start it running. For example, connect to a
;; database, create thread pools, or initialize shared
;; state.
(let [conn (connect-to-database host port)]
;; Return an updated version of the component with
;; the run-time state assoc'd in.
(assoc component :connection conn)))
(stop [component]
(println ";; Stopping database")
;; In the 'stop' method, shut down the running
;; component and release any external resources it has
;; acquired.
(.close connection)
;; Return the component, optionally modified. Remember that if you
;; dissoc one of a record's base fields, you get a plain map.
(assoc component :connection nil)))
(defn new-database [host port]
(map->Database {:host host :port port}))
(defrecord ExampleComponent [options cache database scheduler]
component/Lifecycle
(start [this]
(println ";; Starting ExampleComponent")
;; In the 'start' method, a component may assume that its
;; dependencies are available and have already been started.
(assoc this :admin (get-user database "admin")))
(stop [this]
(println ";; Stopping ExampleComponent")
;; Likewise, in the 'stop' method, a component may assume that its
;; dependencies will not be stopped until AFTER it is stopped.
this))
(defn example-component [config-options]
(map->ExampleComponent {:options config-options
:cache (atom {})}))
(defn example-system [config-options]
(let [{:keys [host port]} config-options]
(component/system-map
:database (new-database host port)
:scheduler (new-scheduler)
:app (component/using
(example-component config-options)
[:database :scheduler]))))
example-system
app
ExampleComponent
database
Database
scheduler
Scheduler
(go)
example-systemexample-system
app
ExampleComponent
database
Database
scheduler
Scheduler
app
ExampleComponent
database
Database
scheduler
Scheduler
(reset)
Our example
https://github.com/jordillonch/component-example
Shows how component library
works and how use a workflow
that let you change code and
avoid the JVM restarting
The example is just an
API that exposes one
endpoint to do additions
curl http://localhost:8080/math/sum -v --data "value1=1;value2=2"
my-system
application-api
ApplicationApiComponent
context-math-engine
MathOracleSimple
(defn my-system []
(component/system-map
:context-math-engine (new-context-math-engine-system)
:application-api (component/using (new-application-api)
[:context-math-engine])))
Demo time :)
Resources
https://github.com/danielsz/system
https://github.com/stuartsierra/component
https://youtu.be/13cmHf_kt-Q
http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded
Thanks

More Related Content

PPTX
Servlets - filter, listeners, wrapper, internationalization
PDF
Hash Endereçamento Quadrático Main
PDF
Testing Spring Boot Applications
PPTX
Introduction to Srping Web Flow
PDF
Continuous performance management with Gatling
PDF
Lightweight Java EE with MicroProfile
ODP
Shell Shock
PDF
How to connect redis and mule esb using spring data redis module
Servlets - filter, listeners, wrapper, internationalization
Hash Endereçamento Quadrático Main
Testing Spring Boot Applications
Introduction to Srping Web Flow
Continuous performance management with Gatling
Lightweight Java EE with MicroProfile
Shell Shock
How to connect redis and mule esb using spring data redis module

What's hot (20)

PDF
Apache Flink and More @ MesosCon Asia 2017
PDF
Java Server Pages
ODP
Gatling - Stress test tool
PDF
Servlet
PDF
JEE Programming - 04 Java Servlets
PPTX
Developing Agile Java Applications using Spring tools
PPTX
For each component in mule
PPT
Mule with spring security manager
PPT
Simple Poll in Mule
PPTX
Basic example using choice component
PPTX
Snow Leopard
PPTX
Getting started with React Suspense and concurrent rendering
DOCX
Example of BDD/scenario based vertical slicing (for PM/PO community)
PPTX
Repository design pattern in laravel
PPTX
Choice component in mule
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
PPTX
Quartz component in mule
PPTX
Filter expression in mule
PPTX
Javax.servlet,http packages
Apache Flink and More @ MesosCon Asia 2017
Java Server Pages
Gatling - Stress test tool
Servlet
JEE Programming - 04 Java Servlets
Developing Agile Java Applications using Spring tools
For each component in mule
Mule with spring security manager
Simple Poll in Mule
Basic example using choice component
Snow Leopard
Getting started with React Suspense and concurrent rendering
Example of BDD/scenario based vertical slicing (for PM/PO community)
Repository design pattern in laravel
Choice component in mule
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Quartz component in mule
Filter expression in mule
Javax.servlet,http packages
Ad

Viewers also liked (8)

PDF
WPF Custom Control Development Unchained
PDF
React Component Library Design @WalmartLabs
PDF
Creating a Component Library
PPTX
React js - Component specs and lifecycle
PPTX
Design Pattern Libraries, Aufzucht und Pflege
PDF
From PDFs to HTML Prototypes
PDF
How UI Framework improves design process - 2015 (Dribbble meetup)
PDF
Ergosign-wpf-ui-development-best-practices-dotnet
WPF Custom Control Development Unchained
React Component Library Design @WalmartLabs
Creating a Component Library
React js - Component specs and lifecycle
Design Pattern Libraries, Aufzucht und Pflege
From PDFs to HTML Prototypes
How UI Framework improves design process - 2015 (Dribbble meetup)
Ergosign-wpf-ui-development-best-practices-dotnet
Ad

Similar to Component library (20)

PPT
Asp.net,mvc
PPT
Introduction To ASP.NET MVC
PPT
How to ace your .NET technical interview :: .Net Technical Check Tuneup
PPT
ASP.NET MVC introduction
ODP
Spring Portlet MVC
PDF
JavaEE6 my way
PPTX
ASP.Net MVC 4 [Part - 2]
PPTX
Components in real time systems
PPTX
Adding a modern twist to legacy web applications
PPTX
Apache Airdrop detailed description.pptx
PPTX
Mastering Test Automation: How To Use Selenium Successfully
PDF
System verilog important
ODP
Sprint Portlet MVC Seminar
PPT
PCD - Process control daemon
PDF
Spring 2
PPT
MVC
PPT
Ibm
PDF
The Meteor Framework
ODP
Annotation-Based Spring Portlet MVC
PPT
Taming Deployment With Smart Frog
Asp.net,mvc
Introduction To ASP.NET MVC
How to ace your .NET technical interview :: .Net Technical Check Tuneup
ASP.NET MVC introduction
Spring Portlet MVC
JavaEE6 my way
ASP.Net MVC 4 [Part - 2]
Components in real time systems
Adding a modern twist to legacy web applications
Apache Airdrop detailed description.pptx
Mastering Test Automation: How To Use Selenium Successfully
System verilog important
Sprint Portlet MVC Seminar
PCD - Process control daemon
Spring 2
MVC
Ibm
The Meteor Framework
Annotation-Based Spring Portlet MVC
Taming Deployment With Smart Frog

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
A Presentation on Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Modernizing your data center with Dell and AMD
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
The Rise and Fall of 3GPP – Time for a Sabbatical?
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
MYSQL Presentation for SQL database connectivity
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
Modernizing your data center with Dell and AMD
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Monthly Chronicles - July 2025
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)

Component library