Java Code Examples for org.apache.kafka.clients.producer.Producer#abortTransaction()

The following examples show how to use org.apache.kafka.clients.producer.Producer#abortTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultProducerFactoryTest.java    From extension-kafka with Apache License 2.0 6 votes vote down vote up
@Test
void testTransactionalProducerBehaviorOnCommittingAnAbortedTransaction() {
    assumeFalse(
            System.getProperty("os.name").contains("Windows"),
            "Transactional producers not supported on Windows"
    );

    ProducerFactory<String, String> producerFactory = transactionalProducerFactory(kafkaBroker, "xyz");
    Producer<String, String> testProducer = producerFactory.createProducer();

    try {
        testProducer.beginTransaction();
        send(testProducer, "testTransactionalProducerBehaviorOnCommittingAnAbortedTransaction", "bar");
        testProducer.abortTransaction();
        assertThrows(KafkaException.class, testProducer::commitTransaction);
    } finally {
        cleanup(producerFactory, testProducer);
    }
}
 
Example 2
Source File: KafkaPublisher.java    From extension-kafka with Apache License 2.0 5 votes vote down vote up
private void tryRollback(Producer<?, ?> producer) {
    try {
        producer.abortTransaction();
    } catch (Exception e) {
        logger.warn("Unable to abort transaction", e);
        // Not re-throwing exception, its too late
    }
}
 
Example 3
Source File: ProducerInTransaction.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
/**
 * 在一个事务只有生产消息操作
 */
public static void onlyProduceInTransaction() {
	Producer producer = buildProducer();

	// 1.初始化事务
	producer.initTransactions();

	// 2.开启事务
	producer.beginTransaction();

	try {
		// 3.kafka写操作集合
		// 3.1 do业务逻辑

		// 3.2 发送消息
		producer.send(new ProducerRecord<String, String>("test", "transaction-data-1"));
		producer.send(new ProducerRecord<String, String>("test", "transaction-data-2"));

		// 3.3 do其他业务逻辑,还可以发送其他topic的消息。

		// 4.事务提交
		producer.commitTransaction();
	} catch (Exception e) {
		// 5.放弃事务
		producer.abortTransaction();
	}
}
 
Example 4
Source File: CanalKafkaProducer.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
@Override
public void send(MQProperties.CanalDestination canalDestination, Message message, Callback callback) {
    // 开启事务,需要kafka版本支持
    Producer producerTmp;
    if (!kafkaProperties.getFlatMessage()) {
        producerTmp = producer;
    } else {
        producerTmp = producer2;
    }

    if (kafkaProperties.getTransaction()) {
        producerTmp.beginTransaction();
    }
    try {
        if (!StringUtils.isEmpty(canalDestination.getDynamicTopic())) {
            // 动态topic
            Map<String, Message> messageMap = MQMessageUtils.messageTopics(message,
                canalDestination.getTopic(),
                canalDestination.getDynamicTopic());

            for (Map.Entry<String, Message> entry : messageMap.entrySet()) {
                String topicName = entry.getKey().replace('.', '_');
                Message messageSub = entry.getValue();
                if (logger.isDebugEnabled()) {
                    logger.debug("## Send message to kafka topic: " + topicName);
                }
                send(canalDestination, topicName, messageSub);
            }
        } else {
            send(canalDestination, canalDestination.getTopic(), message);
        }
        if (kafkaProperties.getTransaction()) {
            producerTmp.commitTransaction();
        }
        callback.commit();
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        if (kafkaProperties.getTransaction()) {
            producerTmp.abortTransaction();
        }
        callback.rollback();
    }
}
 
Example 5
Source File: ProducerInTransaction.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
/**
 * 在一个事务内,即有生产消息又有消费消息
 */
public static void consumeTransferProduce() {
	// 1.构建上产者
	Producer producer = buildProducer();
	// 2.初始化事务(生成productId),对于一个生产者,只能执行一次初始化事务操作
	producer.initTransactions();

	// 3.构建消费者和订阅主题
	Consumer consumer = buildConsumer();
	consumer.subscribe(Arrays.asList("test"));
	while (true) {
		// 4.开启事务
		producer.beginTransaction();

		// 5.1 接受消息
		ConsumerRecords<String, String> records = consumer.poll(500);

		try {
			// 5.2 do业务逻辑;
			System.out.println("customer Message---");
			Map<TopicPartition, OffsetAndMetadata> commits = new HashMap<>();
			for (ConsumerRecord<String, String> record : records) {
				// 5.2.1 读取消息,并处理消息。print the offset,key and value for the consumer
				// records.
				System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(),
					record.value());

				// 5.2.2 记录提交的偏移量
				commits.put(new TopicPartition(record.topic(), record.partition()),
					new OffsetAndMetadata(record.offset()));

				// 6.生产新的消息。比如外卖订单状态的消息,如果订单成功,则需要发送跟商家结转消息或者派送员的提成消息
				producer.send(new ProducerRecord<String, String>("test", "data2"));
			}

			// 7.提交偏移量
			producer.sendOffsetsToTransaction(commits, "group0323");

			// 8.事务提交
			producer.commitTransaction();
		} catch (Exception e) {
			// 7.放弃事务
			producer.abortTransaction();
		}
	}
}