Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 编写测试以在多线程环境中使用Kafka consumer_Multithreading_Scala_Apache Kafka_Thread Safety_Kafka Consumer Api - Fatal编程技术网

Multithreading 编写测试以在多线程环境中使用Kafka consumer

Multithreading 编写测试以在多线程环境中使用Kafka consumer,multithreading,scala,apache-kafka,thread-safety,kafka-consumer-api,Multithreading,Scala,Apache Kafka,Thread Safety,Kafka Consumer Api,我试图在一个单独的线程中创建一个kafka使用者,该线程使用来自kafka主题的数据。为此,我扩展了ShutdownableThread抽象类,并提供了doWork方法的实现。我的代码是这样的- abstract class MyConsumer(topic: String) extends ShutdownableThread(topic) { val props: Properties = ??? private val consumer = new KafkaConsume

我试图在一个单独的线程中创建一个kafka使用者,该线程使用来自kafka主题的数据。为此,我扩展了
ShutdownableThread
抽象类,并提供了
doWork
方法的实现。我的代码是这样的-

abstract class MyConsumer(topic: String) extends ShutdownableThread(topic) {
    val props: Properties = ???
    private val consumer = new KafkaConsumer[String, String](props)
    consumer.subscribe(List(topic).asJava)

    def process(value: String): Unit // Abstract method defining what to do with each record

    override def doWork(): Unit = {
        for (record <- consumer.poll(Duration.ofMillis(1000)).asScala)
            process(record.value())
    }
}
使用者确实使用了来自kafka的消息,但在测试完成之前它不会更新消息,因此测试失败。所以,我试着把主线放在睡眠上。但它会抛出带有消息的
ConcurrentModificationException
异常-
KafkaConsumer对于多线程访问不安全


你知道我的方法有什么问题吗?提前感谢。

必须将主线程休眠几秒钟,以允许消费者使用来自kafka主题的消息并将其存储在可变变量中。已添加-
Thread.sleep(5000)
在启动使用者之后。

必须将主线程休眠几秒钟,以允许使用者使用来自kafka主题的消息并将其存储在可变变量中。已添加-
Thread.sleep(5000)
启动使用者后

var mutVar = "initial_value"

val consumer = new MyConsumer("test_topic") {
    override def process(value: String): Unit = mutVar = "updated_value"
}

consumer.start()
assert(mutVar === "updated_value")