mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
代码调整
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
for (FlatMessage flatMessage : flatMessages) {
|
for (FlatMessage flatMessage : flatMessages) {
|
||||||
if (StringUtils.isEmpty(canalDestination.getPartitionHash())) {
|
if (StringUtils.isEmpty(canalDestination.getPartitionHash())) {
|
||||||
try {
|
|
||||||
Integer partition = canalDestination.getPartition();
|
Integer partition = canalDestination.getPartition();
|
||||||
if (partition == null) {
|
if (partition == null) {
|
||||||
partition = 0;
|
partition = 0;
|
||||||
@@ -158,12 +159,6 @@ 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;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
FlatMessage[] partitionFlatMessage = MQMessageUtils.messagePartition(flatMessage,
|
FlatMessage[] partitionFlatMessage = MQMessageUtils.messagePartition(flatMessage,
|
||||||
canalDestination.getPartitionsNum(),
|
canalDestination.getPartitionsNum(),
|
||||||
@@ -172,7 +167,6 @@ public class CanalKafkaProducer implements CanalMQProducer {
|
|||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
FlatMessage flatMessagePart = partitionFlatMessage[i];
|
FlatMessage flatMessagePart = partitionFlatMessage[i];
|
||||||
if (flatMessagePart != null) {
|
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,12 +177,6 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,10 +191,14 @@ public class CanalKafkaProducer implements CanalMQProducer {
|
|||||||
JSON.toJSONString(flatMessage, SerializerFeature.WriteMapNullValue));
|
JSON.toJSONString(flatMessage, SerializerFeature.WriteMapNullValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (kafkaProperties.getTransaction()) {
|
||||||
|
producer.abortTransaction();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// producer.commitTransaction();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user