mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
rdb 批量提交
This commit is contained in:
@@ -2,16 +2,16 @@ package com.alibaba.otter.canal.adapter.launcher.monitor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.apache.commons.io.filefilter.FileFilterUtils;
|
||||
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
|
||||
import org.apache.commons.io.monitor.FileAlterationMonitor;
|
||||
import org.apache.commons.io.monitor.FileAlterationObserver;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
@@ -19,76 +19,72 @@ import org.springframework.stereotype.Component;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import com.alibaba.otter.canal.adapter.launcher.loader.CanalAdapterService;
|
||||
import com.alibaba.otter.canal.client.adapter.support.Constant;
|
||||
import com.alibaba.otter.canal.client.adapter.support.Util;
|
||||
|
||||
@Component
|
||||
public class ApplicationRunningMonitor {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApplicationRunningMonitor.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApplicationRunningMonitor.class);
|
||||
|
||||
@Resource
|
||||
private ContextRefresher contextRefresher;
|
||||
private ContextRefresher contextRefresher;
|
||||
|
||||
@Resource
|
||||
private CanalAdapterService canalAdapterService;
|
||||
private CanalAdapterService canalAdapterService;
|
||||
|
||||
private ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
|
||||
|
||||
private static volatile long applicationLastModify = -1L;
|
||||
private FileAlterationMonitor fileMonitor;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
URL url = this.getClass().getClassLoader().getResource("");
|
||||
String path;
|
||||
if (url != null) {
|
||||
path = url.getPath();
|
||||
} else {
|
||||
path = new File("").getAbsolutePath();
|
||||
File confDir = Util.getConfDirPath();
|
||||
try {
|
||||
FileAlterationObserver observer = new FileAlterationObserver(confDir,
|
||||
FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
|
||||
FileFilterUtils.prefixFileFilter("application"),
|
||||
FileFilterUtils.suffixFileFilter("yml")));
|
||||
FileListener listener = new FileListener();
|
||||
observer.addListener(listener);
|
||||
fileMonitor = new FileAlterationMonitor(3000, observer);
|
||||
fileMonitor.start();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
File file = null;
|
||||
if (path != null) {
|
||||
file = new File(path + ".." + File.separator + Constant.CONF_DIR + File.separator + "application.yml");
|
||||
if (!file.exists()) {
|
||||
file = new File(path + "application.yml");
|
||||
}
|
||||
}
|
||||
if (file == null || !file.exists()) {
|
||||
throw new RuntimeException("application.yml config file not found.");
|
||||
}
|
||||
File configFile = file;
|
||||
scheduled.scheduleWithFixedDelay(() -> {
|
||||
if (applicationLastModify == -1L) {
|
||||
applicationLastModify = configFile.lastModified();
|
||||
} else {
|
||||
if (configFile.lastModified() != applicationLastModify) {
|
||||
applicationLastModify = configFile.lastModified();
|
||||
|
||||
try {
|
||||
// 检查yml格式
|
||||
new Yaml().loadAs(new FileReader(configFile), Map.class);
|
||||
|
||||
canalAdapterService.destroy();
|
||||
|
||||
// refresh context
|
||||
contextRefresher.refresh();
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
canalAdapterService.init();
|
||||
logger.info("## adapter application config reloaded.");
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 5, 5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
scheduled.shutdown();
|
||||
try {
|
||||
fileMonitor.stop();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private class FileListener extends FileAlterationListenerAdaptor {
|
||||
|
||||
@Override
|
||||
public void onFileChange(File file) {
|
||||
super.onFileChange(file);
|
||||
try {
|
||||
// 检查yml格式
|
||||
new Yaml().loadAs(new FileReader(file), Map.class);
|
||||
|
||||
canalAdapterService.destroy();
|
||||
|
||||
// refresh context
|
||||
contextRefresher.refresh();
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
canalAdapterService.init();
|
||||
logger.info("## adapter application config reloaded.");
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ server:
|
||||
port: 8081
|
||||
logging:
|
||||
level:
|
||||
# org.springframework: WARN
|
||||
org.springframework: WARN
|
||||
com.alibaba.otter.canal.client.adapter.hbase: DEBUG
|
||||
com.alibaba.otter.canal.client.adapter.es: DEBUG
|
||||
com.alibaba.otter.canal.client.adapter.rdb: DEBUG
|
||||
@@ -28,16 +28,16 @@ canal.conf:
|
||||
- instance: example
|
||||
groups:
|
||||
- outAdapters:
|
||||
- name: logger
|
||||
# - name: rdb
|
||||
# key: oracle1
|
||||
# properties:
|
||||
# jdbc.driverClassName: oracle.jdbc.OracleDriver
|
||||
# jdbc.url: jdbc:oracle:thin:@localhost:49161:XE
|
||||
# jdbc.username: mytest
|
||||
# jdbc.password: m121212
|
||||
# threads: 5
|
||||
# commitSize: 3000
|
||||
# - name: logger
|
||||
- name: rdb
|
||||
key: oracle1
|
||||
properties:
|
||||
jdbc.driverClassName: oracle.jdbc.OracleDriver
|
||||
jdbc.url: jdbc:oracle:thin:@localhost:49161:XE
|
||||
jdbc.username: mytest
|
||||
jdbc.password: m121212
|
||||
threads: 5
|
||||
commitSize: 5000
|
||||
# - name: rdb
|
||||
# key: postgres1
|
||||
# properties:
|
||||
|
||||
Reference in New Issue
Block a user