|
37 | 37 | import java.util.concurrent.ExecutionException; |
38 | 38 | import java.util.concurrent.TimeUnit; |
39 | 39 | import java.util.concurrent.TimeoutException; |
| 40 | +import java.util.concurrent.atomic.AtomicBoolean; |
40 | 41 | import java.util.concurrent.atomic.AtomicReference; |
41 | 42 | import java.util.function.Function; |
42 | 43 | import java.util.stream.Collectors; |
@@ -111,6 +112,8 @@ public class EmbeddedKafkaZKBroker implements EmbeddedKafkaBroker { |
111 | 112 |
|
112 | 113 | private final Map<String, Object> brokerProperties = new HashMap<>(); |
113 | 114 |
|
| 115 | + private final AtomicBoolean initialized = new AtomicBoolean(); |
| 116 | + |
114 | 117 | private EmbeddedZookeeper zookeeper; |
115 | 118 |
|
116 | 119 | private String zkConnect; |
@@ -289,45 +292,47 @@ public synchronized EmbeddedKafkaZKBroker zkSessionTimeout(int zkSessionTimeout) |
289 | 292 |
|
290 | 293 | @Override |
291 | 294 | public void afterPropertiesSet() { |
292 | | - overrideExitMethods(); |
293 | | - try { |
294 | | - this.zookeeper = new EmbeddedZookeeper(this.zkPort); |
295 | | - } |
296 | | - catch (IOException | InterruptedException e) { |
297 | | - throw new IllegalStateException("Failed to create embedded Zookeeper", e); |
298 | | - } |
299 | | - this.zkConnect = LOOPBACK + ":" + this.zookeeper.getPort(); |
300 | | - this.kafkaServers.clear(); |
301 | | - boolean userLogDir = this.brokerProperties.get(KafkaConfig.LogDirProp()) != null && this.count == 1; |
302 | | - for (int i = 0; i < this.count; i++) { |
303 | | - Properties brokerConfigProperties = createBrokerProperties(i); |
304 | | - brokerConfigProperties.setProperty(KafkaConfig.ReplicaSocketTimeoutMsProp(), "1000"); |
305 | | - brokerConfigProperties.setProperty(KafkaConfig.ControllerSocketTimeoutMsProp(), "1000"); |
306 | | - brokerConfigProperties.setProperty(KafkaConfig.OffsetsTopicReplicationFactorProp(), "1"); |
307 | | - brokerConfigProperties.setProperty(KafkaConfig.ReplicaHighWatermarkCheckpointIntervalMsProp(), |
308 | | - String.valueOf(Long.MAX_VALUE)); |
309 | | - this.brokerProperties.forEach(brokerConfigProperties::put); |
310 | | - if (!this.brokerProperties.containsKey(KafkaConfig.NumPartitionsProp())) { |
311 | | - brokerConfigProperties.setProperty(KafkaConfig.NumPartitionsProp(), "" + this.partitionsPerTopic); |
| 295 | + if (this.initialized.compareAndSet(false, true)) { |
| 296 | + overrideExitMethods(); |
| 297 | + try { |
| 298 | + this.zookeeper = new EmbeddedZookeeper(this.zkPort); |
| 299 | + } |
| 300 | + catch (IOException | InterruptedException e) { |
| 301 | + throw new IllegalStateException("Failed to create embedded Zookeeper", e); |
312 | 302 | } |
313 | | - if (!userLogDir) { |
314 | | - logDir(brokerConfigProperties); |
| 303 | + this.zkConnect = LOOPBACK + ":" + this.zookeeper.getPort(); |
| 304 | + this.kafkaServers.clear(); |
| 305 | + boolean userLogDir = this.brokerProperties.get(KafkaConfig.LogDirProp()) != null && this.count == 1; |
| 306 | + for (int i = 0; i < this.count; i++) { |
| 307 | + Properties brokerConfigProperties = createBrokerProperties(i); |
| 308 | + brokerConfigProperties.setProperty(KafkaConfig.ReplicaSocketTimeoutMsProp(), "1000"); |
| 309 | + brokerConfigProperties.setProperty(KafkaConfig.ControllerSocketTimeoutMsProp(), "1000"); |
| 310 | + brokerConfigProperties.setProperty(KafkaConfig.OffsetsTopicReplicationFactorProp(), "1"); |
| 311 | + brokerConfigProperties.setProperty(KafkaConfig.ReplicaHighWatermarkCheckpointIntervalMsProp(), |
| 312 | + String.valueOf(Long.MAX_VALUE)); |
| 313 | + this.brokerProperties.forEach(brokerConfigProperties::put); |
| 314 | + if (!this.brokerProperties.containsKey(KafkaConfig.NumPartitionsProp())) { |
| 315 | + brokerConfigProperties.setProperty(KafkaConfig.NumPartitionsProp(), "" + this.partitionsPerTopic); |
| 316 | + } |
| 317 | + if (!userLogDir) { |
| 318 | + logDir(brokerConfigProperties); |
| 319 | + } |
| 320 | + KafkaServer server = TestUtils.createServer(new KafkaConfig(brokerConfigProperties), Time.SYSTEM); |
| 321 | + this.kafkaServers.add(server); |
| 322 | + if (this.kafkaPorts[i] == 0) { |
| 323 | + this.kafkaPorts[i] = TestUtils.boundPort(server, SecurityProtocol.PLAINTEXT); |
| 324 | + } |
315 | 325 | } |
316 | | - KafkaServer server = TestUtils.createServer(new KafkaConfig(brokerConfigProperties), Time.SYSTEM); |
317 | | - this.kafkaServers.add(server); |
318 | | - if (this.kafkaPorts[i] == 0) { |
319 | | - this.kafkaPorts[i] = TestUtils.boundPort(server, SecurityProtocol.PLAINTEXT); |
| 326 | + createKafkaTopics(this.topics); |
| 327 | + if (this.brokerListProperty == null) { |
| 328 | + this.brokerListProperty = System.getProperty(BROKER_LIST_PROPERTY); |
320 | 329 | } |
| 330 | + if (this.brokerListProperty != null) { |
| 331 | + System.setProperty(this.brokerListProperty, getBrokersAsString()); |
| 332 | + } |
| 333 | + System.setProperty(SPRING_EMBEDDED_KAFKA_BROKERS, getBrokersAsString()); |
| 334 | + System.setProperty(SPRING_EMBEDDED_ZOOKEEPER_CONNECT, getZookeeperConnectionString()); |
321 | 335 | } |
322 | | - createKafkaTopics(this.topics); |
323 | | - if (this.brokerListProperty == null) { |
324 | | - this.brokerListProperty = System.getProperty(BROKER_LIST_PROPERTY); |
325 | | - } |
326 | | - if (this.brokerListProperty != null) { |
327 | | - System.setProperty(this.brokerListProperty, getBrokersAsString()); |
328 | | - } |
329 | | - System.setProperty(SPRING_EMBEDDED_KAFKA_BROKERS, getBrokersAsString()); |
330 | | - System.setProperty(SPRING_EMBEDDED_ZOOKEEPER_CONNECT, getZookeeperConnectionString()); |
331 | 336 | } |
332 | 337 |
|
333 | 338 | private void logDir(Properties brokerConfigProperties) { |
|
0 commit comments