Cleanup resources when plugin startup fails (#5765)

Signed-off-by: JohnNiang <johnniang@foxmail.com>
pull/5719/head
John Niang 2024-04-22 16:57:53 +08:00 committed by GitHub
parent b4b6693732
commit b74f7c4463
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 23 deletions

View File

@ -25,6 +25,7 @@ public class SpringPlugin extends Plugin {
@Override
public void start() {
try {
// initialize context
var pluginId = pluginContext.getName();
this.context = contextFactory.create(pluginId);
@ -41,28 +42,38 @@ public class SpringPlugin extends Plugin {
this.delegate.start();
}
context.publishEvent(new SpringPluginStartedEvent(this, this));
} catch (Throwable t) {
// try to stop plugin for cleaning resources if something went wrong
this.stop();
// propagate exception to invoker.
throw t;
}
}
@Override
public void stop() {
try {
if (context != null) {
context.publishEvent(new SpringPluginStoppingEvent(this, this));
}
if (this.delegate != null) {
this.delegate.stop();
}
} finally {
if (context instanceof ConfigurableApplicationContext configurableContext) {
configurableContext.close();
}
// reset application context
context = null;
}
}
@Override
public void delete() {
if (delegate != null) {
delegate.delete();
}
this.delegate = null;
}
public ApplicationContext getApplicationContext() {