spring-cloud / spring-cloud-stream-binder-kafka

Spring Cloud Stream binders for Apache Kafka and Kafka Streams
Apache License 2.0
331 stars 301 forks source link

Timeout in KafkaTransactionManager #1099

Closed minhtran-q closed 3 years ago

minhtran-q commented 3 years ago

I trying to configure a timeout for org.springframework.kafka.transaction.KafkaTransactionManager via @Transactional (org.springframework.transaction.annotation.Transactional).

@Bean
@Transactional(timeout = 40, transactionManager = "kafkaTransactionManager")
public Function<JsonNode, JsonNode> process() {
    return e -> {
        //do something
       //timeout if process hangs
        return e;
    };
}

But it doesn't seem to work. I search in org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder and see the KafkaTransactionManager is not a Bean. Does this have anything to do with inactivity timeout?

And is there any solution for this?

garyrussell commented 3 years ago

There is no concept of a timeout for Kafka transactions.

garyrussell commented 3 years ago

And even with other transaction managers, the timeout won't help if the thread is stuck in user code.

minhtran-q commented 3 years ago

I mean it's not completely stuck. It can be a long process and I want to control it for a certain time. With JpaTransactionManager, I noticed it can be set a timeout for each transaction. So I'm pretty confused why KafkaTransactionManagerdoesn't have a per-transaction timeout configuration?

garyrussell commented 3 years ago

The transaction timeout only applies to the begin transaction or commit; it has nothing to do with how long the method runs for.

minhtran-q commented 3 years ago

Ok, I got it, thanks for your help