Skip to content

Commit

Permalink
Prevent EmbeddedKafkaBroker exiting the JVM
Browse files Browse the repository at this point in the history
See #194
See #345
See gradle/gradle#11195

**cherry-pick to 2.4.x, 2.3.x, 2.2.x, 1.3.x**
  • Loading branch information
garyrussell authored and artembilan committed Apr 27, 2020
1 parent 444f928 commit ffce143
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.security.auth.SecurityProtocol;
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.common.utils.Utils;
import org.apache.zookeeper.server.NIOServerCnxnFactory;
Expand Down Expand Up @@ -283,6 +284,7 @@ public EmbeddedKafkaBroker zkSessionTimeout(int zkSessionTimeout) {

@Override
public void afterPropertiesSet() {
overrideExitMethods();
try {
this.zookeeper = new EmbeddedZookeeper(this.zkPort);
}
Expand Down Expand Up @@ -319,6 +321,26 @@ public void afterPropertiesSet() {
System.setProperty(SPRING_EMBEDDED_ZOOKEEPER_CONNECT, getZookeeperConnectionString());
}

private void overrideExitMethods() {
String exitMsg = "Exit.%s(%d, %s) called";
Exit.setExitProcedure((statusCode, message) -> {
if (logger.isDebugEnabled()) {
logger.debug(new RuntimeException(), String.format(exitMsg, "exit", statusCode, message));
}
else {
logger.warn(String.format(exitMsg, "exit", statusCode, message));
}
});
Exit.setHaltProcedure((statusCode, message) -> {
if (logger.isDebugEnabled()) {
logger.debug(new RuntimeException(), String.format(exitMsg, "halt", statusCode, message));
}
else {
logger.warn(String.format(exitMsg, "halt", statusCode, message));
}
});
}

private Properties createBrokerProperties(int i) {
return TestUtils.createBrokerConfig(i, this.zkConnect, this.controlledShutdown,
true, this.kafkaPorts[i],
Expand Down

0 comments on commit ffce143

Please sign in to comment.