- change version on 1.8
- add docker-compose - update entaxy
This commit is contained in:
@@ -76,7 +76,7 @@ public class InitManager {
|
||||
|
||||
protected List<String> waiting = new ArrayList<>();
|
||||
|
||||
protected class InitializerMeta {
|
||||
protected class InitializerMeta implements Initializer.Callback {
|
||||
|
||||
String id;
|
||||
String className;
|
||||
@@ -84,12 +84,16 @@ public class InitManager {
|
||||
|
||||
Bundle bundle;
|
||||
|
||||
List<InitializerMeta> dependsOn = new ArrayList<>();
|
||||
List<InitializerMeta> dependedBy = new ArrayList<>();
|
||||
Map<String, InitializerMeta> dependsOn = new HashMap<>();
|
||||
Map<String, InitializerMeta> dependedBy = new HashMap<>();
|
||||
|
||||
boolean executed = false;
|
||||
boolean toBeExecuted = true;
|
||||
|
||||
int retries = 1;
|
||||
|
||||
int interval = 1000;
|
||||
|
||||
public void load(String initializerData) {
|
||||
String[] data = initializerData.split("\\?");
|
||||
|
||||
@@ -120,6 +124,21 @@ public class InitManager {
|
||||
String repeat = params.get(Initializer.INITIALIZER_QUERY_STRING_PARAM_REPEAT);
|
||||
this.repeat = "true".equals(repeat);
|
||||
|
||||
String retriesValue = params.get(Initializer.INITIALIZER_QUERY_STRING_PARAM_RETRIES);
|
||||
if (!Strings.isNullOrEmpty(retriesValue))
|
||||
try {
|
||||
this.retries = Integer.parseInt(retriesValue);
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("Retries paramater [{}] is not an integer", retriesValue);
|
||||
}
|
||||
|
||||
String intervalValue = params.get(Initializer.INITIALIZER_QUERY_STRING_PARAM_INTERVAL);
|
||||
if (!Strings.isNullOrEmpty(intervalValue))
|
||||
try {
|
||||
this.interval = Integer.parseInt(intervalValue);
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("Interval paramater [{}] is not an integer", intervalValue);
|
||||
}
|
||||
}
|
||||
|
||||
protected void addDependency(InitializerMeta meta) {
|
||||
@@ -127,7 +146,7 @@ public class InitManager {
|
||||
if (meta.toBeExecuted && !meta.executed)
|
||||
synchronized (this.dependsOn) {
|
||||
meta.addDependent(this);
|
||||
this.dependsOn.add(meta);
|
||||
this.dependsOn.put(meta.id, meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,13 +155,15 @@ public class InitManager {
|
||||
this.className = other.className;
|
||||
this.bundle = other.bundle;
|
||||
this.repeat = other.repeat;
|
||||
for (InitializerMeta meta: other.dependsOn)
|
||||
this.retries = other.retries;
|
||||
this.interval = other.interval;
|
||||
for (InitializerMeta meta: other.dependsOn.values())
|
||||
this.addDependency(meta);
|
||||
}
|
||||
|
||||
public void addDependent(InitializerMeta meta) {
|
||||
synchronized (this.dependedBy) {
|
||||
this.dependedBy.add(meta);
|
||||
this.dependedBy.put(meta.id, meta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +176,7 @@ public class InitManager {
|
||||
}
|
||||
|
||||
public void notifyDependent() {
|
||||
for (InitializerMeta meta: this.dependedBy)
|
||||
for (InitializerMeta meta: this.dependedBy.values())
|
||||
meta.dependencyReady(this);
|
||||
}
|
||||
|
||||
@@ -190,14 +211,39 @@ public class InitManager {
|
||||
|
||||
}
|
||||
|
||||
public void setCallback() {
|
||||
BundleWiring wiring = bundle.adapt(BundleWiring.class);
|
||||
ClassLoader cl = wiring.getClassLoader();
|
||||
Class<?> clazz;
|
||||
try {
|
||||
clazz = cl.loadClass(className);
|
||||
log.info("Loaded class {}", clazz.getName());
|
||||
Constructor<?> constructor = clazz.getConstructor();
|
||||
Initializer initializer = (Initializer) constructor.newInstance();
|
||||
initializer.setCalllback(this);
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void dependencyReady(InitializerMeta meta) {
|
||||
synchronized (this.dependsOn) {
|
||||
this.dependsOn.remove(meta);
|
||||
this.dependsOn.remove(meta.id);
|
||||
if (!this.executed && this.canExecute())
|
||||
this.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inited(Initializer owner) {
|
||||
log.info("Initializer with id {} notified of successful init via callback");
|
||||
InitManager.this.updateById(id, true);
|
||||
this.executed = true;
|
||||
this.notifyDependent();
|
||||
owner.setCalllback(null);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -268,7 +314,7 @@ public class InitManager {
|
||||
}
|
||||
|
||||
if (!meta.canExecute()) {
|
||||
String dependsOn = meta.dependsOn.stream().map((m)->m.id).collect(Collectors.joining(","));
|
||||
String dependsOn = meta.dependsOn.values().stream().map((m)->m.id).collect(Collectors.joining(","));
|
||||
log.info("Initializer with id {} is waiting for dependencies: {}", meta.id, dependsOn);
|
||||
|
||||
/*
|
||||
@@ -276,8 +322,25 @@ public class InitManager {
|
||||
*/
|
||||
return;
|
||||
}
|
||||
int retriesCount = meta.retries;
|
||||
boolean result = false;
|
||||
while (!result && (retriesCount > 0)) {
|
||||
log.info("Executing initializer with id {}: try {} of {}", meta.id, meta.retries-retriesCount+1, meta.retries);
|
||||
retriesCount--;
|
||||
result = meta.execute();
|
||||
if ((retriesCount > 0) && !result)
|
||||
try {
|
||||
log.info("Executing initializer with id {}: sleeping for {}", meta.id, meta.interval);
|
||||
Thread.sleep(meta.interval);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("Failed sleep for thread {} on initializer {}", Thread.currentThread().getId(), meta.id);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
meta.execute();
|
||||
// last try
|
||||
if (!result)
|
||||
meta.setCallback();
|
||||
}
|
||||
|
||||
/* protected void execute(InitializerMeta meta) {
|
||||
|
@@ -52,4 +52,8 @@ public abstract class AbstractInitializer implements Initializer {
|
||||
return this.initializerId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCalllback(Callback callback) {
|
||||
// Ignore it by default
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,10 @@ import org.osgi.framework.BundleContext;
|
||||
|
||||
public interface Initializer {
|
||||
|
||||
public static interface Callback {
|
||||
public void inited(Initializer owner);
|
||||
}
|
||||
|
||||
public static final String INITIALIZER_CLASS_HEADER = "Entaxy-Initializer-Class";
|
||||
|
||||
|
||||
@@ -38,6 +42,8 @@ public interface Initializer {
|
||||
public static final String INITIALIZER_QUERY_STRING_PARAM_ID = "id";
|
||||
public static final String INITIALIZER_QUERY_STRING_PARAM_DEPENDS_ON = "depends-on";
|
||||
public static final String INITIALIZER_QUERY_STRING_PARAM_REPEAT = "repeat";
|
||||
public static final String INITIALIZER_QUERY_STRING_PARAM_RETRIES = "retries";
|
||||
public static final String INITIALIZER_QUERY_STRING_PARAM_INTERVAL = "interval";
|
||||
|
||||
public void init() throws InitializerException;
|
||||
|
||||
@@ -54,4 +60,6 @@ public interface Initializer {
|
||||
|
||||
public void setInitializerId(String id);
|
||||
public String getInitializerId();
|
||||
|
||||
public void setCalllback(Initializer.Callback callback);
|
||||
}
|
||||
|
Reference in New Issue
Block a user