Skip to content

Commit 8a17a79

Browse files
committed
feat(*): step 6
1 parent 3978102 commit 8a17a79

File tree

23 files changed

+148
-133
lines changed

23 files changed

+148
-133
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Steps:
6363
- Add a configuration property inside you `application.properties` file: `greeting=World`
6464
- Modify `BookmarkResource` to read this configuration property and log it at startup:
6565
- Use `@ConfigProperty` to inject the property inside a `greeting` variable.
66-
- Create a `@PostConstruct` method that log it via `log.info("Hello {}", greeting)` using [SLFJ](https://www.slf4j.org/).
66+
- Create a `@PostConstruct` method that log it via `LOGGER.infof("Hello %s", greeting)` using [Jboss logging](https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html).
6767
- Test using `mvn quarkus:dev` you should see `Hello World` in the console.
6868
- Add a new line to `application.properties` file: `%dev.greeting=Dev` this is an override of the same property for the dev profile.
6969
- Test using `mvn quarkus:dev` you should see `Hello Dev` in the console.

bookmark-message-consumer/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<surefire-plugin.version>2.22.0</surefire-plugin.version>
1010
<maven.compiler.target>1.8</maven.compiler.target>
1111
<maven.compiler.source>1.8</maven.compiler.source>
12-
<quarkus.version>0.22.0</quarkus.version>
12+
<quarkus.version>1.0.1.Final</quarkus.version>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1515
</properties>
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package fr.loicmathieu.bookmarkit;
22

33
import org.eclipse.microprofile.reactive.messaging.Incoming;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
4+
import org.jboss.logging.Logger;
65

76
import javax.enterprise.context.ApplicationScoped;
87

98
@ApplicationScoped
109
public class BookmarkConsumer {
1110

12-
private final Logger log = LoggerFactory.getLogger(BookmarkConsumer.class);
11+
private static final Logger LOG = Logger.getLogger(BookmarkConsumer.class);
1312

1413
@Incoming("bookmarks")
1514
public void process(String bookmark) {
16-
log.info("Indexing a bookmark: {}", bookmark);
15+
LOG.infof("Indexing a bookmark: %s", bookmark);
1716
}
1817
}

bookmark-message-consumer/src/test/java/fr/loicmathieu/bookmarkit/NativeBookmarkConsumerIT.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

bookmark-message-consumer/target/classes/application.properties

Lines changed: 0 additions & 8 deletions
This file was deleted.

bookmark-message-consumer/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst

Lines changed: 0 additions & 1 deletion
This file was deleted.

bookmark-service/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<surefire-plugin.version>2.22.0</surefire-plugin.version>
1010
<maven.compiler.target>1.8</maven.compiler.target>
1111
<maven.compiler.source>1.8</maven.compiler.source>
12-
<quarkus.version>0.26.1</quarkus.version>
12+
<quarkus.version>1.0.1.Final</quarkus.version>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1515
</properties>
@@ -42,10 +42,13 @@
4242
<groupId>io.quarkus</groupId>
4343
<artifactId>quarkus-smallrye-health</artifactId>
4444
</dependency>
45+
<!-- Step 5 dependencies -->
4546
<dependency>
4647
<groupId>io.quarkus</groupId>
4748
<artifactId>quarkus-jdbc-postgresql</artifactId>
4849
</dependency>
50+
<!--/ Step 5 dependencies -->
51+
<!-- Step 6 dependencies -->
4952
<dependency>
5053
<groupId>io.quarkus</groupId>
5154
<artifactId>quarkus-smallrye-metrics</artifactId>
@@ -79,6 +82,7 @@
7982
<artifactId>rest-assured</artifactId>
8083
<scope>test</scope>
8184
</dependency>
85+
<!-- test dependencies-->
8286
</dependencies>
8387
<build>
8488
<plugins>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package fr.loicmathieu.bookmarkit;
2+
3+
import org.eclipse.microprofile.health.HealthCheck;
4+
import org.eclipse.microprofile.health.HealthCheckResponse;
5+
import org.eclipse.microprofile.health.Readiness;
6+
7+
@Readiness
8+
public class AppHealthCheck implements HealthCheck {
9+
10+
@Override
11+
public HealthCheckResponse call() {
12+
return HealthCheckResponse.builder().name("bookmark").withData("is", "always").up().build();
13+
}
14+
}
Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package fr.loicmathieu.bookmarkit;
22

3+
import io.smallrye.reactive.messaging.annotations.Channel;
34
import io.smallrye.reactive.messaging.annotations.Emitter;
4-
import io.smallrye.reactive.messaging.annotations.Stream;
55
import org.eclipse.microprofile.config.inject.ConfigProperty;
6+
import org.eclipse.microprofile.faulttolerance.Retry;
67
import org.eclipse.microprofile.metrics.annotation.Counted;
78
import org.eclipse.microprofile.metrics.annotation.Timed;
89
import org.eclipse.microprofile.openapi.annotations.Operation;
9-
import org.eclipse.microprofile.reactive.messaging.Outgoing;
10+
import org.eclipse.microprofile.rest.client.inject.RestClient;
11+
import org.jboss.logging.Logger;
1012

1113
import javax.annotation.PostConstruct;
12-
import javax.inject.Inject;
14+
import javax.annotation.PreDestroy;
1315
import javax.transaction.Transactional;
16+
import javax.validation.Valid;
1417
import javax.ws.rs.Consumes;
1518
import javax.ws.rs.DELETE;
1619
import javax.ws.rs.GET;
@@ -19,37 +22,47 @@
1922
import javax.ws.rs.Path;
2023
import javax.ws.rs.PathParam;
2124
import javax.ws.rs.Produces;
25+
import javax.ws.rs.core.Context;
2226
import javax.ws.rs.core.MediaType;
2327
import javax.ws.rs.core.Response;
28+
import javax.ws.rs.core.UriInfo;
2429
import java.net.URI;
2530
import java.util.List;
2631

2732
@Path("/bookmarks")
2833
@Produces(MediaType.APPLICATION_JSON)
2934
@Consumes(MediaType.APPLICATION_JSON)
3035
public class BookmarkResource {
31-
@ConfigProperty(name="greeting") String greeting;
3236

33-
@Inject @Stream("bookmarks") Emitter<Bookmark> emitter;
37+
private static final Logger LOG = Logger.getLogger(BookmarkResource.class);
38+
39+
@ConfigProperty(name = "greeting")
40+
private String greeting;
41+
42+
@Channel("bookmarks")
43+
private Emitter<Bookmark> emitter;
44+
45+
@RestClient
46+
GeoIpService geoIpService;
3447

3548
@PostConstruct
36-
void init(){
37-
System.out.println("Hello " + greeting);
49+
void init() {
50+
LOG.infof("Hello %s", greeting);
3851
}
3952

4053
@GET
4154
@Operation(summary = "List all bookmarks")
4255
@Counted(name = "listAll.count")
43-
@Timed(name="listAll.time")
44-
public List<Bookmark> listAll(){
56+
@Timed(name = "listAll.time")
57+
public List<Bookmark> listAll() {
4558
return Bookmark.listAll();
4659
}
4760

4861
@GET
49-
@Path("/{id}")
62+
@Path("{id}")
5063
@Operation(summary = "Get a bookmark")
5164
@Counted(name = "get.count")
52-
@Timed(name="get.time")
65+
@Timed(name = "get.time")
5366
public Bookmark get(@PathParam("id") Long id) {
5467
return Bookmark.findById(id);
5568
}
@@ -58,34 +71,44 @@ public Bookmark get(@PathParam("id") Long id) {
5871
@Transactional
5972
@Operation(summary = "Create a bookmark")
6073
@Counted(name = "create.count")
61-
@Timed(name="create.time")
62-
public Response create(Bookmark bookmark){
63-
bookmark.persist();
64-
emitter.send(bookmark);
65-
return Response.created(URI.create("/bookmarks/" + bookmark.id)).build();
74+
@Timed(name = "create.time")
75+
@Retry(maxRetries = 2)
76+
public Response create(@Valid Bookmark bookmark) {
77+
GeoIp geoIp = geoIpService.getIpInfos();
78+
if (geoIp != null) {
79+
bookmark.location = geoIp.city;
80+
bookmark.persist();
81+
this.emitter.send(bookmark);
82+
return Response.status(Response.Status.CREATED).entity(bookmark).build();
83+
} else {
84+
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
85+
}
6686
}
6787

6888
@PUT
69-
@Path("/{id}")
89+
@Path("{id}")
7090
@Transactional
7191
@Operation(summary = "Update a bookmark")
7292
@Counted(name = "update.count")
73-
@Timed(name="update.time")
74-
public void update(Bookmark bookmark){
75-
Bookmark existing = Bookmark.findById(bookmark.id);
76-
existing.url = bookmark.url;
77-
existing.description = bookmark.description;
78-
existing.title = bookmark.title;
93+
@Timed(name = "update.time")
94+
public Response update(@Valid Bookmark bookmark, @PathParam("id") Long id, @Context UriInfo uriInfo) {
95+
Bookmark entity = Bookmark.findById(id);
96+
entity.description = bookmark.description;
97+
entity.location = bookmark.location;
98+
entity.title = bookmark.title;
99+
entity.url = bookmark.url;
100+
return Response.created(URI.create(uriInfo.getPath())).build();
79101
}
80102

81103
@DELETE
82-
@Path("/{id}")
104+
@Path("{id}")
83105
@Transactional
84106
@Operation(summary = "Delete a bookmark")
85107
@Counted(name = "delete.count")
86-
@Timed(name="delete.time")
87-
public void delete(@PathParam("id")Long id){
88-
Bookmark existing = Bookmark.findById(id);
89-
existing.delete();
108+
@Timed(name = "delete.time")
109+
public Response delete(@PathParam("id") Long id) {
110+
Bookmark bookmark = Bookmark.findById(id);
111+
bookmark.delete();
112+
return Response.noContent().build();
90113
}
91114
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fr.loicmathieu.bookmarkit;
2+
3+
public class GeoIp {
4+
public String query;
5+
public String status;
6+
public String country;
7+
public String countryCode;
8+
public String region;
9+
public String regionName;
10+
public String city;
11+
public String zip;
12+
public Double lat;
13+
public Double lon;
14+
public String timezone;
15+
public String isp;
16+
public String org;
17+
public String as;
18+
}

0 commit comments

Comments
 (0)