use of io.openmessaging.connector.api.Connector in project rocketmq-externals by apache.
the class ConfigManagementServiceImpl method putConnectorConfig.
@Override
public String putConnectorConfig(String connectorName, ConnectKeyValue configs) throws Exception {
ConnectKeyValue exist = connectorKeyValueStore.get(connectorName);
if (null != exist) {
Long updateTimestamp = exist.getLong(RuntimeConfigDefine.UPDATE_TIMESTAMP);
if (null != updateTimestamp) {
configs.put(RuntimeConfigDefine.UPDATE_TIMESTAMP, updateTimestamp);
}
}
if (configs.equals(exist)) {
return "Connector with same config already exist.";
}
Long currentTimestamp = System.currentTimeMillis();
configs.put(RuntimeConfigDefine.UPDATE_TIMESTAMP, currentTimestamp);
for (String requireConfig : RuntimeConfigDefine.REQUEST_CONFIG) {
if (!configs.containsKey(requireConfig)) {
return "Request config key: " + requireConfig;
}
}
String connectorClass = configs.getString(RuntimeConfigDefine.CONNECTOR_CLASS);
ClassLoader classLoader = plugin.getPluginClassLoader(connectorClass);
Class clazz;
if (null != classLoader) {
clazz = Class.forName(connectorClass, true, classLoader);
} else {
clazz = Class.forName(connectorClass);
}
final Connector connector = (Connector) clazz.getDeclaredConstructor().newInstance();
String errorMessage = connector.verifyAndSetConfig(configs);
if (errorMessage != null && errorMessage.length() > 0) {
return errorMessage;
}
connectorKeyValueStore.put(connectorName, configs);
recomputeTaskConfigs(connectorName, connector, currentTimestamp);
return "";
}
use of io.openmessaging.connector.api.Connector in project rocketmq-externals by apache.
the class DefaultConnectorContext method requestTaskReconfiguration.
@Override
public void requestTaskReconfiguration() {
Set<WorkerConnector> connectors = controller.getWorker().getWorkingConnectors();
WorkerConnector currentConnector = null;
for (WorkerConnector workerConnector : connectors) {
if (workerConnector.getConnectorName().equals(connectorName)) {
currentConnector = workerConnector;
}
}
if (null != currentConnector) {
Connector connector = currentConnector.getConnector();
controller.getConfigManagementService().recomputeTaskConfigs(connectorName, connector, System.currentTimeMillis());
log.info("Connector {} recompute taskConfigs success.", connectorName);
} else {
log.info("Not found connector {}.", connectorName);
}
}
use of io.openmessaging.connector.api.Connector in project rocketmq-externals by apache.
the class Worker method startConnectors.
/**
* Start a collection of connectors with the given configs. If a connector is already started with the same configs,
* it will not start again. If a connector is already started but not contained in the new configs, it will stop.
*
* @param connectorConfigs
* @param connectController
* @throws Exception
*/
public synchronized void startConnectors(Map<String, ConnectKeyValue> connectorConfigs, ConnectController connectController) throws Exception {
Set<WorkerConnector> stoppedConnector = new HashSet<>();
for (WorkerConnector workerConnector : workingConnectors) {
String connectorName = workerConnector.getConnectorName();
ConnectKeyValue keyValue = connectorConfigs.get(connectorName);
if (null == keyValue || 0 != keyValue.getInt(RuntimeConfigDefine.CONFIG_DELETED)) {
workerConnector.stop();
log.info("Connector {} stop", workerConnector.getConnectorName());
stoppedConnector.add(workerConnector);
} else if (!keyValue.equals(workerConnector.getKeyValue())) {
workerConnector.reconfigure(keyValue);
}
}
workingConnectors.removeAll(stoppedConnector);
if (null == connectorConfigs || 0 == connectorConfigs.size()) {
return;
}
Map<String, ConnectKeyValue> newConnectors = new HashMap<>();
for (String connectorName : connectorConfigs.keySet()) {
boolean isNewConnector = true;
for (WorkerConnector workerConnector : workingConnectors) {
if (workerConnector.getConnectorName().equals(connectorName)) {
isNewConnector = false;
break;
}
}
if (isNewConnector) {
newConnectors.put(connectorName, connectorConfigs.get(connectorName));
}
}
for (String connectorName : newConnectors.keySet()) {
ConnectKeyValue keyValue = newConnectors.get(connectorName);
String connectorClass = keyValue.getString(RuntimeConfigDefine.CONNECTOR_CLASS);
ClassLoader loader = plugin.getPluginClassLoader(connectorClass);
final ClassLoader currentThreadLoader = plugin.currentThreadLoader();
Class clazz;
boolean isolationFlag = false;
if (loader instanceof PluginClassLoader) {
clazz = ((PluginClassLoader) loader).loadClass(connectorClass, false);
isolationFlag = true;
} else {
clazz = Class.forName(connectorClass);
}
final Connector connector = (Connector) clazz.getDeclaredConstructor().newInstance();
WorkerConnector workerConnector = new WorkerConnector(connectorName, connector, connectorConfigs.get(connectorName), new DefaultConnectorContext(connectorName, connectController));
if (isolationFlag) {
Plugin.compareAndSwapLoaders(loader);
}
workerConnector.initialize();
workerConnector.start();
log.info("Connector {} start", workerConnector.getConnectorName());
Plugin.compareAndSwapLoaders(currentThreadLoader);
this.workingConnectors.add(workerConnector);
}
}
Aggregations