Search in sources :

Example 1 with ManagerCanalInstanceGenerator

use of com.alibaba.otter.canal.instance.manager.ManagerCanalInstanceGenerator in project canal by alibaba.

the class CanalController method initGlobalConfig.

private InstanceConfig initGlobalConfig(Properties properties) {
    InstanceConfig globalConfig = new InstanceConfig();
    String modeStr = getProperty(properties, CanalConstants.getInstanceModeKey(CanalConstants.GLOBAL_NAME));
    if (StringUtils.isNotEmpty(modeStr)) {
        globalConfig.setMode(InstanceMode.valueOf(StringUtils.upperCase(modeStr)));
    }
    String lazyStr = getProperty(properties, CanalConstants.getInstancLazyKey(CanalConstants.GLOBAL_NAME));
    if (StringUtils.isNotEmpty(lazyStr)) {
        globalConfig.setLazy(Boolean.valueOf(lazyStr));
    }
    String managerAddress = getProperty(properties, CanalConstants.getInstanceManagerAddressKey(CanalConstants.GLOBAL_NAME));
    if (StringUtils.isNotEmpty(managerAddress)) {
        globalConfig.setManagerAddress(managerAddress);
    }
    String springXml = getProperty(properties, CanalConstants.getInstancSpringXmlKey(CanalConstants.GLOBAL_NAME));
    if (StringUtils.isNotEmpty(springXml)) {
        globalConfig.setSpringXml(springXml);
    }
    instanceGenerator = new CanalInstanceGenerator() {

        public CanalInstance generate(String destination) {
            InstanceConfig config = instanceConfigs.get(destination);
            if (config == null) {
                throw new CanalServerException("can't find destination:{}");
            }
            if (config.getMode().isManager()) {
                ManagerCanalInstanceGenerator instanceGenerator = new ManagerCanalInstanceGenerator();
                instanceGenerator.setCanalConfigClient(managerClients.get(config.getManagerAddress()));
                return instanceGenerator.generate(destination);
            } else if (config.getMode().isSpring()) {
                SpringCanalInstanceGenerator instanceGenerator = new SpringCanalInstanceGenerator();
                synchronized (this) {
                    try {
                        // 设置当前正在加载的通道,加载spring查找文件时会用到该变量
                        System.setProperty(CanalConstants.CANAL_DESTINATION_PROPERTY, destination);
                        instanceGenerator.setBeanFactory(getBeanFactory(config.getSpringXml()));
                        return instanceGenerator.generate(destination);
                    } catch (Throwable e) {
                        logger.error("generator instance failed.", e);
                        throw new CanalException(e);
                    } finally {
                        System.setProperty(CanalConstants.CANAL_DESTINATION_PROPERTY, "");
                    }
                }
            } else {
                throw new UnsupportedOperationException("unknow mode :" + config.getMode());
            }
        }
    };
    return globalConfig;
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) ManagerCanalInstanceGenerator(com.alibaba.otter.canal.instance.manager.ManagerCanalInstanceGenerator) ManagerCanalInstanceGenerator(com.alibaba.otter.canal.instance.manager.ManagerCanalInstanceGenerator) CanalInstanceGenerator(com.alibaba.otter.canal.instance.core.CanalInstanceGenerator) SpringCanalInstanceGenerator(com.alibaba.otter.canal.instance.spring.SpringCanalInstanceGenerator) SpringCanalInstanceGenerator(com.alibaba.otter.canal.instance.spring.SpringCanalInstanceGenerator) CanalServerException(com.alibaba.otter.canal.server.exception.CanalServerException) CanalException(com.alibaba.otter.canal.common.CanalException)

Aggregations

CanalException (com.alibaba.otter.canal.common.CanalException)1 CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)1 CanalInstanceGenerator (com.alibaba.otter.canal.instance.core.CanalInstanceGenerator)1 ManagerCanalInstanceGenerator (com.alibaba.otter.canal.instance.manager.ManagerCanalInstanceGenerator)1 SpringCanalInstanceGenerator (com.alibaba.otter.canal.instance.spring.SpringCanalInstanceGenerator)1 CanalServerException (com.alibaba.otter.canal.server.exception.CanalServerException)1