|
@@ -33,9 +33,9 @@ public class CanalKafkaStarter implements CanalServerStarter {
|
|
|
|
|
|
private ExecutorService executorService;
|
|
|
|
|
|
- private CanalKafkaProducer canalKafkaProducer;
|
|
|
+ private CanalKafkaProducer canalKafkaProducer;
|
|
|
|
|
|
- private KafkaProperties kafkaProperties;
|
|
|
+ private KafkaProperties kafkaProperties;
|
|
|
|
|
|
public void init() {
|
|
|
try {
|
|
@@ -55,7 +55,7 @@ public class CanalKafkaStarter implements CanalServerStarter {
|
|
|
canalKafkaProducer.init(kafkaProperties);
|
|
|
// set filterTransactionEntry
|
|
|
// if (kafkaProperties.isFilterTransactionEntry()) {
|
|
|
- // System.setProperty("canal.instance.filter.transaction.entry", "true");
|
|
|
+ // System.setProperty("canal.instance.filter.transaction.entry", "true");
|
|
|
// }
|
|
|
// 对应每个instance启动一个worker线程
|
|
|
List<CanalDestination> destinations = kafkaProperties.getCanalDestinations();
|
|
@@ -101,8 +101,8 @@ public class CanalKafkaStarter implements CanalServerStarter {
|
|
|
while (!running)
|
|
|
;
|
|
|
logger.info("## start the canal consumer: {}.", destination.getCanalDestination());
|
|
|
- CanalServerWithEmbedded server = CanalServerWithEmbedded.instance();
|
|
|
- ClientIdentity clientIdentity = new ClientIdentity(destination.getCanalDestination(), (short) 1001, "");
|
|
|
+ final CanalServerWithEmbedded server = CanalServerWithEmbedded.instance();
|
|
|
+ final ClientIdentity clientIdentity = new ClientIdentity(destination.getCanalDestination(), (short) 1001, "");
|
|
|
while (running) {
|
|
|
try {
|
|
|
if (!server.getCanalInstances().containsKey(clientIdentity.getDestination())) {
|
|
@@ -118,26 +118,28 @@ public class CanalKafkaStarter implements CanalServerStarter {
|
|
|
|
|
|
while (running) {
|
|
|
Message message = server.getWithoutAck(clientIdentity, kafkaProperties.getCanalBatchSize()); // 获取指定数量的数据
|
|
|
- long batchId = message.getId();
|
|
|
+ final long batchId = message.getId();
|
|
|
try {
|
|
|
int size = message.isRaw() ? message.getRawEntries().size() : message.getEntries().size();
|
|
|
if (batchId != -1 && size != 0) {
|
|
|
- if (!StringUtils.isEmpty(destination.getTopic())) {
|
|
|
- Topic topic = new Topic();
|
|
|
- topic.setTopic(destination.getTopic());
|
|
|
- topic.setPartition(destination.getPartition());
|
|
|
- destination.getTopics().add(topic);
|
|
|
- }
|
|
|
- for (Topic topic : destination.getTopics()) {
|
|
|
- canalKafkaProducer.send(topic, message); // 发送message到所有topic
|
|
|
- }
|
|
|
+ Topic topic = new Topic();
|
|
|
+ topic.setTopic(destination.getTopic());
|
|
|
+ topic.setPartition(destination.getPartition());
|
|
|
+ canalKafkaProducer.send(topic, message, new CanalKafkaProducer.Callback() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void commit() {
|
|
|
+ server.ack(clientIdentity, batchId); // 提交确认
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void rollback() {
|
|
|
+ server.rollback(clientIdentity, batchId);
|
|
|
+ }
|
|
|
+ }); // 发送message到topic
|
|
|
}
|
|
|
|
|
|
- if (batchId != -1) {
|
|
|
- server.ack(clientIdentity, batchId); // 提交确认
|
|
|
- }
|
|
|
} catch (Exception e) {
|
|
|
- server.rollback(clientIdentity);
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
}
|