ENTAXY-248 release 1.8.1
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<groupId>ru.entaxy.esb.platform.runtime.core</groupId>
|
||||
<artifactId>initializer</artifactId>
|
||||
<version>1.8.0</version>
|
||||
<version>1.8.1</version>
|
||||
</parent>
|
||||
<groupId>ru.entaxy.esb.platform.runtime.core.initializer</groupId>
|
||||
<artifactId>init-manager</artifactId>
|
||||
@ -13,6 +13,7 @@
|
||||
|
||||
<properties>
|
||||
<bundle.osgi.export.pkg>ru.entaxy.esb.platform.runtime.core.initializer.api</bundle.osgi.export.pkg>
|
||||
<!-- bundle.osgi.private.pkg>ru.entaxy.esb.platform.runtime.core.initializer</bundle.osgi.private.pkg -->
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
@ -34,6 +34,7 @@ import org.apache.felix.utils.properties.TypedProperties;
|
||||
import org.apache.karaf.config.core.ConfigRepository;
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.FrameworkUtil;
|
||||
import org.osgi.framework.InvalidSyntaxException;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
import org.osgi.framework.wiring.BundleWiring;
|
||||
@ -82,7 +83,10 @@ public class InitManager {
|
||||
String className;
|
||||
boolean repeat;
|
||||
|
||||
Bundle bundle;
|
||||
// Bundle bundle;
|
||||
long bundleId;
|
||||
|
||||
BundleContext bundleContext;
|
||||
|
||||
Map<String, InitializerMeta> dependsOn = new HashMap<>();
|
||||
Map<String, InitializerMeta> dependedBy = new HashMap<>();
|
||||
@ -153,10 +157,11 @@ public class InitManager {
|
||||
|
||||
public void load(InitializerMeta other) {
|
||||
this.className = other.className;
|
||||
this.bundle = other.bundle;
|
||||
this.bundleId = other.bundleId;
|
||||
this.repeat = other.repeat;
|
||||
this.retries = other.retries;
|
||||
this.interval = other.interval;
|
||||
this.bundleContext = other.bundleContext;
|
||||
for (InitializerMeta meta: other.dependsOn.values())
|
||||
this.addDependency(meta);
|
||||
}
|
||||
@ -180,6 +185,10 @@ public class InitManager {
|
||||
meta.dependencyReady(this);
|
||||
}
|
||||
|
||||
public Bundle getBundle() {
|
||||
return bundleContext.getBundle(bundleId);
|
||||
}
|
||||
|
||||
public boolean execute() {
|
||||
/**
|
||||
* instantiate initializer and call "init"
|
||||
@ -187,6 +196,7 @@ public class InitManager {
|
||||
*
|
||||
* @see <code>Initializer</code>
|
||||
*/
|
||||
Bundle bundle = getBundle();
|
||||
BundleWiring wiring = bundle.adapt(BundleWiring.class);
|
||||
ClassLoader cl = wiring.getClassLoader();
|
||||
Class<?> clazz;
|
||||
@ -212,7 +222,7 @@ public class InitManager {
|
||||
}
|
||||
|
||||
public void setCallback() {
|
||||
BundleWiring wiring = bundle.adapt(BundleWiring.class);
|
||||
BundleWiring wiring = getBundle().adapt(BundleWiring.class);
|
||||
ClassLoader cl = wiring.getClassLoader();
|
||||
Class<?> clazz;
|
||||
try {
|
||||
@ -246,6 +256,66 @@ public class InitManager {
|
||||
|
||||
};
|
||||
|
||||
protected static class Executor implements Runnable {
|
||||
|
||||
InitializerMeta meta;
|
||||
|
||||
public Executor(InitializerMeta meta) {
|
||||
this.meta = meta;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
/**
|
||||
* check the initialization status
|
||||
*/
|
||||
if (meta.isExecutedBefore()) {
|
||||
log.info("Initializer with id {} in bundle [{}] {} is already inited", meta.id,
|
||||
meta.bundleId, meta.getBundle().getSymbolicName());
|
||||
|
||||
if (meta.repeat)
|
||||
log.info("Initializer with id {} is set to be repeated", meta.id);
|
||||
else {
|
||||
meta.toBeExecuted = false;
|
||||
meta.notifyDependent();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!meta.canExecute()) {
|
||||
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);
|
||||
|
||||
/*
|
||||
* synchronized (this.waiting) { this.waiting.add(meta.id); }
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// last try
|
||||
if (!result)
|
||||
meta.setCallback();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected InitializerMeta getMetaById(String id) {
|
||||
synchronized (initializers) {
|
||||
@ -272,7 +342,8 @@ public class InitManager {
|
||||
protected InitializerMeta createMeta(String initializerMeta, Bundle bundle) {
|
||||
InitializerMeta meta = new InitializerMeta();
|
||||
meta.load(initializerMeta);
|
||||
meta.bundle = bundle;
|
||||
meta.bundleId = bundle.getBundleId();
|
||||
meta.bundleContext = bundleContext;
|
||||
|
||||
synchronized (this.initializers) {
|
||||
if (this.initializers.containsKey(meta.id)) {
|
||||
@ -297,50 +368,10 @@ public class InitManager {
|
||||
|
||||
InitializerMeta meta = createMeta(initializerClass, bundle);
|
||||
|
||||
/**
|
||||
* check the initialization status
|
||||
*/
|
||||
if (meta.isExecutedBefore()) {
|
||||
log.info("Initializer with id {} in bundle [{}] {} is already inited", meta.id,
|
||||
meta.bundle.getBundleId(), meta.bundle.getSymbolicName());
|
||||
|
||||
if (meta.repeat)
|
||||
log.info("Initializer with id {} is set to be repeated", meta.id);
|
||||
else {
|
||||
meta.toBeExecuted = false;
|
||||
meta.notifyDependent();
|
||||
return;
|
||||
}
|
||||
}
|
||||
Thread thread = new Thread(new Executor(meta));
|
||||
thread.setContextClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
thread.start();
|
||||
|
||||
if (!meta.canExecute()) {
|
||||
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);
|
||||
|
||||
/*
|
||||
* synchronized (this.waiting) { this.waiting.add(meta.id); }
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// last try
|
||||
if (!result)
|
||||
meta.setCallback();
|
||||
}
|
||||
|
||||
/* protected void execute(InitializerMeta meta) {
|
||||
|
Reference in New Issue
Block a user