提交 | 用户 | 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 |
|
|
38 |
|
|
39 |
public void doStart() {
|
|
40 |
Message message = null;
|
|
41 |
try {
|
|
42 |
message = connector.getWithoutAck(endpointInstance.getBatchSize()); // 获取指定数量的数据
|
|
43 |
} catch (Exception clientException) {
|
|
44 |
logger.error("[MessageTransponderContainer] error msg : ", clientException);
|
|
45 |
endpointInstance.setRetryCount(endpointInstance.getRetryCount() - 1);
|
|
46 |
if (endpointInstance.getRetryCount() < 0) {
|
|
47 |
logger.error("[MessageTransponderContainer] retry count is zero , " +
|
|
48 |
"thread interrupt , current connector host: {} , port: {} ",
|
|
49 |
endpointInstance.getHost(), endpointInstance.getPort());
|
|
50 |
Thread.currentThread().interrupt();
|
fa654a
|
51 |
connector.disconnect();
|
de8c2b
|
52 |
} else {
|
D |
53 |
sleep(endpointInstance.getAcquireInterval());
|
|
54 |
}
|
|
55 |
return;
|
|
56 |
}
|
|
57 |
List<CanalEntry.Entry> entries = message.getEntries();
|
|
58 |
if (message.getId() == -1 || entries.isEmpty()) {
|
|
59 |
sleep(endpointInstance.getAcquireInterval());
|
|
60 |
return;
|
|
61 |
}
|
|
62 |
try {
|
|
63 |
entries.forEach(e -> consumer(e));
|
|
64 |
} catch (Exception e) {
|
|
65 |
e.printStackTrace();
|
|
66 |
logger.error("[MessageTransponderContainer_doStart] CanalEntry.Entry consumer error ", e);
|
|
67 |
// connector.rollback(message.getId()); // 目前先不处理失败, 无需回滚数据
|
|
68 |
// return;
|
|
69 |
}
|
|
70 |
connector.ack(message.getId()); // 提交确认
|
|
71 |
}
|
|
72 |
|
|
73 |
public DmlMessageTransponderContainer(
|
|
74 |
CanalConnector connector, List<CanalListenerEndpointRegistrar> registrars,
|
|
75 |
CanalAutoConfigurationProperties.EndpointInstance endpointInstance) {
|
|
76 |
this.connector = connector;
|
|
77 |
this.registrars.addAll(registrars);
|
|
78 |
this.endpointInstance = endpointInstance;
|
|
79 |
}
|
|
80 |
|
|
81 |
private void consumer(CanalEntry.Entry entry) {
|
|
82 |
if (IGNORE_ENTRY_TYPES.contains(entry.getEntryType())) return;
|
|
83 |
CanalEntry.RowChange rowChange = null;
|
|
84 |
try {
|
|
85 |
rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
|
|
86 |
} catch (Exception e) {
|
|
87 |
logger.error("[MessageTransponderContainer_consumer] RowChange parse has an error ", e);
|
|
88 |
throw new RuntimeException("RowChange parse has an error , data:" + entry.toString(), e);
|
|
89 |
}
|
|
90 |
|
|
91 |
// 忽略 ddl 语句
|
|
92 |
if (rowChange.hasIsDdl() && rowChange.getIsDdl()) return;
|
|
93 |
CanalEntry.EventType eventType = rowChange.getEventType();
|
|
94 |
if (!SUPPORT_ALL_TYPES.contains(eventType)) return;
|
|
95 |
for (CanalEntry.RowData rowData : rowChange.getRowDatasList()) {
|
|
96 |
registrars
|
|
97 |
.stream()
|
|
98 |
.filter(CanalListenerEndpointRegistrar.filterArgs(
|
|
99 |
entry.getHeader().getSchemaName(),
|
|
100 |
entry.getHeader().getTableName(),
|
|
101 |
eventType))
|
|
102 |
.forEach(element -> {
|
|
103 |
try {
|
|
104 |
element.getListenerEntry().getKey().invoke(element.getBean(), eventType, rowData);
|
|
105 |
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
106 |
logger.error("[MessageTransponderContainer_consumer] RowData Callback Method invoke error message", e);
|
|
107 |
throw new RuntimeException("RowData Callback Method invoke error message: " + e.getMessage(), e);
|
|
108 |
}
|
|
109 |
});
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
}
|