-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractConnectionsRetrievalActor.java
103 lines (86 loc) · 4.19 KB
/
AbstractConnectionsRetrievalActor.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
/*
* Copyright (c) 2022 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.gateway.service.endpoints.actors;
import java.time.Duration;
import org.eclipse.ditto.connectivity.model.ConnectivityInternalErrorException;
import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityErrorResponse;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveConnections;
import org.eclipse.ditto.gateway.service.util.config.DittoGatewayConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.CommandConfig;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ReceiveTimeout;
import akka.japi.pf.ReceiveBuilder;
/**
* Abstract actor for retrieving multiple connections.
* Implementers should add the custom retrieval logic.
*/
public abstract class AbstractConnectionsRetrievalActor extends AbstractActor {
protected final ThreadSafeDittoLogger logger = DittoLoggerFactory.getThreadSafeLogger(getClass());
protected final ActorRef edgeCommandForwarder;
protected final ActorRef sender;
protected final int connectionsRetrieveLimit;
protected final Duration defaultTimeout;
protected RetrieveConnections initialCommand;
protected AbstractConnectionsRetrievalActor(final ActorRef edgeCommandForwarder, final ActorRef sender) {
this.edgeCommandForwarder = edgeCommandForwarder;
this.sender = sender;
CommandConfig commandConfig =
DittoGatewayConfig.of(DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config()))
.getCommandConfig();
this.connectionsRetrieveLimit = commandConfig.connectionsRetrieveLimit();
this.defaultTimeout = commandConfig.getDefaultTimeout();
getContext().setReceiveTimeout(defaultTimeout);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveConnections.class, this::handleRetrieveConnections)
.match(ReceiveTimeout.class, timeout -> handleTimeout())
.matchAny(msg -> logger.warn("Unknown message: <{}>", msg))
.build();
}
protected abstract void retrieveConnections(final RetrieveConnections retrieveConnections);
protected abstract Receive commandResponseAwaitingBehaviour();
private Receive responseAwaitingBehavior(){
return commandResponseAwaitingBehaviour().orElse(ReceiveBuilder.create()
.match(ReceiveTimeout.class, timeout -> handleTimeout())
.matchAny(msg -> logger.warn("Unknown message: <{}>", msg))
.build());
}
private void handleRetrieveConnections(RetrieveConnections retrieveConnections) {
getContext().become(responseAwaitingBehavior());
this.initialCommand = retrieveConnections;
retrieveConnections(retrieveConnections);
}
protected void handleTimeout() {
ConnectivityInternalErrorException.Builder builder = ConnectivityInternalErrorException.newBuilder();
ConnectivityErrorResponse response;
if (initialCommand != null) {
builder.dittoHeaders(initialCommand.getDittoHeaders())
.message("RetrieveConnections command timed out.");
response = ConnectivityErrorResponse.of(builder.build(), initialCommand.getDittoHeaders());
} else {
builder.message("Actor time out. command timed out.");
response = ConnectivityErrorResponse.of(ConnectivityInternalErrorException.newBuilder().build());
}
sender.tell(response, getSelf());
stop();
}
void stop() {
getContext().stop(getSelf());
}
}