提交 8fdca1f4 编写于 作者: wu-sheng's avatar wu-sheng

Provide new stage of module initialization. Named `notifyAfterCompleted`. And...

Provide new stage of module initialization. Named `notifyAfterCompleted`. And rename `init` stage to `start`. Now the initialization sequence is prepare -> start -> notifyAfterCompleted
上级 1d884be3
......@@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.core.framework.DefineException;
import org.skywalking.apm.collector.core.framework.Loader;
import org.skywalking.apm.collector.core.util.DefinitionLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......
......@@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.core.framework.DefineException;
import org.skywalking.apm.collector.core.framework.Loader;
import org.skywalking.apm.collector.core.util.DefinitionLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......
......@@ -45,7 +45,11 @@ public class ClusterModuleRedisProvider extends ModuleProvider {
this.registerServiceImplementation(ModuleRegisterService.class, new RedisModuleRegistrationGetService());
}
@Override public void init(Properties config) throws ServiceNotProvidedException {
@Override public void start(Properties config) throws ServiceNotProvidedException {
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
......
......@@ -45,7 +45,11 @@ public class ClusterModuleStandaloneProvider extends ModuleProvider {
this.registerServiceImplementation(ModuleRegisterService.class, new StandaloneModuleRegistrationGetService());
}
@Override public void init(Properties config) throws ServiceNotProvidedException {
@Override public void start(Properties config) throws ServiceNotProvidedException {
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
......
......@@ -45,7 +45,11 @@ public class ClusterModuleZookeeperProvider extends ModuleProvider {
this.registerServiceImplementation(ModuleRegisterService.class, new ZookeeperModuleRegistrationGetService());
}
@Override public void init(Properties config) throws ServiceNotProvidedException {
@Override public void start(Properties config) throws ServiceNotProvidedException {
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.core;
/**
* @author wu-sheng
*/
public class UnexpectedException extends RuntimeException {
public UnexpectedException(String message) {
super(message);
}
}
......@@ -22,7 +22,7 @@ import java.util.HashMap;
import java.util.Properties;
/**
* Modulization configurations. The {@link ModuleManager} is going to start, lookup, init modules based on this.
* Modulization configurations. The {@link ModuleManager} is going to start, lookup, start modules based on this.
*
* @author wu-sheng
*/
......
......@@ -77,7 +77,7 @@ public abstract class Module {
}
}
void init(ModuleManager moduleManager,
void start(ModuleManager moduleManager,
ApplicationConfiguration.ModuleConfiguration configuration) throws ProviderNotFoundException, ModuleNotFoundException, ServiceNotProvidedException {
for (ModuleProvider provider : loadedProviders) {
String[] requiredModules = provider.requiredModules();
......@@ -88,12 +88,18 @@ public abstract class Module {
}
}
}
provider.init(configuration.getProviderConfiguration(provider.name()));
provider.start(configuration.getProviderConfiguration(provider.name()));
provider.requiredCheck(services());
}
}
void notifyAfterCompleted() throws ProviderNotFoundException, ModuleNotFoundException, ServiceNotProvidedException {
for (ModuleProvider provider : loadedProviders) {
provider.notifyAfterCompleted();
}
}
/**
* @return providers of this module
*/
......
......@@ -60,12 +60,16 @@ public class ModuleManager {
}
}
if (moduleList.size() > 0) {
throw new ModuleNotFoundException(moduleList.toString() + " missing.");
}
for (Module module : loadedModules.values()) {
module.init(this, applicationConfiguration.getModuleConfiguration(module.name()));
module.start(this, applicationConfiguration.getModuleConfiguration(module.name()));
}
if (moduleList.size() > 0) {
throw new ModuleNotFoundException(moduleList.toString() + " missing.");
for (Module module : loadedModules.values()) {
module.notifyAfterCompleted();
}
}
......
......@@ -63,11 +63,18 @@ public abstract class ModuleProvider {
public abstract void prepare(Properties config) throws ServiceNotProvidedException;
/**
* In prepare stage, the module can interop with other modules.
* In start stage, the module has been ready for interop.
*
* @param config from `application.yml`
*/
public abstract void init(Properties config) throws ServiceNotProvidedException;
public abstract void start(Properties config) throws ServiceNotProvidedException;
/**
* This callback executes after all modules start up successfully.
*
* @throws ServiceNotProvidedException
*/
public abstract void notifyAfterCompleted() throws ServiceNotProvidedException;
/**
* @return module names which does this module require?
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.core.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import org.skywalking.apm.collector.core.framework.DefinitionFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author peng-yongsheng
*/
public class DefinitionLoader<D> implements Iterable<D> {
private final Logger logger = LoggerFactory.getLogger(DefinitionLoader.class);
private final Class<D> definition;
private final DefinitionFile definitionFile;
protected DefinitionLoader(Class<D> svc, DefinitionFile definitionFile) {
this.definition = Objects.requireNonNull(svc, "definition interface cannot be null");
this.definitionFile = definitionFile;
}
public static <D> DefinitionLoader<D> load(Class<D> definition, DefinitionFile definitionFile) {
return new DefinitionLoader(definition, definitionFile);
}
@Override public final Iterator<D> iterator() {
logger.info("load definition file: {}", definitionFile.get());
List<String> definitionList = new LinkedList<>();
try {
Enumeration<URL> urlEnumeration = this.getClass().getClassLoader().getResources(definitionFile.get());
while (urlEnumeration.hasMoreElements()) {
URL definitionFileURL = urlEnumeration.nextElement();
logger.info("definition file url: {}", definitionFileURL.getPath());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(definitionFileURL.openStream()));
Properties properties = new Properties();
properties.load(bufferedReader);
Enumeration defineItem = properties.propertyNames();
while (defineItem.hasMoreElements()) {
String fullNameClass = (String)defineItem.nextElement();
definitionList.add(fullNameClass);
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
Iterator<String> moduleDefineIterator = definitionList.iterator();
return new Iterator<D>() {
@Override public boolean hasNext() {
return moduleDefineIterator.hasNext();
}
@Override public D next() {
String definitionClass = moduleDefineIterator.next();
logger.info("definitionClass: {}", definitionClass);
try {
Class c = Class.forName(definitionClass);
return (D)c.newInstance();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
};
}
}
......@@ -21,7 +21,7 @@ package org.skywalking.apm.collector.core.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import org.skywalking.apm.collector.core.framework.UnexpectedException;
import org.skywalking.apm.collector.core.UnexpectedException;
/**
* @author peng-yongsheng
......
......@@ -36,10 +36,14 @@ public class ModuleAProvider extends ModuleProvider {
this.registerServiceImplementation(BaseModuleA.ServiceABusiness1.class, new Business1());
}
@Override public void init(Properties config) throws ServiceNotProvidedException {
@Override public void start(Properties config) throws ServiceNotProvidedException {
this.registerServiceImplementation(BaseModuleA.ServiceABusiness2.class, new Business2());
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
@Override public String[] requiredModules() {
return new String[0];
}
......
......@@ -36,10 +36,14 @@ public class ModuleBProvider extends ModuleProvider {
this.registerServiceImplementation(BaseModuleB.ServiceBBusiness1.class, new Business1());
}
@Override public void init(Properties config) throws ServiceNotProvidedException {
@Override public void start(Properties config) throws ServiceNotProvidedException {
this.registerServiceImplementation(BaseModuleB.ServiceBBusiness2.class, new Business2());
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
@Override public String[] requiredModules() {
return new String[0];
}
......
......@@ -36,7 +36,11 @@ public class TestModuleProvider extends ModuleProvider {
}
@Override public void init(Properties config) {
@Override public void start(Properties config) {
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
......
......@@ -41,7 +41,11 @@ public class NamingModuleJettyProvider extends ModuleProvider {
@Override public void prepare(Properties config) throws ServiceNotProvidedException {
}
@Override public void init(Properties config) throws ServiceNotProvidedException {
@Override public void start(Properties config) throws ServiceNotProvidedException {
}
@Override public void notifyAfterCompleted() throws ServiceNotProvidedException {
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册