Search in sources :

Example 1 with TransactionManager

use of org.apache.kafka.clients.producer.internals.TransactionManager in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducer method configureTransactionState.

private static TransactionManager configureTransactionState(ProducerConfig config, LogContext logContext, Logger log) {
    TransactionManager transactionManager = null;
    boolean userConfiguredIdempotence = false;
    if (config.originals().containsKey(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG))
        userConfiguredIdempotence = true;
    boolean userConfiguredTransactions = false;
    if (config.originals().containsKey(ProducerConfig.TRANSACTIONAL_ID_CONFIG))
        userConfiguredTransactions = true;
    boolean idempotenceEnabled = config.getBoolean(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG);
    if (!idempotenceEnabled && userConfiguredIdempotence && userConfiguredTransactions)
        throw new ConfigException("Cannot set a " + ProducerConfig.TRANSACTIONAL_ID_CONFIG + " without also enabling idempotence.");
    if (userConfiguredTransactions)
        idempotenceEnabled = true;
    if (idempotenceEnabled) {
        String transactionalId = config.getString(ProducerConfig.TRANSACTIONAL_ID_CONFIG);
        int transactionTimeoutMs = config.getInt(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG);
        long retryBackoffMs = config.getLong(ProducerConfig.RETRY_BACKOFF_MS_CONFIG);
        transactionManager = new TransactionManager(logContext, transactionalId, transactionTimeoutMs, retryBackoffMs);
        if (transactionManager.isTransactional())
            log.info("Instantiated a transactional producer.");
        else
            log.info("Instantiated an idempotent producer.");
    }
    return transactionManager;
}
Also used : TransactionManager(org.apache.kafka.clients.producer.internals.TransactionManager) ConfigException(org.apache.kafka.common.config.ConfigException)

Example 2 with TransactionManager

use of org.apache.kafka.clients.producer.internals.TransactionManager in project kafka by apache.

the class KafkaProducer method configureTransactionState.

private TransactionManager configureTransactionState(ProducerConfig config, LogContext logContext) {
    TransactionManager transactionManager = null;
    if (config.idempotenceEnabled()) {
        final String transactionalId = config.getString(ProducerConfig.TRANSACTIONAL_ID_CONFIG);
        final int transactionTimeoutMs = config.getInt(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG);
        final long retryBackoffMs = config.getLong(ProducerConfig.RETRY_BACKOFF_MS_CONFIG);
        transactionManager = new TransactionManager(logContext, transactionalId, transactionTimeoutMs, retryBackoffMs, apiVersions);
        if (transactionManager.isTransactional())
            log.info("Instantiated a transactional producer.");
        else
            log.info("Instantiated an idempotent producer.");
    }
    return transactionManager;
}
Also used : TransactionManager(org.apache.kafka.clients.producer.internals.TransactionManager)

Aggregations

TransactionManager (org.apache.kafka.clients.producer.internals.TransactionManager)2 ConfigException (org.apache.kafka.common.config.ConfigException)1