SlideShare a Scribd company logo
1 of 21
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
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!

More Related Content

Recently uploaded

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 

Recently uploaded (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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.
  • 5.
  • 6.
  • 7.
  • 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

Editor's Notes

  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