SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
Promises in Java:
Using Promises to Recover from Failure
BJ Hargrave
IBM
What is a
Promise?
2
3
• A Promise object holds the eventual result of a potentially asynchronous
task.
• A Promise is either unresolved or resolved.
• An unresolved Promise does not have the result of the associated task
available while a resolved Promise has the result of the associated task
available.
• A resolved Promise can be either resolved with a value, which means
the associated task completed successfully and supplied a result, or
resolved with a failure, which means the associated task completed
unsuccessfully and supplied an exception.
https://osgi.org/specification/osgi.cmpn/7.0.0/util.promise.html
Promises in Java: Using Promises to Recover from Failure
Promises in Java: Using Promises to Recover from Failure
Promises in Java: Using Promises to Recover from Failure
Monadic
Chaining Promises
9
• Promises can be chained
• Methods on Promise can return a new Promise chained to the original promise
• Each promise in the chain is used to resolve the next promise in the chain with
some possible mutation of the result/failure
Chaining Example
10
Promise<String> name = getDownloadName();
Promise<URL> mirror = name.flatMap(p -> getMirror(p));
Promise<File> file = mirror.flatMap(p -> getFile(p));
Since we probably do not need the intermediate Promises, we can collapse
the chain into a single statement:
Promise<File> file = getDownloadName()
.flatMap(this::getMirror)
.flatMap(this::getFile);
Using Promises
to Recover from
Failure
11
Bnd
12
• Bnd is open source tooling to build OSGi bundles
• Bnd provides Eclipse, Maven, and Gradle plugins
• Bnd has support for Maven/Nexus, Eclipse P2, and other remote artifact
repositories
• https://github.com/bndtools/bnd
Bnd’s HttpClient
13
• For communications, Bnd has its own HttpClient class
• This is for historical reasons and to minimize dependencies on 3rd party
communications packages
• Recent reliability issues with P2 repositories on Eclipse’s download site caused
us to add retry support to the HttpClient class
• The HttpClient class already had synchronous and asynchronous request
methods
• The asynchronous request method already returns a Promise 
Changes to add Recovery support
14
• Change async method to add retry support
• Retry count with delay
• Timeout for connection and read hangs
• Server side 5XX errors and IOException failures
• Change sync method to use async method which now has retry support
• Uses a Promise blocking method to synchronously wait for result
• Through the “magic” of Promise chaining, we can further add checksum
validation and checksum failure retry logic
Recovery functions
Code
Coordinates
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.promise</artifactId>
<version>7.0.1</version>
</dependency>
CompletableFuture
18
• Introduced in Java 8 and expanded in Java 9
• A promise type in the Java platform but…
• Has a large and unwieldy API
• Odd method names
• Combines resolving promise with using promise
• Mutable
• Promise is a one-way latch – one-time resolve
• CompletableFuture can be resolved repeatedly - obtrude
19
CompletionStage Promise
thenApply map
thenCompose flatMap
exceptionally recover
handle then
thenRun onResolve
whenComplete onSuccess/onFailure
20
CompletableFuture Promise
supplyAsync/runAsync PromiseFactory.submit
allOf PromiseFactory.all
complete Deferred.resolve
completeExceptionally Deferred.fail
completedFuture PromiseFactory.resolved
failedFuture PromiseFactory.failed
<init> PromiseFactory.deferred
cancel X
delayedExecutor delay
orTimeout timeout
isDone isDone
get getValue/getFailure
Thanks!

Contenu connexe

Dernier

Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServiceRenan Moreira de Oliveira
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxYounusS2
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataSafe Software
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum ComputingGDSC PJATK
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncObject Automation
 
Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfAnna Loughnan Colquhoun
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 

Dernier (20)

Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptx
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation Inc
 
Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdf
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 

