-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultMqttConfig.java
169 lines (147 loc) · 6.56 KB
/
DefaultMqttConfig.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2020 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.connectivity.service.config;
import java.time.Duration;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.service.config.ThrottlingConfig;
import org.eclipse.ditto.internal.utils.config.ConfigWithFallback;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
/**
* This class is the default implementation of {@link MqttConfig}.
*/
@Immutable
final class DefaultMqttConfig implements MqttConfig {
private static final String CONFIG_PATH = "mqtt";
private static final String RECONNECT_PATH = "reconnect";
private final int maxQueueSize;
private final int eventLoopThreads;
private final boolean cleanSession;
private final boolean reconnectForRedelivery;
private final Duration reconnectForRedeliveryDelay;
private final boolean useSeparateClientForPublisher;
private final Duration reconnectMinTimeoutForMqttBrokerInitiatedDisconnect;
private final BackOffConfig reconnectBackOffConfig;
private final ThrottlingConfig consumerThrottlingConfig;
private DefaultMqttConfig(final ScopedConfig config) {
eventLoopThreads = config.getNonNegativeIntOrThrow(MqttConfigValue.EVENT_LOOP_THREADS);
cleanSession = config.getBoolean(MqttConfigValue.CLEAN_SESSION.getConfigPath());
reconnectForRedelivery = config.getBoolean(MqttConfigValue.RECONNECT_FOR_REDELIVERY.getConfigPath());
reconnectForRedeliveryDelay =
config.getNonNegativeDurationOrThrow(MqttConfigValue.RECONNECT_FOR_REDELIVERY_DELAY);
useSeparateClientForPublisher = config.getBoolean(MqttConfigValue.SEPARATE_PUBLISHER_CLIENT.getConfigPath());
maxQueueSize = config.getInt(MqttConfigValue.MAX_QUEUE_SIZE.getConfigPath());
reconnectMinTimeoutForMqttBrokerInitiatedDisconnect = config.getNonNegativeDurationOrThrow(
MqttConfigValue.RECONNECT_MIN_TIMEOUT_FOR_MQTT_BROKER_INITIATED_DISCONNECT);
reconnectBackOffConfig = DefaultBackOffConfig.of(config.hasPath(RECONNECT_PATH)
? config.getConfig(RECONNECT_PATH)
: ConfigFactory.parseString("backoff" + "={}"));
consumerThrottlingConfig = ThrottlingConfig.of(config);
}
/**
* Returns an instance of {@code DefaultMqttConfig} based on the settings of the specified Config.
*
* @param config is supposed to provide the settings of the JavaScript mapping config at {@value #CONFIG_PATH}.
* @return the instance.
* @throws org.eclipse.ditto.internal.utils.config.DittoConfigError if {@code config} is invalid.
*/
public static DefaultMqttConfig of(final Config config) {
return new DefaultMqttConfig(ConfigWithFallback.newInstance(config, CONFIG_PATH, MqttConfigValue.values()));
}
@Override
public int getEventLoopThreads() {
return eventLoopThreads;
}
@Override
public boolean isCleanSession() {
return cleanSession;
}
@Override
public boolean shouldReconnectForRedelivery() {
return reconnectForRedelivery;
}
@Override
public Duration getReconnectForRedeliveryDelay() {
return reconnectForRedeliveryDelay;
}
@Override
public boolean shouldUseSeparatePublisherClient() {
return useSeparateClientForPublisher;
}
@Override
public Duration getReconnectMinTimeoutForMqttBrokerInitiatedDisconnect() {
return reconnectMinTimeoutForMqttBrokerInitiatedDisconnect;
}
@Override
public int getMaxQueueSize() {
return maxQueueSize;
}
@Override
public ThrottlingConfig getConsumerThrottlingConfig() {
return consumerThrottlingConfig;
}
@Override
public BackOffConfig getReconnectBackOffConfig() {
return reconnectBackOffConfig;
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (DefaultMqttConfig) o;
return Objects.equals(eventLoopThreads, that.eventLoopThreads) &&
Objects.equals(cleanSession, that.cleanSession) &&
Objects.equals(reconnectForRedelivery, that.reconnectForRedelivery) &&
Objects.equals(reconnectForRedeliveryDelay, that.reconnectForRedeliveryDelay) &&
Objects.equals(useSeparateClientForPublisher, that.useSeparateClientForPublisher) &&
Objects.equals(reconnectMinTimeoutForMqttBrokerInitiatedDisconnect,
that.reconnectMinTimeoutForMqttBrokerInitiatedDisconnect) &&
Objects.equals(maxQueueSize, that.maxQueueSize) &&
Objects.equals(reconnectBackOffConfig, that.reconnectBackOffConfig) &&
Objects.equals(consumerThrottlingConfig, that.consumerThrottlingConfig);
}
@Override
public int hashCode() {
return Objects.hash(eventLoopThreads,
cleanSession,
reconnectForRedelivery,
reconnectForRedeliveryDelay,
useSeparateClientForPublisher,
reconnectMinTimeoutForMqttBrokerInitiatedDisconnect,
maxQueueSize,
reconnectBackOffConfig,
consumerThrottlingConfig);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"eventLoopThreads=" + eventLoopThreads +
", cleanSession=" + cleanSession +
", reconnectForRedelivery=" + reconnectForRedelivery +
", reconnectForRedeliveryDelay=" + reconnectForRedeliveryDelay +
", useSeparateClientForPublisher=" + useSeparateClientForPublisher +
", reconnectMinTimeoutForMqttBrokerInitiatedDisconnect=" +
reconnectMinTimeoutForMqttBrokerInitiatedDisconnect +
", maxQueueSize=" + maxQueueSize +
", reconnectBackOffConfig=" + reconnectBackOffConfig +
", consumerThrottlingConfig=" + consumerThrottlingConfig +
"]";
}
}