-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractPubSubListenerActor.java
75 lines (63 loc) · 2.63 KB
/
AbstractPubSubListenerActor.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
/*
* 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.cluster;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.Set;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.cluster.pubsub.DistributedPubSubMediator;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Listens to events via PubSub and invokes the abstract {@link #handleEvents()} for each received event.
*/
public abstract class AbstractPubSubListenerActor extends AbstractActor {
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
/**
* Creates a cache updater.
*
* @param pubSubMediator the DistributedPubSubMediator for registering for events.
* @param eventTopics the event topics to register for.
*/
protected AbstractPubSubListenerActor(final ActorRef pubSubMediator,
final Set<String> eventTopics) {
checkNotNull(eventTopics, "Event Topics");
eventTopics.forEach(topic -> {
log.info("Subscribing for pub/sub topic <{}>", topic);
pubSubMediator.tell(subscribe(topic), getSelf());
});
}
@Override
public Receive createReceive() {
return handleEvents().orElse(ReceiveBuilder.create()
.match(DistributedPubSubMediator.SubscribeAck.class, subscribeAck ->
log.debug("Got SubscribeAck about topic <{}> for group <{}>",
subscribeAck.subscribe().topic(),
subscribeAck.subscribe().group()))
.matchAny(message -> {
log.warning("Unhandled message <{}>", message);
unhandled(message);
})
.build());
}
/**
* Return a partial function to handle events this actor subscribes to.
*
* @return a partial function.
*/
protected abstract Receive handleEvents();
private DistributedPubSubMediator.Subscribe subscribe(final String topic) {
return DistPubSubAccess.subscribe(topic, getSelf());
}
}