-
Notifications
You must be signed in to change notification settings - Fork 214
/
DistributedPub.java
108 lines (96 loc) · 4.15 KB
/
DistributedPub.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
/*
* Copyright (c) 2019 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.pubsub;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.internal.utils.pubsub.extractors.PubSubTopicExtractor;
import org.apache.pekko.actor.ActorRef;
/**
* A jolly locale for the spreading of news.
*
* @param <T> type of messages.
*/
public interface DistributedPub<T> {
/**
* Get the publisher actor to send requests to.
*
* @return the publisher actor.
*/
ActorRef getPublisher();
/**
* Wrap the message in an envelope to send to the publisher.
*
* @param message the message.
* @param groupIndexKey the group index key used to select a subscriber from each group. Usually the entity ID
* of a signal to ensure event order for each entity.
* @return the wrapped message to send to the publisher.
*/
Object wrapForPublication(T message, final CharSequence groupIndexKey);
/**
* Wrap the message in an envelope to send to the publisher.
*
* @param message the message to publish.
* @param groupIndexKey the group index key used to select a subscriber from each group. Usually the entity ID
* of a signal to ensure event order for each entity.
* @param ackExtractor extractor of ack-related information from the message.
* @return the wrapped message to send to the publisher.
*/
<S extends T> Object wrapForPublicationWithAcks(S message, final CharSequence groupIndexKey,
AckExtractor<S> ackExtractor);
/**
* Publish a message.
*
* @param message the message to publish.
* @param groupIndexKey the group index key used to select a subscriber from each group. Usually the entity ID
* of a signal to ensure event order for each entity.
* @param sender reply address for all subscribers who receive this message.
*/
default void publish(final T message, final CharSequence groupIndexKey, @Nullable final ActorRef sender) {
getPublisher().tell(wrapForPublication(message, groupIndexKey), sender);
}
/**
* Publish a message with acknowledgement requests.
*
* @param message the message to publish.
* @param groupIndexKey the group index key used to select a subscriber from each group. Usually the entity ID
* of a signal to ensure event order for each entity.
* @param ackExtractor extractor of ack-related information from the message.
* @param sender the sender of the message and the receiver of acknowledgements.
*/
default void publishWithAcks(final T message, final CharSequence groupIndexKey, final AckExtractor<T> ackExtractor,
@Nullable final ActorRef sender) {
getPublisher().tell(wrapForPublicationWithAcks(message, groupIndexKey, ackExtractor), sender);
}
/**
* Create a new interface of this distributed-pub with a new topic extractor.
*
* @param topicExtractor the previous topic extractor.
* @return a new interface of this object.
*/
default <S extends Signal<?>> DistributedPub<S> withTopicExtractor(final PubSubTopicExtractor<S> topicExtractor) {
return DistributedPubWithTopicExtractor.of(this, topicExtractor);
}
/**
* Create publication access from an already-started pub-supervisor and topic extractor.
*
* @param pubSupervisor the pub-supervisor.
* @param topicExtractor the topic extractor.
* @param <T> the type of messages.
* @return the publication access.
*/
static <T extends Signal<?>> DistributedPub<T> of(final ActorRef pubSupervisor,
final PubSubTopicExtractor<T> topicExtractor) {
return new DistributedPubImpl<>(pubSupervisor, topicExtractor);
}
}