代码调整

This commit is contained in:
machey
2019-01-20 14:30:20 +08:00
parent 2f214ce3a6
commit 4ebf17f57b

View File

@@ -140,11 +140,12 @@ public class CanalKafkaProducer implements CanalMQProducer {
if (flatMessages != null) {
// 开启事务,需要kafka版本支持
if (kafkaProperties.getTransaction()) {
producer.initTransactions();
producer.beginTransaction();
}
try {
for (FlatMessage flatMessage : flatMessages) {
if (StringUtils.isEmpty(canalDestination.getPartitionHash())) {
try {
Integer partition = canalDestination.getPartition();
if (partition == null) {
partition = 0;
@@ -158,12 +159,6 @@ public class CanalKafkaProducer implements CanalMQProducer {
} else {
producer2.send(record).get();
}
} catch (Exception e) {
if (kafkaProperties.getTransaction()) {
producer.abortTransaction();
}
throw e;
}
} else {
FlatMessage[] partitionFlatMessage = MQMessageUtils.messagePartition(flatMessage,
canalDestination.getPartitionsNum(),
@@ -172,7 +167,6 @@ public class CanalKafkaProducer implements CanalMQProducer {
for (int i = 0; i < length; i++) {
FlatMessage flatMessagePart = partitionFlatMessage[i];
if (flatMessagePart != null) {
try {
ProducerRecord<String, String> record = new ProducerRecord<String, String>(
topicName,
i,
@@ -183,12 +177,6 @@ public class CanalKafkaProducer implements CanalMQProducer {
} else {
producer2.send(record).get();
}
} catch (Exception e) {
if (kafkaProperties.getTransaction()) {
producer.abortTransaction();
}
throw e;
}
}
}
}
@@ -203,10 +191,14 @@ public class CanalKafkaProducer implements CanalMQProducer {
JSON.toJSONString(flatMessage, SerializerFeature.WriteMapNullValue));
}
}
} catch (Exception e) {
if (kafkaProperties.getTransaction()) {
producer.abortTransaction();
}
throw e;
}
}
}
// producer.commitTransaction();
}
}