-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractStreamingActor.java
121 lines (106 loc) · 4.03 KB
/
AbstractStreamingActor.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.pekko.streaming;
import java.time.Duration;
import java.util.List;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.event.Logging;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.SourceRef;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.javadsl.StreamRefs;
/**
* Abstract actor that responds to each command by streaming elements from a source to the sender of the command.
*
* @param <C> Type of commands to start a stream.
* @param <E> Type of elements of a stream.
*/
public abstract class AbstractStreamingActor<C, E> extends AbstractActor {
/**
* Logger for this actor.
* Intentionally _NOT_ instantiated with DittoLoggerFactory as this would lead to scala-java compile problems within
* the "ditto-internal-utils-pekko" module:
*/
protected final DiagnosticLoggingAdapter log = Logging.apply(this);
/**
* Actor materializer of this actor's system.
*/
protected final Materializer materializer = Materializer.createMaterializer(this::getContext);
/**
* @return Class of the commands.
*/
protected abstract Class<C> getCommandClass();
/**
* Extract batch size from a command. The rate specifies the number of elements to be sent per message.
*
* @param command The command to start a stream.
* @return The number of elements to be streamed per second.
*/
protected abstract int getBurst(final C command);
/**
* Extract initial timeout.
*
* @param command The command to start a stream.
* @return The initial timeout.
*/
protected abstract Duration getInitialTimeout(final C command);
/**
* Extract idle timeout.
*
* @param command The command to start a stream.
* @return The idle timeout.
*/
protected abstract Duration getIdleTimeout(final C command);
/**
* Starts a source of elements according to the command.
*
* @param command The command to start a stream.
* @return A source of elements to stream to the recipient.
*/
protected abstract Source<E, NotUsed> createSource(final C command);
/**
* Batch elements together into 1 message. Default to the first element of the list if it is a singleton and the
* list itself otherwise.
*
* @param elements Elements from the source.
* @return A batched message.
*/
protected Object batchMessages(final List<E> elements) {
return elements.size() == 1
? elements.get(0)
: elements;
}
@Override
public final Receive createReceive() {
return ReceiveBuilder.create()
.match(getCommandClass(), this::startStreaming)
.matchAny(message -> log.warning("Unexpected message: <{}>", message))
.build();
}
private void startStreaming(final C command) {
log.debug("Starting streaming due to command: {}", command);
final int burst = getBurst(command);
final Duration initialTimeout = getInitialTimeout(command);
final Duration idleTimeout = getIdleTimeout(command);
final SourceRef<Object> sourceRef = createSource(command)
.grouped(burst)
.map(this::batchMessages)
.initialTimeout(initialTimeout)
.idleTimeout(idleTimeout)
.runWith(StreamRefs.sourceRef(), materializer);
getSender().tell(sourceRef, getSelf());
}
}