Skip to content

Single embedded kafka as mvn/gradle plugin #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mykolapolonskyi opened this issue Jun 21, 2018 · 4 comments
Closed

Single embedded kafka as mvn/gradle plugin #713

mykolapolonskyi opened this issue Jun 21, 2018 · 4 comments

Comments

@mykolapolonskyi
Copy link

mykolapolonskyi commented Jun 21, 2018

Maybe will be good idea to put "embedded kafka" on to separated maven/gradle plugin

it can allow creation single kafka node per integration test phase

I`m trying to put 1.1.0 kafka to the
https://github.com/charithe/kafka-maven-plugin

It`s no a very successful or useful for now but want to ask advises pros/cons to this staff
Maybe there is way to create single kafka per tests but I miss it
Or etc

code
@Mojo(name = "start-kafka-broker")
class StartKafkaBrokerMojo(): AbstractMojo() {

    @Parameter(defaultValue = "9092")
    val kafkaPort: Int = 29092

    @Parameter(defaultValue = "2181")
    val zookeeperPort: Int = 22181

    @Throws(MojoExecutionException::class, MojoFailureException::class)
    override fun execute() {
        try {
            log.info("Starting Zookeeper on port $zookeeperPort and Kafka broker on port $kafkaPort")
            KafkaHandler.start(zookeeperPort, kafkaPort)
        } catch (e: Exception) {
            log.error("Failed to start Kafka broker", e)
            throw MojoExecutionException("Failed to start Kafka broker")
        }

    }
}

@Mojo(name = "stop-kafka-broker")
class StopKafkaBrokerMojo : AbstractMojo() {

    @Parameter(defaultValue = "29092")
    val kafkaPort: Int = 29092

    @Parameter(defaultValue = "22181")
    val zookeeperPort: Int = 22181

    @Throws(MojoExecutionException::class, MojoFailureException::class)
    override fun execute() {
        log.info("Attempting to stop the Kafka broker")
        KafkaHandler.stop()
    }
}


object KafkaHandler {
    private lateinit var zookeeper: TestingServer
    private lateinit var kafkaServer: KafkaServerStartable
    private lateinit var kafkaLogDir: Path

    private fun buildKafkaConfig(zookeeperQuorum: String, kafkaPort: Int): KafkaConfig {
        kafkaLogDir = Files.createTempDirectory("kafka_maven")

        val props = Properties().apply {
            put("port", "$kafkaPort")
            put("broker.id", "1")
            put("log.dirs", kafkaLogDir.toAbsolutePath().toString())
            put("zookeeper.connect", zookeeperQuorum)
        }
        return KafkaConfig(props)
    }


    fun start(zookeeperPort: Int, kafkaPort: Int) {
        zookeeper = TestingServer(zookeeperPort, true)
        val zookeeperConnectionString = zookeeper.getConnectString()
        val kafkaConfig = buildKafkaConfig(zookeeperConnectionString, kafkaPort)
        kafkaServer = KafkaServerStartable(kafkaConfig)
        kafkaServer.startup()
    }


    fun stop() {
        try {
            kafkaServer.shutdown()
            zookeeper.close()

            if (Files.exists(kafkaLogDir)) {
                Files.walkFileTree(kafkaLogDir, object : SimpleFileVisitor<Path>() {
                    @Throws(IOException::class)
                    override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
                        Files.deleteIfExists(file)
                        return FileVisitResult.CONTINUE
                    }

                    @Throws(IOException::class)
                    override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
                        Files.deleteIfExists(dir)
                        return FileVisitResult.CONTINUE
                    }
                })
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
@garyrussell
Copy link
Contributor

See this issue and its related PR as well as the documentation.

@artembilan
Copy link
Member

I would say this goes out of scope of this project.

Perhaps better to ask for such a functionality in Apache Kafka community.
Although it isn't clear why would one need to have an embedded Kafka in the plugin since it is going to be semantically the same as have a standalone Kafka Broker...

@mykolapolonskyi
Copy link
Author

@artembilan - probably this ticket can be closed - I wold prefer use testcontainers next time for this purpose.

@artembilan
Copy link
Member

Closed as Won't Fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants