public void doAdd(@FormGroup("canalInfo") Group canalInfo,
@FormGroup("canalParameterInfo") Group canalParameterInfo,
@FormField(name = "formCanalError", group = "canalInfo") CustomErrors err,
@FormField(name = "formHeartBeatError", group = "canalParameterInfo") CustomErrors heartBeatErr,
Navigator nav) throws Exception {
Canal canal = new Canal();
CanalParameter parameter = new CanalParameter();
canalInfo.setProperties(canal);
canalParameterInfo.setProperties(parameter);
String zkClustersString = canalParameterInfo.getField("zkClusters").getStringValue();
String[] zkClusters = StringUtils.split(zkClustersString, ";");
parameter.setZkClusters(Arrays.asList(zkClusters));
Long zkClusterId = canalParameterInfo.getField("autoKeeperClusterId").getLongValue();
parameter.setZkClusterId(zkClusterId);
canal.setCanalParameter(parameter);
String dbAddressesString = canalParameterInfo.getField("groupDbAddresses").getStringValue();
// 解析格式:
// 127.0.0.1:3306:MYSQL,127.0.0.1:3306:ORACLE;127.0.0.1:3306,127.0.0.1:3306;
// 第一层的分号代表主备概念,,第二层逗号代表分组概念
if (StringUtils.isNotEmpty(dbAddressesString)) {
List<List<DataSourcing>> dbSocketAddress = new ArrayList<List<DataSourcing>>();
String[] dbAddresses = StringUtils.split(dbAddressesString, ";");
for (String dbAddressString : dbAddresses) {
List<DataSourcing> groupDbSocketAddress = new ArrayList<DataSourcing>();
String[] groupDbAddresses = StringUtils.split(dbAddressString, ",");
for (String groupDbAddress : groupDbAddresses) {
String strs[] = StringUtils.split(groupDbAddress, ":");
InetSocketAddress address = new InetSocketAddress(strs[0].trim(), Integer.valueOf(strs[1]));
SourcingType type = parameter.getSourcingType();
if (strs.length > 2) {
type = SourcingType.valueOf(strs[2]);
}
groupDbSocketAddress.add(new DataSourcing(type, address));
}
dbSocketAddress.add(groupDbSocketAddress);
}
parameter.setGroupDbAddresses(dbSocketAddress);
}
String positionsString = canalParameterInfo.getField("positions").getStringValue();
if (StringUtils.isNotEmpty(positionsString)) {
String positions[] = StringUtils.split(positionsString, ";");
parameter.setPositions(Arrays.asList(positions));
}
if (parameter.getDetectingEnable() && StringUtils.startsWithIgnoreCase(parameter.getDetectingSQL(), "select")) {
heartBeatErr.setMessage("invaliedHeartBeat");
return;
}
try {
canalService.create(canal);
} catch (RepeatConfigureException rce) {
err.setMessage("invalidCanal");
return;
}
if (parameter.getSourcingType().isMysql() && parameter.getSlaveId() == null) {
parameter.setSlaveId(10000 + canal.getId());
// 再次更新一下slaveId
try {
canalService.modify(canal);
} catch (RepeatConfigureException rce) {
err.setMessage("invalidCanal");