Skip to content

Commit c9443ee

Browse files
garyrussellartembilan
authored andcommitted
Prevent KafkaEmbedded exiting the JVM
See #194 See #345 See gradle/gradle#11195 **cherry-pick to 2.4.x, 2.3.x, 2.2.x, 1.3.x** # Conflicts: # spring-kafka-test/src/main/java/org/springframework/kafka/test/EmbeddedKafkaBroker.java
1 parent 7b492f5 commit c9443ee

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/KafkaEmbedded.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2018 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,8 @@
3737

3838
import org.I0Itec.zkclient.ZkClient;
3939
import org.I0Itec.zkclient.exception.ZkInterruptedException;
40+
import org.apache.commons.logging.Log;
41+
import org.apache.commons.logging.LogFactory;
4042
import org.apache.kafka.clients.consumer.Consumer;
4143
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
4244
import org.apache.kafka.common.Node;
@@ -45,6 +47,7 @@
4547
import org.apache.kafka.common.protocol.SecurityProtocol;
4648
import org.apache.kafka.common.requests.MetadataResponse;
4749
import org.apache.kafka.common.utils.AppInfoParser;
50+
import org.apache.kafka.common.utils.Exit;
4851
import org.apache.kafka.common.utils.Time;
4952
import org.junit.rules.ExternalResource;
5053

@@ -81,6 +84,8 @@
8184
*/
8285
public class KafkaEmbedded extends ExternalResource implements KafkaRule, InitializingBean, DisposableBean {
8386

87+
private static final Log logger = LogFactory.getLog(KafkaEmbedded.class);
88+
8489
public static final String BEAN_NAME = "kafkaEmbedded";
8590

8691
public static final String SPRING_EMBEDDED_KAFKA_BROKERS = "spring.embedded.kafka.brokers";
@@ -173,6 +178,7 @@ public void afterPropertiesSet() throws Exception {
173178

174179
@Override
175180
public void before() throws Exception { //NOSONAR
181+
overrideExitMethods();
176182
startZookeeper();
177183
int zkConnectionTimeout = 6000;
178184
int zkSessionTimeout = 6000;
@@ -226,11 +232,11 @@ public Properties createProperties(int i, Integer port) {
226232
boolean.class, boolean.class, int.class, boolean.class, int.class, boolean.class,
227233
int.class, scala.Option.class, int.class);
228234
return (Properties) method.invoke(null, i, this.zkConnect, this.controlledShutdown,
229-
true, port,
230-
scala.Option.<SecurityProtocol>apply(null),
231-
scala.Option.<File>apply(null),
232-
scala.Option.<Properties>apply(null),
233-
true, false, 0, false, 0, false, 0, scala.Option.<String>apply(null), 1);
235+
true, port,
236+
scala.Option.<SecurityProtocol>apply(null),
237+
scala.Option.<File>apply(null),
238+
scala.Option.<Properties>apply(null),
239+
true, false, 0, false, 0, false, 0, scala.Option.<String>apply(null), 1);
234240
}
235241
catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
236242
| InvocationTargetException e) {
@@ -501,4 +507,34 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
501507
.isTrue();
502508
}
503509

510+
private void overrideExitMethods() {
511+
final String exitMsg = "Exit.%s(%d, %s) called";
512+
Exit.setExitProcedure(
513+
new Exit.Procedure() {
514+
515+
@Override
516+
public void execute(int statusCode, String message) {
517+
if (logger.isDebugEnabled()) {
518+
logger.debug(String.format(exitMsg, "exit", statusCode, message), new RuntimeException());
519+
}
520+
else {
521+
logger.warn(String.format(exitMsg, "exit", statusCode, message));
522+
}
523+
}
524+
});
525+
Exit.setHaltProcedure(
526+
new Exit.Procedure() {
527+
528+
@Override
529+
public void execute(int statusCode, String message) {
530+
if (logger.isDebugEnabled()) {
531+
logger.debug(String.format(exitMsg, "halt", statusCode, message), new RuntimeException());
532+
}
533+
else {
534+
logger.warn(String.format(exitMsg, "halt", statusCode, message));
535+
}
536+
}
537+
});
538+
}
539+
504540
}

0 commit comments

Comments
 (0)