代码调整

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) { if (flatMessages != null) {
// 开启事务,需要kafka版本支持 // 开启事务,需要kafka版本支持
if (kafkaProperties.getTransaction()) { if (kafkaProperties.getTransaction()) {
producer.initTransactions();
producer.beginTransaction(); producer.beginTransaction();
} }
for (FlatMessage flatMessage : flatMessages) { try {
if (StringUtils.isEmpty(canalDestination.getPartitionHash())) { for (FlatMessage flatMessage : flatMessages) {
try { if (StringUtils.isEmpty(canalDestination.getPartitionHash())) {
Integer partition = canalDestination.getPartition(); Integer partition = canalDestination.getPartition();
if (partition == null) { if (partition == null) {
partition = 0; partition = 0;
@@ -158,21 +159,14 @@ public class CanalKafkaProducer implements CanalMQProducer {
} else { } else {
producer2.send(record).get(); producer2.send(record).get();
} }
} catch (Exception e) { } else {
if (kafkaProperties.getTransaction()) { FlatMessage[] partitionFlatMessage = MQMessageUtils.messagePartition(flatMessage,
producer.abortTransaction(); canalDestination.getPartitionsNum(),
} canalDestination.getPartitionHash());
throw e; int length = partitionFlatMessage.length;
} for (int i = 0; i < length; i++) {
} else { FlatMessage flatMessagePart = partitionFlatMessage[i];
FlatMessage[] partitionFlatMessage = MQMessageUtils.messagePartition(flatMessage, if (flatMessagePart != null) {
canalDestination.getPartitionsNum(),
canalDestination.getPartitionHash());
int length = partitionFlatMessage.length;
for (int i = 0; i < length; i++) {
FlatMessage flatMessagePart = partitionFlatMessage[i];
if (flatMessagePart != null) {
try {
ProducerRecord<String, String> record = new ProducerRecord<String, String>( ProducerRecord<String, String> record = new ProducerRecord<String, String>(
topicName, topicName,
i, i,
@@ -183,30 +177,28 @@ public class CanalKafkaProducer implements CanalMQProducer {
} else { } else {
producer2.send(record).get(); producer2.send(record).get();
} }
} catch (Exception e) {
if (kafkaProperties.getTransaction()) {
producer.abortTransaction();
}
throw e;
} }
} }
} }
}
if (kafkaProperties.getTransaction()) {
producer.commitTransaction();
}
if (logger.isDebugEnabled()) {
logger.debug("Send flat message to kafka topic: [{}], packet: {}",
topicName,
JSON.toJSONString(flatMessage, SerializerFeature.WriteMapNullValue));
}
}
} catch (Exception e) {
if (kafkaProperties.getTransaction()) { if (kafkaProperties.getTransaction()) {
producer.commitTransaction(); producer.abortTransaction();
}
if (logger.isDebugEnabled()) {
logger.debug("Send flat message to kafka topic: [{}], packet: {}",
topicName,
JSON.toJSONString(flatMessage, SerializerFeature.WriteMapNullValue));
} }
throw e;
} }
} }
} }
// producer.commitTransaction();
} }
} }