Promises in Java: Using Promises to Recover from Failure

  • 1. Promises in Java: Using Promises to Recover from Failure BJ Hargrave IBM
  • 3. 3 • A Promise object holds the eventual result of a potentially asynchronous task. • A Promise is either unresolved or resolved. • An unresolved Promise does not have the result of the associated task available while a resolved Promise has the result of the associated task available. • A resolved Promise can be either resolved with a value, which means the associated task completed successfully and supplied a result, or resolved with a failure, which means the associated task completed unsuccessfully and supplied an exception.
  • 9. Chaining Promises 9 • Promises can be chained • Methods on Promise can return a new Promise chained to the original promise • Each promise in the chain is used to resolve the next promise in the chain with some possible mutation of the result/failure
  • 10. Chaining Example 10 Promise<String> name = getDownloadName(); Promise<URL> mirror = name.flatMap(p -> getMirror(p)); Promise<File> file = mirror.flatMap(p -> getFile(p)); Since we probably do not need the intermediate Promises, we can collapse the chain into a single statement: Promise<File> file = getDownloadName() .flatMap(this::getMirror) .flatMap(this::getFile);
  • 11. Using Promises to Recover from Failure 11
  • 12. Bnd 12 • Bnd is open source tooling to build OSGi bundles • Bnd provides Eclipse, Maven, and Gradle plugins • Bnd has support for Maven/Nexus, Eclipse P2, and other remote artifact repositories • https://github.com/bndtools/bnd
  • 13. Bnd’s HttpClient 13 • For communications, Bnd has its own HttpClient class • This is for historical reasons and to minimize dependencies on 3rd party communications packages • Recent reliability issues with P2 repositories on Eclipse’s download site caused us to add retry support to the HttpClient class • The HttpClient class already had synchronous and asynchronous request methods • The asynchronous request method already returns a Promise 
  • 14. Changes to add Recovery support 14 • Change async method to add retry support • Retry count with delay • Timeout for connection and read hangs • Server side 5XX errors and IOException failures • Change sync method to use async method which now has retry support • Uses a Promise blocking method to synchronously wait for result • Through the “magic” of Promise chaining, we can further add checksum validation and checksum failure retry logic
  • 16. Code
  • 18. CompletableFuture 18 • Introduced in Java 8 and expanded in Java 9 • A promise type in the Java platform but… • Has a large and unwieldy API • Odd method names • Combines resolving promise with using promise • Mutable • Promise is a one-way latch – one-time resolve • CompletableFuture can be resolved repeatedly - obtrude
  • 19. 19 CompletionStage Promise thenApply map thenCompose flatMap exceptionally recover handle then thenRun onResolve whenComplete onSuccess/onFailure
  • 20. 20 CompletableFuture Promise supplyAsync/runAsync PromiseFactory.submit allOf PromiseFactory.all complete Deferred.resolve completeExceptionally Deferred.fail completedFuture PromiseFactory.resolved failedFuture PromiseFactory.failed <init> PromiseFactory.deferred cancel X delayedExecutor delay orTimeout timeout isDone isDone get getValue/getFailure

Notes de l'éditeur

  1. HttpClient.send https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L209-L216 HttpClient.sendAsync https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L163-L207 RetryException in HttpClient.HttpConnection.doConnect https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L671-L673 https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L685-L704 Checksum in P2Indexer.fetch https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.repository/src/aQute/bnd/repository/p2/provider/P2Indexer.java#L209-L224 MavenRemoteRepository.fetch https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.repository/src/aQute/maven/provider/MavenRemoteRepository.java#L53-L101
  2. HttpClient.send https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L209-L216 HttpClient.sendAsync https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L163-L207 RetryException in HttpClient.HttpConnection.doConnect https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L671-L673 https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.bndlib/src/aQute/bnd/http/HttpClient.java#L685-L704 Checksum in P2Indexer.fetch https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.repository/src/aQute/bnd/repository/p2/provider/P2Indexer.java#L209-L224 MavenRemoteRepository.fetch https://github.com/bndtools/bnd/blob/8cc23644f75bbdbde12526500dd6444a504f875c/biz.aQute.repository/src/aQute/maven/provider/MavenRemoteRepository.java#L53-L101