@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package example.springdata.mongodb;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@@ -25,13 +22,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
|
||||
* @author Christoph Strobl
|
||||
* @currentRead The Core - Peter V. Brett
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Document("processes")
|
||||
public class Process {
|
||||
|
||||
@Id Integer id;
|
||||
State state;
|
||||
int transitionCount;
|
||||
public record Process(@Id Integer id, State state, int transitionCount) {
|
||||
|
||||
}
|
||||
|
||||
@@ -49,9 +49,9 @@ class TransitionService {
|
||||
@Transactional
|
||||
public void run(Integer id) {
|
||||
|
||||
Process process = lookup(id);
|
||||
var process = lookup(id);
|
||||
|
||||
if (!State.CREATED.equals(process.getState())) {
|
||||
if (!State.CREATED.equals(process.state())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -62,13 +62,13 @@ class TransitionService {
|
||||
|
||||
private void finish(Process process) {
|
||||
|
||||
template.update(Process.class).matching(Query.query(Criteria.where("id").is(process.getId())))
|
||||
template.update(Process.class).matching(Query.query(Criteria.where("id").is(process.id())))
|
||||
.apply(Update.update("state", State.DONE).inc("transitionCount", 1)).first();
|
||||
}
|
||||
|
||||
void start(Process process) {
|
||||
|
||||
template.update(Process.class).matching(Query.query(Criteria.where("id").is(process.getId())))
|
||||
template.update(Process.class).matching(Query.query(Criteria.where("id").is(process.id())))
|
||||
.apply(Update.update("state", State.ACTIVE).inc("transitionCount", 1)).first();
|
||||
}
|
||||
|
||||
@@ -77,6 +77,6 @@ class TransitionService {
|
||||
}
|
||||
|
||||
void verify(Process process) {
|
||||
Assert.state(process.getId() % 3 != 0, "We're sorry but we needed to drop that one");
|
||||
Assert.state(process.id() % 3 != 0, "We're sorry but we needed to drop that one");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,21 +52,21 @@ public class ReactiveManagedTransitionService {
|
||||
|
||||
return lookup(id) //
|
||||
.flatMap(process -> start(template, process)) //
|
||||
.flatMap(it -> verify(it)) //
|
||||
.flatMap(this::verify) //
|
||||
.flatMap(process -> finish(template, process)) //
|
||||
.map(Process::getId);
|
||||
.map(Process::id);
|
||||
}
|
||||
|
||||
private Mono<Process> finish(ReactiveMongoOperations operations, Process process) {
|
||||
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.getId())))
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.id())))
|
||||
.apply(Update.update("state", State.DONE).inc("transitionCount", 1)).first() //
|
||||
.then(Mono.just(process));
|
||||
}
|
||||
|
||||
Mono<Process> start(ReactiveMongoOperations operations, Process process) {
|
||||
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.getId())))
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.id())))
|
||||
.apply(Update.update("state", State.ACTIVE).inc("transitionCount", 1)).first() //
|
||||
.then(Mono.just(process));
|
||||
}
|
||||
@@ -77,7 +77,7 @@ public class ReactiveManagedTransitionService {
|
||||
|
||||
Mono<Process> verify(Process process) {
|
||||
|
||||
Assert.state(process.getId() % 3 != 0, "We're sorry but we needed to drop that one");
|
||||
Assert.state(process.id() % 3 != 0, "We're sorry but we needed to drop that one");
|
||||
return Mono.just(process);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,19 +56,19 @@ public class ReactiveTransitionService {
|
||||
.flatMap(it -> verify(it)) //
|
||||
.flatMap(process -> finish(action, process));
|
||||
|
||||
}).next().map(Process::getId);
|
||||
}).next().map(Process::id);
|
||||
}
|
||||
|
||||
private Mono<Process> finish(ReactiveMongoOperations operations, Process process) {
|
||||
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.getId())))
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.id())))
|
||||
.apply(Update.update("state", State.DONE).inc("transitionCount", 1)).first() //
|
||||
.then(Mono.just(process));
|
||||
}
|
||||
|
||||
Mono<Process> start(ReactiveMongoOperations operations, Process process) {
|
||||
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.getId())))
|
||||
return operations.update(Process.class).matching(Query.query(Criteria.where("id").is(process.id())))
|
||||
.apply(Update.update("state", State.ACTIVE).inc("transitionCount", 1)).first() //
|
||||
.then(Mono.just(process));
|
||||
}
|
||||
@@ -79,7 +79,7 @@ public class ReactiveTransitionService {
|
||||
|
||||
Mono<Process> verify(Process process) {
|
||||
|
||||
Assert.state(process.getId() % 3 != 0, "We're sorry but we needed to drop that one");
|
||||
Assert.state(process.id() % 3 != 0, "We're sorry but we needed to drop that one");
|
||||
return Mono.just(process);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,13 +88,13 @@ public class TransitionServiceTests {
|
||||
@Test
|
||||
public void txCommitRollback() {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
|
||||
Process process = transitionService.newProcess();
|
||||
var process = transitionService.newProcess();
|
||||
|
||||
try {
|
||||
|
||||
transitionService.run(process.getId());
|
||||
transitionService.run(process.id());
|
||||
Assertions.assertThat(stateInDb(process)).isEqualTo(State.DONE);
|
||||
} catch (IllegalStateException e) {
|
||||
Assertions.assertThat(stateInDb(process)).isEqualTo(State.CREATED);
|
||||
@@ -107,7 +107,7 @@ public class TransitionServiceTests {
|
||||
|
||||
State stateInDb(Process process) {
|
||||
|
||||
return State.valueOf(client.getDatabase(DB_NAME).getCollection("processes").find(Filters.eq("_id", process.getId()))
|
||||
return State.valueOf(client.getDatabase(DB_NAME).getCollection("processes").find(Filters.eq("_id", process.id()))
|
||||
.projection(Projections.include("state")).first().get("state", String.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -86,9 +86,9 @@ public class ReactiveManagedTransitionServiceTests {
|
||||
@Test
|
||||
public void reactiveTxCommitRollback() {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
managedTransitionService.newProcess() //
|
||||
.map(Process::getId) //
|
||||
.map(Process::id) //
|
||||
.flatMap(managedTransitionService::run) //
|
||||
.onErrorReturn(-1).as(StepVerifier::create) //
|
||||
.consumeNextWith(val -> {}) //
|
||||
@@ -100,7 +100,7 @@ public class ReactiveManagedTransitionServiceTests {
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith(list -> {
|
||||
|
||||
for (Document document : list) {
|
||||
for (var document : list) {
|
||||
|
||||
System.out.println("document: " + document);
|
||||
|
||||
|
||||
@@ -76,9 +76,9 @@ public class ReactiveTransitionServiceTests {
|
||||
@Test
|
||||
public void reactiveTxCommitRollback() {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
transitionService.newProcess() //
|
||||
.map(Process::getId) //
|
||||
.map(Process::id) //
|
||||
.flatMap(transitionService::run) //
|
||||
.onErrorReturn(-1).as(StepVerifier::create) //
|
||||
.consumeNextWith(val -> {}) //
|
||||
@@ -90,7 +90,7 @@ public class ReactiveTransitionServiceTests {
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith(list -> {
|
||||
|
||||
for (Document document : list) {
|
||||
for (var document : list) {
|
||||
|
||||
System.out.println("document: " + document);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user