Skip to content

Commit 95262fb

Browse files
committed
Polish code for chapter 7
1 parent 3303b6b commit 95262fb

26 files changed

Lines changed: 113 additions & 70 deletions

File tree

10/part1/images/src/main/java/com/greglturnquist/learningspringboot/ops/CustomMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.greglturnquist.learningspringboot.ops;
1717

18-
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
18+
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
1919
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.stereotype.Component;

10/part1/images/src/main/resources/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h4 th:text="${page.size} + ' item(s) per page'" />
1414
</tr>
1515
</thead>
1616
<tbody>
17-
<tr th:each="image : ${page.content}">
17+
<tr th:each="image : ${images}">
1818
<td th:text="${image.id}" />
1919
<td th:text="${image.name}" />
2020
<td><a th:href="@{'/images/' + ${image.name} + '/raw'}"><img th:src="@{'/images/' + ${image.name} + '/raw'}" class="thumbnail" /></a></td>

5/part2/src/main/java/com/greglturnquist/learningspringboot/CustomMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.greglturnquist.learningspringboot;
1717

18-
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
18+
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
1919
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.stereotype.Component;

6/part1/src/main/java/com/greglturnquist/learningspringboot/ops/CustomMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.greglturnquist.learningspringboot.ops;
1717

18-
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
18+
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
1919
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.stereotype.Component;

6/part2/src/main/java/com/greglturnquist/learningspringboot/CommentSimulator.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package com.greglturnquist.learningspringboot;
1717

18+
import java.time.Duration;
1819
import java.util.concurrent.atomic.AtomicInteger;
1920

20-
import reactor.core.publisher.Mono;
21-
21+
import org.springframework.boot.context.event.ApplicationReadyEvent;
2222
import org.springframework.context.annotation.Profile;
23-
import org.springframework.scheduling.annotation.Scheduled;
23+
import org.springframework.context.event.EventListener;
2424
import org.springframework.stereotype.Component;
25+
import reactor.core.publisher.Flux;
26+
import reactor.core.publisher.Mono;
2527

2628
import com.greglturnquist.learningspringboot.comments.Comment;
2729
import com.greglturnquist.learningspringboot.comments.CommentController;
@@ -47,19 +49,23 @@ public CommentSimulator(CommentController controller,
4749
this.counter = new AtomicInteger(1);
4850
}
4951

50-
@Scheduled(fixedRate = 100)
51-
public void simulateActivity() {
52-
repository
53-
.findAll()
52+
@EventListener
53+
public void onApplicationReadyEvent(ApplicationReadyEvent event) {
54+
Flux
55+
.interval(Duration.ofMillis(1000))
56+
.flatMap(tick -> repository.findAll())
5457
.map(image -> {
5558
Comment comment = new Comment();
5659
comment.setImageId(image.getId());
5760
comment.setComment(
5861
"Comment #" + counter.getAndIncrement());
5962
return Mono.just(comment);
6063
})
61-
.map(controller::addComment)
64+
.flatMap(newComment ->
65+
Mono.defer(() ->
66+
controller.addComment(newComment)))
6267
.subscribe();
6368
}
69+
6470
}
6571
// end::tag[]

6/part2/src/main/java/com/greglturnquist/learningspringboot/ops/CustomMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.greglturnquist.learningspringboot.ops;
1717

18-
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
18+
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
1919
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.stereotype.Component;

6/part3/src/main/java/com/greglturnquist/learningspringboot/CommentSimulator.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package com.greglturnquist.learningspringboot;
1717

18+
import java.time.Duration;
1819
import java.util.concurrent.atomic.AtomicInteger;
1920

20-
import reactor.core.publisher.Mono;
21-
21+
import org.springframework.boot.context.event.ApplicationReadyEvent;
2222
import org.springframework.context.annotation.Profile;
23-
import org.springframework.scheduling.annotation.Scheduled;
23+
import org.springframework.context.event.EventListener;
2424
import org.springframework.stereotype.Component;
25+
import reactor.core.publisher.Flux;
26+
import reactor.core.publisher.Mono;
2527

2628
import com.greglturnquist.learningspringboot.comments.Comment;
2729
import com.greglturnquist.learningspringboot.comments.CommentController;
@@ -47,19 +49,23 @@ public CommentSimulator(CommentController controller,
4749
this.counter = new AtomicInteger(1);
4850
}
4951

50-
@Scheduled(fixedRate = 100)
51-
public void simulateActivity() {
52-
repository
53-
.findAll()
52+
@EventListener
53+
public void onApplicationReadyEvent(ApplicationReadyEvent event) {
54+
Flux
55+
.interval(Duration.ofMillis(1000))
56+
.flatMap(tick -> repository.findAll())
5457
.map(image -> {
5558
Comment comment = new Comment();
5659
comment.setImageId(image.getId());
5760
comment.setComment(
5861
"Comment #" + counter.getAndIncrement());
5962
return Mono.just(comment);
6063
})
61-
.map(controller::addComment)
64+
.flatMap(newComment ->
65+
Mono.defer(() ->
66+
controller.addComment(newComment)))
6267
.subscribe();
6368
}
69+
6470
}
6571
// end::tag[]

6/part3/src/main/java/com/greglturnquist/learningspringboot/ops/CustomMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.greglturnquist.learningspringboot.ops;
1717

18-
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
18+
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
1919
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.stereotype.Component;

7/part1/eureka-server/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// tag::version[]
12
buildscript {
23
ext {
34
springBootVersion = '2.0.0.BUILD-SNAPSHOT'
45
springCloudVersion = 'Finchley.BUILD-SNAPSHOT'
56
}
7+
// end::version[]
68
repositories {
79
mavenCentral()
810
maven { url "https://repo.spring.io/snapshot" }

7/part1/images/src/main/java/com/greglturnquist/learningspringboot/CommentSimulator.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package com.greglturnquist.learningspringboot;
1717

18+
import java.time.Duration;
1819
import java.util.concurrent.atomic.AtomicInteger;
1920

20-
import reactor.core.publisher.Mono;
21-
21+
import org.springframework.boot.context.event.ApplicationReadyEvent;
2222
import org.springframework.context.annotation.Profile;
23-
import org.springframework.scheduling.annotation.Scheduled;
23+
import org.springframework.context.event.EventListener;
2424
import org.springframework.stereotype.Component;
25+
import reactor.core.publisher.Flux;
26+
import reactor.core.publisher.Mono;
2527

2628
import com.greglturnquist.learningspringboot.images.Comment;
2729
import com.greglturnquist.learningspringboot.images.CommentController;
@@ -47,19 +49,23 @@ public CommentSimulator(CommentController controller,
4749
this.counter = new AtomicInteger(1);
4850
}
4951

50-
@Scheduled(fixedRate = 100)
51-
public void simulateActivity() {
52-
repository
53-
.findAll()
52+
@EventListener
53+
public void simulateComments(ApplicationReadyEvent event) {
54+
Flux
55+
.interval(Duration.ofMillis(1000))
56+
.flatMap(tick -> repository.findAll())
5457
.map(image -> {
5558
Comment comment = new Comment();
5659
comment.setImageId(image.getId());
5760
comment.setComment(
5861
"Comment #" + counter.getAndIncrement());
5962
return Mono.just(comment);
6063
})
61-
.map(controller::addComment)
64+
.flatMap(newComment ->
65+
Mono.defer(() ->
66+
controller.addComment(newComment)))
6267
.subscribe();
6368
}
69+
6470
}
6571
// end::tag[]

0 commit comments

Comments
 (0)