pf4j / pf4j-spring

Plugin Framework for Spring (PF4J - Spring Framework integration)
Apache License 2.0
346 stars 105 forks source link

SpringPlugin start/stop/start results in Exception #58

Closed michael-becker closed 1 year ago

michael-becker commented 4 years ago

I tested the following calls:

pluginManager.startPlugin(PLUGIN_ID);
pluginManager.getExtensions(PLUGIN_ID);
pluginManager.stopPlugin(PLUGIN_ID);

pluginManager.startPlugin(PLUGIN_ID);
pluginManager.getExtensions(PLUGIN_ID);
pluginManager.stopPlugin(PLUGIN_ID);

The secons call to getExtensions results in an Exception: java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@4a6facb0 has been closed already

I figured out the following workaround in SpringPlugin

    @Override
    public void stop() {
        // close applicationContext
        if ((applicationContext != null) && (applicationContext instanceof ConfigurableApplicationContext)) {
            ((ConfigurableApplicationContext) applicationContext).close();
            **applicationContext = null;**
        }
    }

By adding applicationContext = null a new application context is created when restarting the plugin. It should work but I am not 100% sure that there are no side effects so maybe you could have a quick check?

decebals commented 4 years ago

@michael-becker Thanks for reporting this issue. Can you create a PR please? A PR has a higher visibility (diff, ...). It will be awesome if the PR will contains a unit test.

decebals commented 1 year ago

A SNAPSHOT version is available on Maven central repository.