renxue
2022-10-24 0b055a3f554da3a934e79e88c4781705cbab5a21
提交 | 用户 | age
0b055a 1 package com.hz.canal.starter.factory;
R 2
3 import com.alibaba.otter.canal.client.CanalConnector;
4 import com.hz.canal.starter.configuration.CanalAutoConfigurationProperties;
5 import com.hz.canal.starter.configuration.CanalListenerEndpointRegistrar;
6 import com.hz.canal.starter.container.DmlMessageTransponderContainer;
7 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
8
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 /**
15  * @author wuqiong 2022/4/18
16  * @description
17  */
18 public class TransponderContainerFactory {
19
20     private static final String CONTAINER_ID_PREFIX = "com.duxinglangzi.canal.starter.container.MessageTransponderContainer#";
21
22
23     /**
24      * 将所有待注册的端点,注册到spring中
25      *
26      * @param beanFactory
27      * @param canalConfig
28      * @param registrars
29      * @return void
30      * @author wuqiong 2022-04-23 20:34
31      */
32     public static void registerListenerContainer(
33             ConfigurableListableBeanFactory beanFactory, CanalAutoConfigurationProperties canalConfig,
34             Set<CanalListenerEndpointRegistrar> registrars) {
35         if (registrars == null || registrars.isEmpty()) return;
36         if (canalConfig == null || canalConfig.getInstances().isEmpty()) return;
37
38         for (Map.Entry<String, CanalAutoConfigurationProperties.EndpointInstance> endpointInstance : canalConfig.getInstances().entrySet()) {
39             if (beanFactory.containsBean(getContainerID(endpointInstance.getKey()))) continue; // 如果已经存在则不在创建
40             List<CanalListenerEndpointRegistrar> registrarList = new ArrayList<>();
41             for (CanalListenerEndpointRegistrar registrar : registrars) {
42                 registrar.checkParameter(canalConfig.getInstances().keySet());
43                 if (!registrar.isContainDestination(endpointInstance.getKey())) continue;
44                 registrarList.add(registrar);
45             }
46             if (registrarList.isEmpty()) continue;
47             registerTransponderContainer(
48                     endpointInstance.getKey(), endpointInstance.getValue(), beanFactory, registrarList);
49         }
50
51     }
52
53     private static void registerTransponderContainer(
54             String destination, CanalAutoConfigurationProperties.EndpointInstance endpointInstance,
55             ConfigurableListableBeanFactory beanFactory, List<CanalListenerEndpointRegistrar> registrarList) {
56         CanalConnector connector = CanalConnectorFactory.createConnector(destination, endpointInstance);
57         beanFactory.registerSingleton(getContainerID(destination),
58                 new DmlMessageTransponderContainer(connector, registrarList, endpointInstance));
59     }
60
61
62     private static String getContainerID(String destination) {
63         return CONTAINER_ID_PREFIX + "#" + destination;
64     }
65
66
67 }