-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve hibernate example and add resilience example using failsafe
- Loading branch information
Showing
20 changed files
with
687 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,10 @@ | |
*/ | ||
package com.github.benmanes.caffeine.examples.hibernate; | ||
|
||
import static jakarta.persistence.AccessType.FIELD; | ||
import static org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE; | ||
|
||
import org.hibernate.annotations.Cache; | ||
|
||
import jakarta.persistence.Access; | ||
import jakarta.persistence.Cacheable; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
@@ -31,8 +28,6 @@ | |
* @author [email protected] (Ben Manes) | ||
*/ | ||
@Entity | ||
@Cacheable | ||
@Access(FIELD) | ||
@Cache(usage = READ_WRITE) | ||
public class Project { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,10 @@ | |
*/ | ||
package com.github.benmanes.caffeine.examples.hibernate; | ||
|
||
import static jakarta.persistence.AccessType.FIELD; | ||
import static org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE; | ||
|
||
import org.hibernate.annotations.Cache; | ||
|
||
import jakarta.persistence.Access; | ||
import jakarta.persistence.Cacheable; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
@@ -30,8 +27,6 @@ | |
* @author [email protected] (Ben Manes) | ||
*/ | ||
@Entity | ||
@Cacheable | ||
@Access(FIELD) | ||
@Cache(usage = READ_WRITE) | ||
public class Skill { | ||
@Id | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,16 +15,13 @@ | |
*/ | ||
package com.github.benmanes.caffeine.examples.hibernate; | ||
|
||
import static jakarta.persistence.AccessType.FIELD; | ||
import static org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.hibernate.annotations.Cache; | ||
|
||
import jakarta.persistence.Access; | ||
import jakarta.persistence.Cacheable; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
@@ -35,8 +32,6 @@ | |
* @author [email protected] (Ben Manes) | ||
*/ | ||
@Entity | ||
@Cacheable | ||
@Access(FIELD) | ||
@Cache(usage = READ_WRITE) | ||
public class User { | ||
@Id | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
rootLogger.level = info | ||
rootLogger.appenderRefs = console | ||
rootLogger.appenderRef.console.ref = console | ||
|
||
logger.hibernate.name = org.hibernate | ||
logger.hibernate.level = warn | ||
|
||
logger.hikaricp.name = com.zaxxer.hikari | ||
logger.hikaricp.level = warn | ||
|
||
appender.console.name = console | ||
appender.console.type = Console | ||
appender.console.layout.type = PatternLayout | ||
appender.console.layout.pattern = %highlight{[%p]} %c %m%n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[Failsafe's][failsafe] retry, timeout, and fallback strategies can be used to make the cache | ||
operations resiliant to intermittent failures. | ||
|
||
### Retry | ||
A [retry policy][retry] will retry failed executions a certain number of times, with an optional | ||
delay between attempts. | ||
|
||
```java | ||
var retryPolicy = RetryPolicy.builder() | ||
.withDelay(Duration.ofSeconds(1)) | ||
.withMaxAttempts(3) | ||
.build(); | ||
var failsafe = Failsafe.with(retryPolicy); | ||
|
||
// Retry outside of the cache loader for synchronous calls | ||
Cache<Integer, String> cache = Caffeine.newBuilder().build(); | ||
failsafe.get(() -> cache.get(key, key -> /* intermittent failures */ )); | ||
|
||
// Optionally, retry inside the cache load for asynchronous calls | ||
AsyncCache<Integer, String> asyncCache = Caffeine.newBuilder().buildAsync(); | ||
asyncCache.get(key, (key, executor) -> failsafe.getAsync(() -> /* intermittent failure */)); | ||
``` | ||
|
||
### Timeout | ||
A [timeout policy][timeout] will cancel the execution if it takes too long to complete. | ||
|
||
```java | ||
var retryPolicy = RetryPolicy.builder() | ||
.withDelay(Duration.ofSeconds(1)) | ||
.withMaxAttempts(3) | ||
.build(); | ||
var timeout = Timeout.builder(Duration.ofSeconds(1)).withInterrupt().build(); | ||
var failsafe = Failsafe.with(timeout, retryPolicy); | ||
|
||
Cache<Integer, String> cache = Caffeine.newBuilder().build(); | ||
failsafe.get(() -> cache.get(key, key -> /* timeout */ )); | ||
``` | ||
|
||
### Fallback | ||
A [fallback policy][fallback] will provide an alternative result for a failed execution. | ||
|
||
```java | ||
var retryPolicy = RetryPolicy.builder() | ||
.withDelay(Duration.ofSeconds(1)) | ||
.withMaxAttempts(3) | ||
.build(); | ||
var fallback = Fallback.of(/* fallback */); | ||
var failsafe = Failsafe.with(fallback, retryPolicy); | ||
|
||
Cache<Integer, String> cache = Caffeine.newBuilder().build(); | ||
failsafe.get(() -> cache.get(key, key -> /* failure */ )); | ||
``` | ||
|
||
[failsafe]: https://failsafe.dev | ||
[retry]: https://failsafe.dev/retry | ||
[timeout]: https://failsafe.dev/timeout | ||
[fallback]: https://failsafe.dev/fallback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
`java-library` | ||
alias(libs.plugins.versions) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.caffeine) | ||
implementation(libs.failsafe) | ||
|
||
testImplementation(libs.junit) | ||
testImplementation(libs.truth) | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
|
||
java.toolchain.languageVersion = JavaLanguageVersion.of( | ||
System.getenv("JAVA_VERSION")?.toIntOrNull() ?: 11) | ||
|
||
tasks.withType<JavaCompile>().configureEach { | ||
javaCompiler = javaToolchains.compilerFor { | ||
languageVersion = java.toolchain.languageVersion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[versions] | ||
caffeine = "3.1.7" | ||
failsafe = "3.3.2" | ||
junit = "5.10.0" | ||
truth = "1.1.5" | ||
versions = "0.47.0" | ||
|
||
[libraries] | ||
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine" } | ||
failsafe = { module = "dev.failsafe:failsafe", version.ref = "failsafe" } | ||
junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" } | ||
truth = { module = "com.google.truth:truth", version.ref = "truth" } | ||
|
||
[plugins] | ||
versions = { id = "com.github.ben-manes.versions", version.ref = "versions" } |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
examples/resilience-failsafe/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.