Skip to content

Commit 062acae

Browse files
committed
Add coherence to archetype
1 parent 691a37b commit 062acae

File tree

28 files changed

+591
-42
lines changed

28 files changed

+591
-42
lines changed

archetypes/archetypes/filters.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ health=!${health} || ${health.builtin}
3737
metrics=!${metrics} || ${metrics.builtin}
3838

3939
# group extra options
40-
extra=${extra} == [] || ${extra} == ['cors', 'fault-tolerance'] || \
41-
(${flavor} == 'se' && ${extra} == ['webclient', 'cors', 'fault-tolerance'])
40+
extra=${extra} == [] || ${extra} == ['cors', 'fault-tolerance', 'coherence'] || \
41+
(${flavor} == 'se' && ${extra} == ['webclient', 'cors', 'fault-tolerance', 'coherence'])
4242

4343
# group docker, k8s, v8o and multi-module
4444
packaging=!(${docker} || ${k8s} || ${v8o}) || (${docker} && ${k8s} && ${v8o})

archetypes/archetypes/src/main/archetype/defaults.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,7 @@
8282
<boolean path="requires-org-jvnet-mimepull">false</boolean>
8383
<boolean path="requires-com-fasterxml-jackson-annotation">false</boolean>
8484
<boolean path="requires-com-fasterxml-jackson-core">false</boolean>
85+
<boolean path="requires-com-oracle-coherence">false</boolean>
86+
<boolean path="requires-com-oracle-coherence-cdi">false</boolean>
8587
</variables>
8688
</archetype-script>

archetypes/archetypes/src/main/archetype/features/customize-project/inputs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2121
xsi:schemaLocation="https://helidon.io/archetype/2.0 https://helidon.io/xsd/archetype-2.0.xsd">
2222
<inputs>
23-
<enum id="build-system" name="Select a Build System" default="maven" optional="true">
23+
<enum id="build-system" name="Select a Build System" optional="true" default="maven">
2424
<option value="maven" name="Apache Maven"/>
2525
</enum>
2626
<text id="groupId" name="Project groupId" optional="true" default="com.examples"/>

archetypes/archetypes/src/main/archetype/features/jpms/output.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
<value if="${requires-org-jvnet-mimepull}">org.jvnet.mimepull</value>
8686
<value if="${requires-com-fasterxml-jackson-annotation}">com.fasterxml.jackson.annotation</value>
8787
<value if="${requires-com-fasterxml-jackson-core}">com.fasterxml.jackson.core</value>
88+
<value if="${requires-com-oracle-coherence}">com.oracle.coherence</value>
89+
<value if="${requires-com-oracle-coherence-cdi}">com.oracle.coherence.cdi</value>
8890
</list>
8991
</model>
9092
</output>

archetypes/archetypes/src/main/archetype/features/maven/files/pom.xml.hbs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,59 @@
1313
<artifactId>{{artifactId}}</artifactId>
1414
<version>{{project-version}}</version>
1515

