提交 | 用户 | age
|
de8c2b
|
1 |
package com.duxinglangzi.canal.starter.configuration;
|
D |
2 |
|
|
3 |
import com.alibaba.otter.canal.protocol.CanalEntry;
|
|
4 |
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
|
5 |
import com.duxinglangzi.canal.starter.annotation.CanalListener;
|
|
6 |
import org.apache.commons.lang3.StringUtils;
|
|
7 |
|
|
8 |
import java.lang.reflect.Method;
|
|
9 |
import java.util.Arrays;
|
|
10 |
import java.util.List;
|
|
11 |
import java.util.Map;
|
|
12 |
import java.util.Set;
|
|
13 |
import java.util.function.Predicate;
|
|
14 |
import java.util.stream.Collectors;
|
|
15 |
|
|
16 |
/**
|
627420
|
17 |
* 登记员
|
de8c2b
|
18 |
* @author wuqiong 2022/4/11
|
D |
19 |
*/
|
|
20 |
public class CanalListenerEndpointRegistrar {
|
|
21 |
|
|
22 |
private Object bean;
|
|
23 |
private Map.Entry<Method, CanalListener> listenerEntry;
|
|
24 |
|
627420
|
25 |
/**
|
D |
26 |
* 1、目前实现的 DML 解析器仅支持两个参数 <p>
|
|
27 |
* 2、且顺序必须为: CanalEntry.EventType 、 CanalEntry.RowData <p>
|
|
28 |
* 3、如果CanalListener 指定的 destination 不在配置文件内,则直接抛错 <p>
|
|
29 |
* @param sets
|
|
30 |
* @return void
|
|
31 |
* @author wuqiong 2022-04-23 20:27
|
|
32 |
*/
|
de8c2b
|
33 |
public void checkParameter(Set<String> sets) {
|
D |
34 |
List<Class<?>> classes = parameterTypes();
|
|
35 |
if (classes.size() > 2
|
|
36 |
|| classes.get(1) != CanalEntry.RowData.class
|
|
37 |
|| classes.get(0) != CanalEntry.EventType.class)
|
|
38 |
throw new IllegalArgumentException("@CanalListener Method Parameter Type Invalid, " +
|
|
39 |
"Need Parameter Type [CanalEntry.EventType,CanalEntry.RowData] please check ");
|
|
40 |
if (StringUtils.isNotBlank(getListenerEntry().getValue().destination())
|
|
41 |
&& !sets.contains(getListenerEntry().getValue().destination()))
|
|
42 |
throw new CanalClientException("@CanalListener Illegal destination , please check ");
|
|
43 |
|
|
44 |
}
|
|
45 |
|
|
46 |
|
|
47 |
public List<Class<?>> parameterTypes() {
|
|
48 |
return Arrays.stream(getListenerEntry().getKey().getParameterTypes()).collect(Collectors.toList());
|
|
49 |
}
|
|
50 |
|
|
51 |
public boolean isContainDestination(String destination) {
|
|
52 |
if (StringUtils.isBlank(getListenerEntry().getValue().destination())) return true;
|
|
53 |
return getListenerEntry().getValue().destination().equals(destination);
|
|
54 |
}
|
|
55 |
|
627420
|
56 |
/**
|
D |
57 |
* 过滤参数
|
|
58 |
* @param database
|
|
59 |
* @param tableName
|
|
60 |
* @param eventType
|
|
61 |
* @return java.util.function.Predicate
|
|
62 |
* @author wuqiong 2022-04-23 20:47
|
|
63 |
*/
|
de8c2b
|
64 |
public static Predicate<CanalListenerEndpointRegistrar> filterArgs(
|
D |
65 |
String database, String tableName, CanalEntry.EventType eventType) {
|
|
66 |
Predicate<CanalListenerEndpointRegistrar> databases = e -> StringUtils.isBlank(e.getListenerEntry().getValue().database())
|
|
67 |
|| e.getListenerEntry().getValue().database().equals(database);
|
|
68 |
Predicate<CanalListenerEndpointRegistrar> table = e -> e.getListenerEntry().getValue().table().length == 0
|
|
69 |
|| (e.getListenerEntry().getValue().table().length == 1 && "".equals(e.getListenerEntry().getValue().table()[0]))
|
|
70 |
|| Arrays.stream(e.getListenerEntry().getValue().table()).anyMatch(s -> s.equals(tableName));
|
|
71 |
Predicate<CanalListenerEndpointRegistrar> eventTypes = e -> e.getListenerEntry().getValue().eventType().length == 0
|
|
72 |
|| Arrays.stream(e.getListenerEntry().getValue().eventType()).anyMatch(eve -> eve == eventType);
|
|
73 |
return databases.and(table).and(eventTypes);
|
|
74 |
}
|
|
75 |
|
|
76 |
|
|
77 |
public CanalListenerEndpointRegistrar(Object bean, Map.Entry<Method, CanalListener> entry) {
|
|
78 |
this.bean = bean;
|
|
79 |
this.listenerEntry = entry;
|
|
80 |
}
|
|
81 |
|
|
82 |
public Map.Entry<Method, CanalListener> getListenerEntry() {
|
|
83 |
return listenerEntry;
|
|
84 |
}
|
|
85 |
|
|
86 |
public Object getBean() {
|
|
87 |
return bean;
|
|
88 |
}
|
|
89 |
}
|