duxinglangzi
2022-04-25 92920274207200239e3bcee629a469e2685eebdd
提交 | 用户 | age
de8c2b 1 package com.duxinglangzi.canal.starter.container;
D 2
3 import com.alibaba.otter.canal.client.CanalConnector;
4 import com.alibaba.otter.canal.protocol.CanalEntry;
5 import com.alibaba.otter.canal.protocol.Message;
6 import com.duxinglangzi.canal.starter.configuration.CanalAutoConfigurationProperties;
7 import com.duxinglangzi.canal.starter.configuration.CanalListenerEndpointRegistrar;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import java.lang.reflect.InvocationTargetException;
12 import java.util.*;
13
14 /**
627420 15  * DML 数据拉取、解析
de8c2b 16  * @author wuqiong 2022/4/11
D 17  */
18 public class DmlMessageTransponderContainer extends AbstractCanalTransponderContainer {
19
20     private final static Logger logger = LoggerFactory.getLogger(DmlMessageTransponderContainer.class);
21
22     private CanalConnector connector;
23     private CanalAutoConfigurationProperties.EndpointInstance endpointInstance;
24     private List<CanalListenerEndpointRegistrar> registrars = new ArrayList<>();
25     private Set<CanalEntry.EventType> SUPPORT_ALL_TYPES = new HashSet<>();
26
27
28     public void initConnect() {
29         // init supportAllTypes
30         registrars.forEach(e -> SUPPORT_ALL_TYPES.addAll(
31                 Arrays.asList(e.getListenerEntry().getValue().eventType())));
32         connector.connect();
33         connector.subscribe(endpointInstance.getSubscribe());
34         connector.rollback();
35
36     }
37
929202 38     public void disconnect(){
D 39         // 关闭连接
40         connector.disconnect();
41     }
42
de8c2b 43
D 44     public void doStart() {
45         Message message = null;
46         try {
47             message = connector.getWithoutAck(endpointInstance.getBatchSize()); // 获取指定数量的数据
48         } catch (Exception clientException) {
49             logger.error("[MessageTransponderContainer] error msg : ", clientException);
50             endpointInstance.setRetryCount(endpointInstance.getRetryCount() - 1);
51             if (endpointInstance.getRetryCount() < 0) {
52                 logger.error("[MessageTransponderContainer] retry count is zero ,  " +
53                                 "thread interrupt , current connector host: {} , port: {} ",
54                         endpointInstance.getHost(), endpointInstance.getPort());
55                 Thread.currentThread().interrupt();
929202 56                 disconnect();
de8c2b 57             } else {
D 58                 sleep(endpointInstance.getAcquireInterval());
59             }
60             return;
61         }
62         List<CanalEntry.Entry> entries = message.getEntries();
63         if (message.getId() == -1 || entries.isEmpty()) {
64             sleep(endpointInstance.getAcquireInterval());
65             return;
66         }
67         try {
68             entries.forEach(e -> consumer(e));
69         } catch (Exception e) {
70             e.printStackTrace();
71             logger.error("[MessageTransponderContainer_doStart] CanalEntry.Entry consumer error ", e);
72             // connector.rollback(message.getId()); // 目前先不处理失败, 无需回滚数据
73             // return;
74         }
75         connector.ack(message.getId()); // 提交确认
76     }
77
78     public DmlMessageTransponderContainer(
79             CanalConnector connector, List<CanalListenerEndpointRegistrar> registrars,
80             CanalAutoConfigurationProperties.EndpointInstance endpointInstance) {
81         this.connector = connector;
82         this.registrars.addAll(registrars);
83         this.endpointInstance = endpointInstance;
84     }
85
86     private void consumer(CanalEntry.Entry entry) {
87         if (IGNORE_ENTRY_TYPES.contains(entry.getEntryType())) return;
88         CanalEntry.RowChange rowChange = null;
89         try {
90             rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
91         } catch (Exception e) {
92             logger.error("[MessageTransponderContainer_consumer] RowChange parse has an error ", e);
93             throw new RuntimeException("RowChange parse has an error , data:" + entry.toString(), e);
94         }
95
96         // 忽略 ddl 语句
97         if (rowChange.hasIsDdl() && rowChange.getIsDdl()) return;
98         CanalEntry.EventType eventType = rowChange.getEventType();
99         if (!SUPPORT_ALL_TYPES.contains(eventType)) return;
100         for (CanalEntry.RowData rowData : rowChange.getRowDatasList()) {
101             registrars
102                     .stream()
103                     .filter(CanalListenerEndpointRegistrar.filterArgs(
104                             entry.getHeader().getSchemaName(),
105                             entry.getHeader().getTableName(),
106                             eventType))
107                     .forEach(element -> {
108                         try {
109                             element.getListenerEntry().getKey().invoke(element.getBean(), eventType, rowData);
110                         } catch (IllegalAccessException | InvocationTargetException e) {
111                             logger.error("[MessageTransponderContainer_consumer] RowData Callback Method invoke error message", e);
112                             throw new RuntimeException("RowData Callback Method invoke error message: " + e.getMessage(), e);
113                         }
114                     });
115         }
116     }
117
118 }