This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AccountExtractProcessor.java
73 lines (57 loc) · 2.32 KB
/
AccountExtractProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (C) 2016-2017 Lightbend Inc. <https://www.lightbend.com>
*/
package com.lightbend.lagom.account.impl.readside;
import akka.Done;
import akka.japi.Pair;
import akka.stream.javadsl.Flow;
import com.lightbend.lagom.account.impl.AccountEvent;
import com.lightbend.lagom.javadsl.persistence.AggregateEventTag;
import com.lightbend.lagom.javadsl.persistence.Offset;
import com.lightbend.lagom.javadsl.persistence.ReadSideProcessor;
import org.pcollections.PSequence;
import org.pcollections.TreePVector;
import javax.inject.Inject;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
/**
* A read-side processor that subscribes to AccountEntity events and forward them to AccountExtractRepository.
*
* This processor does not save offsets and consumes the full event stream from scratch each time the application is restarted.
* This is done on purpose. The repository does not save to any database, but keeps accumulating Account Extracts in-memory before
* archiving it in Cloud Object Storage.
*/
public class AccountExtractProcessor extends ReadSideProcessor<AccountEvent> {
final private AccountExtractRepositoryImpl repository;
@Inject
public AccountExtractProcessor(AccountExtractRepositoryImpl repository) {
this.repository = repository;
}
@Override
public PSequence<AggregateEventTag<AccountEvent>> aggregateTags() {
return TreePVector.singleton(AccountEvent.TAG);
}
@Override
public ReadSideHandler<AccountEvent> buildHandler() {
return new ReadSideHandler<AccountEvent>() {
@Override
public CompletionStage<Done> globalPrepare() {
// no global prepare, nothing to store in DB
return CompletableFuture.completedFuture(Done.getInstance());
}
@Override
public CompletionStage<Offset> prepare(AggregateEventTag<AccountEvent> tag) {
// always start reading from first event, not offset storage
return CompletableFuture.completedFuture(Offset.NONE);
}
@Override
public Flow<Pair<AccountEvent, Offset>, Done, ?> handle() {
return Flow.<Pair<AccountEvent, Offset>>create()
.mapAsync(
1,
eventAndOffset -> repository.handleEvent(eventAndOffset.first(), eventAndOffset.second())
);
}
};
}
}