-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultKafkaProducerConfig.java
149 lines (127 loc) · 5.23 KB
/
DefaultKafkaProducerConfig.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
/*
* Copyright (c) 2021 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.concurrent.Immutable;
import org.eclipse.ditto.internal.utils.config.ConfigWithFallback;
import com.typesafe.config.Config;
/**
* This class is the default implementation of {@link KafkaProducerConfig}.
*/
@Immutable
final class DefaultKafkaProducerConfig implements KafkaProducerConfig {
private static final String CONFIG_PATH = "producer";
private static final String ALPAKKA_PATH = "alpakka";
private final int queueSize;
private final int parallelism;
private final Duration minBackoff;
private final Duration maxBackoff;
private final double randomFactor;
private final int maxRestartsCount;
private final Duration maxRestartsWithin;
private final Config alpakkaConfig;
private final long initTimeoutSeconds;
private DefaultKafkaProducerConfig(final Config kafkaProducerScopedConfig) {
queueSize = kafkaProducerScopedConfig.getInt(ConfigValue.QUEUE_SIZE.getConfigPath());
parallelism = kafkaProducerScopedConfig.getInt(ConfigValue.PARALLELISM.getConfigPath());
minBackoff = kafkaProducerScopedConfig.getDuration(ConfigValue.MIN_BACKOFF.getConfigPath());
maxBackoff = kafkaProducerScopedConfig.getDuration(ConfigValue.MAX_BACKOFF.getConfigPath());
randomFactor = kafkaProducerScopedConfig.getDouble(ConfigValue.RANDOM_FACTOR.getConfigPath());
maxRestartsCount = kafkaProducerScopedConfig.getInt(ConfigValue.MAX_RESTARTS_COUNT.getConfigPath());
maxRestartsWithin = kafkaProducerScopedConfig.getDuration(ConfigValue.MAX_RESTARTS_WITHIN.getConfigPath());
alpakkaConfig = kafkaProducerScopedConfig.getConfig(ALPAKKA_PATH);
initTimeoutSeconds = kafkaProducerScopedConfig.getLong(ConfigValue.INIT_TIMEOUT_SECONDS.getConfigPath());
}
/**
* Returns an instance of {@code DefaultKafkaProducerConfig} based on the settings of the specified Config.
*
* @param config is supposed to provide the Kafka config setting.
* @return the instance.
* @throws org.eclipse.ditto.internal.utils.config.DittoConfigError if {@code config} is invalid.
*/
public static DefaultKafkaProducerConfig of(final Config config) {
return new DefaultKafkaProducerConfig(
ConfigWithFallback.newInstance(config, CONFIG_PATH, ConfigValue.values()));
}
@Override
public int getQueueSize() {
return queueSize;
}
@Override
public int getParallelism() {
return parallelism;
}
@Override
public Duration getMinBackoff() {
return minBackoff;
}
@Override
public Duration getMaxBackoff() {
return maxBackoff;
}
@Override
public double getRandomFactor() {
return randomFactor;
}
@Override
public int getMaxRestartsCount() {
return maxRestartsCount;
}
@Override
public Duration getMaxRestartsWithin() {
return maxRestartsWithin;
}
@Override
public Config getAlpakkaConfig() {
return alpakkaConfig;
}
@Override
public long getInitTimeoutSeconds() {
return initTimeoutSeconds;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final DefaultKafkaProducerConfig that = (DefaultKafkaProducerConfig) o;
return Objects.equals(queueSize, that.queueSize) &&
Objects.equals(parallelism, that.parallelism) &&
Objects.equals(minBackoff, that.minBackoff) &&
Objects.equals(maxBackoff, that.maxBackoff) &&
Objects.equals(randomFactor, that.randomFactor) &&
Objects.equals(maxRestartsCount, that.maxRestartsCount) &&
Objects.equals(maxRestartsWithin, that.maxRestartsWithin) &&
Objects.equals(alpakkaConfig, that.alpakkaConfig) &&
Objects.equals(initTimeoutSeconds, that.initTimeoutSeconds);
}
@Override
public int hashCode() {
return Objects.hash(queueSize, parallelism, minBackoff, maxBackoff, maxRestartsCount, maxRestartsWithin,
randomFactor, alpakkaConfig, initTimeoutSeconds);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"queueSize=" + queueSize +
", parallelism=" + parallelism +
", minBackoff=" + minBackoff +
", maxBackoff=" + maxBackoff +
", randomFactor=" + randomFactor +
", maxRestartsCount=" + maxRestartsCount +
", maxRestartsWithin=" + maxRestartsWithin +
", alpakkaConfig=" + alpakkaConfig +
", initTimeoutSeconds=" + initTimeoutSeconds +
"]";
}
}