如何在Kotlin中创建回调函数?
我在创建回调函数时遇到以下问题: 卡夫卡消费者监听新消息,以便能够在数据库中记录:如何在Kotlin中创建回调函数?,kotlin,callback,Kotlin,Callback,我在创建回调函数时遇到以下问题: 卡夫卡消费者监听新消息,以便能够在数据库中记录: suspend fun consumerClient(service: ClientService) { val messages = consumerCommands( "create-client", "localhost:9092", "consumer-client", false, OffsetBehaviour.Earliest, 10 ) } sus
suspend fun consumerClient(service: ClientService) {
val messages = consumerCommands(
"create-client", "localhost:9092", "consumer-client", false,
OffsetBehaviour.Earliest, 10
)
}
suspend fun consumerCommands(
topic: String,
bootstrapServers: String,
group: String,
autoCommit: Boolean,
offsetBehaviour: OffsetBehaviour,
pollMax: Int
) {
val consumer = consumer(bootstrapServers, group, autoCommit, offsetBehaviour, pollMax)
consumer.subscribe(mutableListOf(topic))
while (true) {
val records = consumer.poll(Duration.ofMillis(1000))
if (records.count() > 0) {
records.forEach {
val entity = treeToValue(it.value().get("message"), Client::class.java) as Client
ClientService().insert(entity)
}
}
}
}
它工作得很好。但我试图创造一些更一般的东西,如下所示:
interface KafkaConsumer<T> {
fun execute(callback: (T) -> Unit)
}
suspend fun <T> consumerCommand(
topic: String,
bootstrapServers: String,
group: String,
autoCommit: Boolean,
offsetBehaviour: OffsetBehaviour,
pollMax: Int,
callback: KafkaConsumer<T>
): ConsumerRecords<String, JsonNode>? {
val consumer = consumer(bootstrapServers, group, autoCommit, offsetBehaviour, pollMax)
consumer.subscribe(mutableListOf(topic))
while (true) {
val records = consumer.poll(Duration.ofMillis(1000))
if (records.count() > 0) {
records.forEach {
val entity = (treeToValue(it.value().get("message"), Any::class.java) as T)
coroutineScope {
callback.execute { entity }
}
}
}
}
}
suspend fun consumerClient(service: ClientService) {
val messages = consumerCommand<Client>(
"create-client", "localhost:9092", "consumer-client", false,
OffsetBehaviour.Earliest, 10, {client: Client -> ClientService().insert(client)}
)
}
接口卡夫卡消费者{
乐趣执行(回调:(T)->单位)
}
暂停趣味消费命令(
主题:字符串,
bootstrapserver:String,
组别:String,,
自动提交:布尔值,
抵消行为:抵消行为,
pollMax:Int,
回拨:卡夫卡消费者
):消费记录?{
val consumer=消费者(引导服务器、组、自动提交、OffsetBehavior、pollMax)
consumer.subscribe(可变列表(主题))
while(true){
val记录=消费者投票(持续时间百万(1000))
if(records.count()>0){
forEach记录{
val entity=(treeToValue(it.value().get(“message”),Any::class.java)作为T)
共线镜{
callback.execute{entity}
}
}
}
}
}
suspend fun consumerClient(服务:ClientService){
val消息=消费者命令(
“创建客户端”、“本地主机:9092”、“消费者客户端”,false,
OffsetBehavior.10,{client:client->ClientService().insert(client)}
)
}
但它不起作用。有人能帮忙吗?您正在尝试使用lambda
{client:client->…}
,其中需要使用KafkaConsumer
。相反,您需要使用函数类型。与您的KafkaConsumer
相当的是((T)->Unit)->Unit
,但我怀疑这是一个错误,您实际上想要
suspend fun <T> consumerCommand(
topic: String,
bootstrapServers: String,
group: String,
autoCommit: Boolean,
offsetBehaviour: OffsetBehaviour,
pollMax: Int,
callback: (T) -> Unit
): ConsumerRecords<String, JsonNode>? {
...
coroutineScope {
callback(entity)
}
}
}
}
}
suspend fun consumerCommand(
主题:字符串,
bootstrapserver:String,
组别:String,,
自动提交:布尔值,
抵消行为:抵消行为,
pollMax:Int,
回调:(T)->单位
):消费记录?{
...
共线镜{
回调(实体)
}
}
}
}
}
甚至suspend(T)->单元
旁注:在
consumerClient
函数中不使用service
参数,而是创建一个新的ClientService
;这是故意的吗?也许你可以这样做:
suspend fun <T> consumerCommand(
topic: String,
bootstrapServers: String,
group: String,
autoCommit: Boolean,
offsetBehaviour: OffsetBehaviour,
pollMax: Int,
callback: (entity: T) -> Unit>
): ConsumerRecords<String, JsonNode>? {
val consumer = consumer(bootstrapServers, group, autoCommit, offsetBehaviour, pollMax)
consumer.subscribe(mutableListOf(topic))
while (true) {
val records = consumer.poll(Duration.ofMillis(1000))
if (records.count() > 0) {
records.forEach {
val entity = (treeToValue(it.value().get("message"), Any::class.java) as T)
callback(entity)
}
}
}
}
suspend fun consumerClient(service: ClientService) {
val messages = consumerCommand<Client>(
"create-client", "localhost:9092", "consumer-client", false,
OffsetBehaviour.Earliest, 10, {client: Client -> ClientService().insert(client)}
)
}
suspend fun consumerCommand(
主题:字符串,
bootstrapserver:String,
组别:String,,
自动提交:布尔值,
抵消行为:抵消行为,
pollMax:Int,
回调:(实体:T)->单位>
):消费记录?{
val consumer=消费者(引导服务器、组、自动提交、OffsetBehavior、pollMax)
consumer.subscribe(可变列表(主题))
while(true){
val记录=消费者投票(持续时间百万(1000))
if(records.count()>0){
forEach记录{
val entity=(treeToValue(it.value().get(“message”),Any::class.java)作为T)
回调(实体)
}
}
}
}
suspend fun consumerClient(服务:ClientService){
val消息=消费者命令(
“创建客户端”、“本地主机:9092”、“消费者客户端”,false,
OffsetBehavior.10,{client:client->ClientService().insert(client)}
)
}
请添加有关哪些不起作用的信息。期望的结果是什么?