16-
{{#mainClass}}
16+
{{#pom-properties}}
17+
{{#first}}
1718
<properties>
18-
<mainClass>{{mainClass}}</mainClass>
19+
{{/first}}
20+
<{{key}}>{{value}}</{{key}}>
21+
{{#last}}
1922
</properties>
2023

21-
{{/mainClass}}
22-
<dependencies>
24+
{{/last}}
25+
{{/pom-properties}}
26+
{{#dependencies-management}}
27+
{{#first}}
28+
<dependencyManagement>
29+
<dependencies>
30+
{{/first}}
31+
<dependency>
32+
<groupId>{{groupId}}</groupId>
33+
<artifactId>{{artifactId}}</artifactId>
34+
{{#version}}
35+
<version>{{.}}</version>
36+
{{/version}}
37+
{{#scope}}
38+
<scope>{{.}}</scope>
39+
{{/scope}}
40+
{{#type}}
41+
<type>{{.}}</type>
42+
{{/type}}
43+
{{#optional}}
44+
<optional>{{.}}</optional>
45+
{{/optional}}
46+
{{#exclusions}}
47+
{{#first}}
48+
<exclusions>
49+
{{/first}}
50+
<exclusion>
51+
<groupId>{{groupId}}</groupId>
52+
<artifactId>{{artifactId}}</artifactId>
53+
</exclusion>
54+
{{#last}}
55+
</exclusions>
56+
{{/last}}
57+
{{/exclusions}}
58+
</dependency>
59+
{{#last}}
60+
</dependencies>
61+
</dependencyManagement>
62+
63+
{{/last}}
64+
{{/dependencies-management}}
2365
{{#dependencies}}
66+
{{#first}}
67+
<dependencies>
68+
{{/first}}
2469
<dependency>
2570
<groupId>{{groupId}}</groupId>
2671
<artifactId>{{artifactId}}</artifactId>
@@ -49,9 +94,11 @@
4994
{{/first}}
5095
{{/exclusions}}
5196
</dependency>
52-
{{/dependencies}}
97+
{{#last}}
5398
</dependencies>
5499

100+
{{/last}}
101+
{{/dependencies}}
55102
<build>
56103
<plugins>
57104
<plugin>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```shell
2+
curl -X POST -H "Content-Type: application/json" \
3+
-d '{"ssn" : "123-45-6789", "firstName" : "Frank", "lastName" : "Helidon", "dateOfBirth" : "02/14/2019"}' \
4+
http://localhost:8080/creditscore
5+
```
6+
7+
You'll notice a short delay as the application computes the credit score.
8+
Now repeat the same request. You'll see the score is returned instantly
9+
as it is retrieved from the cache.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# helidon-creditscore-mp
2+
3+
Sample Helidon MP applications that uses Coherence CE as a cache for application data.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package {{package}};
2+
3+
import com.oracle.coherence.cdi.Name;
4+
import com.tangosol.net.NamedCache;
5+
import jakarta.enterprise.context.ApplicationScoped;
6+
import jakarta.inject.Inject;
7+
import jakarta.ws.rs.Consumes;
8+
import jakarta.ws.rs.POST;
9+
import jakarta.ws.rs.Path;
10+
import jakarta.ws.rs.Produces;
11+
import jakarta.ws.rs.core.MediaType;
12+
import jakarta.ws.rs.core.Response;
13+
14+
import static java.lang.System.Logger.Level.INFO;
15+
16+
/**
17+
* Coherence resource.
18+
*/
19+
@ApplicationScoped
20+
@Path("/creditscore")
21+
public class CoherenceResource {
22+
23+
private static final System.Logger LOGGER = System.getLogger(CoherenceResource.class.getName());
24+
private static final String CACHE_NAME = "creditScoreCache";
25+
private static final int SCORE_MAX = 800;
26+
private static final int SCORE_MIN = 550;
27+
28+
@Inject
29+
@Name(CACHE_NAME)
30+
private NamedCache<String, Integer> creditScoreCache;
31+
32+
/**
33+
* Generate a credit score.
34+
* @param person Person to generate score for
35+
* @return Person with score populated and ssn redacted
36+
*/
37+
@POST
38+
@Consumes(MediaType.APPLICATION_JSON)
39+
@Produces(MediaType.APPLICATION_JSON)
40+
public Response postCreditScore(Person person) {
41+
42+
if (person.firstName() == null || person.lastName() == null || person.dateOfBirth() == null || person.ssn() == null) {
43+
return Response.status(Response.Status.BAD_REQUEST).entity("Bad request").build();
44+
}
45+
46+
LOGGER.log(INFO, "Computing credit score for " + person.firstName() + " " + person.lastName());
47+
48+
String ssn = person.ssn();
49+
Integer creditScore = creditScoreCache.get(ssn);
50+
51+
if (creditScore == null) {
52+
creditScore = calculateCreditScore(person);
53+
creditScoreCache.put(ssn, creditScore);
54+
}
55+
return Response.ok(
56+
new Person(person.firstName(), person.lastName(), person.dateOfBirth(), "NNN-NN-NNNN", creditScore))
57+
.build();
58+
}
59+
60+
private int calculateCreditScore(Person p) {
61+
int score = Math.abs(p.hashCode()) % SCORE_MAX;
62+
while (score < SCORE_MIN) {
63+
score += 100;
64+
}
65+
// Pause for dramatic effect
66+
sleep();
67+
return score;
68+
}
69+
70+
private void sleep() {
71+
try {
72+
Thread.sleep(2_000L);
73+
} catch (InterruptedException ignored) {
74+
}
75+
}
76+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package {{package}};
2+
3+
public record Person(
4+
String firstName,
5+
String lastName,
6+
String dateOfBirth,
7+
String ssn,
8+
int creditScore) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Coherence configuration
2+
coherence.ttl=0
3+
coherence.localhost=127.0.0.1
4+
coherence.wka=127.0.0.1

0 commit comments

Comments
 (0)