Search in sources :

Example 1 with CanalAlarmHandler

use of com.alibaba.otter.canal.common.alarm.CanalAlarmHandler in project canal by alibaba.

the class CanalInstanceWithManager method initAlarmHandler.

@SuppressWarnings("resource")
protected void initAlarmHandler() {
    logger.info("init alarmHandler begin...");
    String alarmHandlerClass = parameters.getAlarmHandlerClass();
    String alarmHandlerPluginDir = parameters.getAlarmHandlerPluginDir();
    if (alarmHandlerClass == null || alarmHandlerPluginDir == null) {
        alarmHandler = new LogAlarmHandler();
    } else {
        try {
            File externalLibDir = new File(alarmHandlerPluginDir);
            File[] jarFiles = externalLibDir.listFiles((dir, name) -> name.endsWith(".jar"));
            if (jarFiles == null || jarFiles.length == 0) {
                throw new IllegalStateException(String.format("alarmHandlerPluginDir [%s] can't find any name endswith \".jar\" file.", alarmHandlerPluginDir));
            }
            URL[] urls = new URL[jarFiles.length];
            for (int i = 0; i < jarFiles.length; i++) {
                urls[i] = jarFiles[i].toURI().toURL();
            }
            ClassLoader currentClassLoader = new URLClassLoader(urls, CanalInstanceWithManager.class.getClassLoader());
            Class<CanalAlarmHandler> _alarmClass = (Class<CanalAlarmHandler>) currentClassLoader.loadClass(alarmHandlerClass);
            alarmHandler = _alarmClass.newInstance();
            logger.info("init [{}] alarm handler success.", alarmHandlerClass);
        } catch (Throwable e) {
            String errorMsg = String.format("init alarmHandlerPluginDir [%s] alarm handler [%s] error: %s", alarmHandlerPluginDir, alarmHandlerClass, ExceptionUtils.getFullStackTrace(e));
            logger.error(errorMsg);
            throw new CanalException(errorMsg, e);
        }
    }
    logger.info("init alarmHandler end! \n\t load CanalAlarmHandler:{} ", alarmHandler.getClass().getName());
}
Also used : CanalAlarmHandler(com.alibaba.otter.canal.common.alarm.CanalAlarmHandler) URL(java.net.URL) LogAlarmHandler(com.alibaba.otter.canal.common.alarm.LogAlarmHandler) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) CanalException(com.alibaba.otter.canal.common.CanalException)

Aggregations

CanalException (com.alibaba.otter.canal.common.CanalException)1 CanalAlarmHandler (com.alibaba.otter.canal.common.alarm.CanalAlarmHandler)1 LogAlarmHandler (com.alibaba.otter.canal.common.alarm.LogAlarmHandler)1 File (java.io.File)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1