在线发送消息

This commit is contained in:
许晓东
2021-12-20 00:09:20 +08:00
parent bd814d550d
commit 0ec3bac6c2
8 changed files with 109 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package kafka.console
import com.xuxd.kafka.console.config.KafkaConfig
import org.apache.kafka.clients.consumer.{ConsumerConfig, ConsumerRecord}
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.TopicPartition
import java.time.Duration
@@ -170,4 +171,15 @@ class MessageConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConf
})
res
}
def send(topic: String, partition: Int, key: String, value: String, num: Int): Unit = {
withProducerAndCatchError(producer => {
val nullKey = if (key != null && key.trim().length() == 0) null else key
for (a <- 1 to num) {
val record = if (partition != -1) new ProducerRecord[String, String](topic, partition, nullKey, value) else new ProducerRecord[String, String](topic, nullKey, value)
producer.send(record)
}
}, e => log.error("send error.", e))
}
}