recreate the EventBus on restart to avoid classloader leaks

This commit is contained in:
Sebastian Sdorra
2019-06-05 16:10:15 +02:00
parent 73dc0d0544
commit 2b64a49e11
3 changed files with 29 additions and 6 deletions

View File

@@ -39,6 +39,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContext;
import sonia.scm.Stage;
import sonia.scm.event.RecreateEventBusEvent;
import sonia.scm.event.ScmEventBus;
import javax.servlet.FilterConfig;
@@ -72,7 +73,7 @@ public class BootstrapContextFilter extends GuiceFilter
*
* @throws ServletException
*/
@Subscribe(async = false)
@Subscribe
public void handleRestartEvent(RestartEvent event) throws ServletException
{
logger.warn("received restart event from {} with reason: {}",
@@ -87,6 +88,10 @@ public class BootstrapContextFilter extends GuiceFilter
logger.warn("destroy filter pipeline, because of a received restart event");
destroy();
logger.warn("send recreate eventbus event");
ScmEventBus.getInstance().post(new RecreateEventBusEvent());
ScmEventBus.getInstance().register(this);
logger.warn("reinitialize filter pipeline, because of a received restart event");
initGuice();
}

View File

@@ -36,7 +36,7 @@ package sonia.scm.event;
//~--- non-JDK imports --------------------------------------------------------
import com.github.legman.EventBus;
import com.github.legman.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -62,9 +62,13 @@ public class LegmanScmEventBus extends ScmEventBus
* Constructs ...
*
*/
public LegmanScmEventBus()
{
eventBus = new EventBus(NAME);
public LegmanScmEventBus() {
eventBus = create();
}
private EventBus create() {
logger.info("create new event bus {}", NAME);
return new EventBus(NAME);
}
//~--- methods --------------------------------------------------------------
@@ -118,8 +122,15 @@ public class LegmanScmEventBus extends ScmEventBus
}
}
@Subscribe(async = false)
public void recreateEventBus(RecreateEventBusEvent recreateEventBusEvent) {
logger.info("shutdown event bus executor");
eventBus.shutdown();
eventBus = create();
}
//~--- fields ---------------------------------------------------------------
/** event bus */
private final EventBus eventBus;
private EventBus eventBus;
}

View File

@@ -0,0 +1,7 @@
package sonia.scm.event;
/**
* This event forces the {@link ScmEventBus} to recreate the underlying implementation and to clear all its caches.
* Note: After this event is fired, every subscription is removed from the event bus.
*/
public final class RecreateEventBusEvent {}