2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2012-12-07 17:41:43 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2012-12-07 17:41:43 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2012-12-07 17:41:43 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2012-12-07 17:41:43 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2012-12-07 17:41:43 +01:00
|
|
|
*/
|
2021-03-24 15:54:29 +01:00
|
|
|
|
2014-01-03 12:56:18 +01:00
|
|
|
package sonia.scm.event;
|
2012-12-07 17:41:43 +01:00
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2014-01-03 12:56:18 +01:00
|
|
|
import com.github.legman.EventBus;
|
2019-06-05 16:10:15 +02:00
|
|
|
import com.github.legman.Subscribe;
|
2021-03-24 15:54:29 +01:00
|
|
|
import com.github.legman.micrometer.MicrometerPlugin;
|
|
|
|
|
import com.github.legman.shiro.ShiroPlugin;
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import io.micrometer.core.instrument.Tag;
|
2013-05-15 09:26:29 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2021-03-24 15:54:29 +01:00
|
|
|
import sonia.scm.metrics.MeterRegistryProvider;
|
2013-05-15 09:26:29 +02:00
|
|
|
|
2021-03-24 15:54:29 +01:00
|
|
|
import java.util.concurrent.Executor;
|
2019-06-19 11:53:13 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
2012-12-07 17:41:43 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
2021-03-24 15:54:29 +01:00
|
|
|
public class LegmanScmEventBus extends ScmEventBus {
|
2012-12-07 17:41:43 +01:00
|
|
|
|
2019-06-19 11:53:13 +02:00
|
|
|
private static final AtomicLong INSTANCE_COUNTER = new AtomicLong();
|
2021-03-24 15:54:29 +01:00
|
|
|
private static final String FORMAT_NAME = "ScmEventBus-%s";
|
2014-01-03 12:56:18 +01:00
|
|
|
|
2013-05-15 09:26:29 +02:00
|
|
|
/**
|
2014-01-03 12:56:18 +01:00
|
|
|
* the logger for LegmanScmEventBus
|
2013-05-15 09:26:29 +02:00
|
|
|
*/
|
2021-03-24 15:54:29 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LegmanScmEventBus.class);
|
2014-01-03 12:56:18 +01:00
|
|
|
|
2019-06-20 14:57:22 +02:00
|
|
|
private String name;
|
2021-03-24 15:54:29 +01:00
|
|
|
private EventBus eventBus;
|
2019-06-20 14:57:22 +02:00
|
|
|
|
2019-06-05 16:10:15 +02:00
|
|
|
public LegmanScmEventBus() {
|
2021-03-24 15:54:29 +01:00
|
|
|
eventBus = create(null);
|
2019-06-05 16:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-24 15:54:29 +01:00
|
|
|
@VisibleForTesting
|
|
|
|
|
LegmanScmEventBus(Executor executor) {
|
|
|
|
|
eventBus = create(executor);
|
2014-01-03 12:56:18 +01:00
|
|
|
}
|
2013-05-15 09:26:29 +02:00
|
|
|
|
2021-03-24 15:54:29 +01:00
|
|
|
private EventBus create(Executor executor) {
|
|
|
|
|
name = String.format(FORMAT_NAME, INSTANCE_COUNTER.incrementAndGet());
|
|
|
|
|
logger.info("create new event bus {}", name);
|
|
|
|
|
|
|
|
|
|
MicrometerPlugin micrometerPlugin = new MicrometerPlugin(MeterRegistryProvider.getStaticRegistry())
|
|
|
|
|
.withExecutorTags(Tag.of("type", "fixed"));
|
|
|
|
|
|
|
|
|
|
ShiroPlugin shiroPlugin = new ShiroPlugin();
|
|
|
|
|
|
|
|
|
|
EventBus.Builder builder = EventBus.builder()
|
|
|
|
|
.withIdentifier(name)
|
|
|
|
|
.withPlugins(shiroPlugin, micrometerPlugin);
|
|
|
|
|
|
|
|
|
|
if (executor != null) {
|
|
|
|
|
builder.withExecutor(executor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
2013-05-15 09:26:29 +02:00
|
|
|
|
2012-12-07 17:41:43 +01:00
|
|
|
@Override
|
2021-03-24 15:54:29 +01:00
|
|
|
public void post(Object event) {
|
2019-11-21 16:20:55 +01:00
|
|
|
if (eventBus != null) {
|
|
|
|
|
logger.debug("post {} to event bus {}", event, name);
|
|
|
|
|
eventBus.post(event);
|
|
|
|
|
} else {
|
|
|
|
|
logger.error("failed to post event {}, because event bus is shutdown", event);
|
|
|
|
|
}
|
2014-01-03 12:56:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers a object to the eventbus.
|
|
|
|
|
*
|
|
|
|
|
* @param object object to register
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2021-03-24 15:54:29 +01:00
|
|
|
public void register(Object object) {
|
2019-11-21 16:20:55 +01:00
|
|
|
if (eventBus != null) {
|
|
|
|
|
logger.trace("register {} to event bus {}", object, name);
|
|
|
|
|
eventBus.register(object);
|
|
|
|
|
} else {
|
|
|
|
|
logger.error("failed to register {}, because eventbus is shutdown", object);
|
|
|
|
|
}
|
2014-01-03 12:56:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2021-03-24 15:54:29 +01:00
|
|
|
public void unregister(Object object) {
|
2019-11-21 16:20:55 +01:00
|
|
|
if (eventBus != null) {
|
|
|
|
|
logger.trace("unregister {} from event bus {}", object, name);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
eventBus.unregister(object);
|
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
|
logger.trace("object {} was not registered", object);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logger.error("failed to unregister object {}, because event bus is shutdown", object);
|
2012-12-07 17:41:43 +01:00
|
|
|
}
|
2019-11-21 16:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Subscribe(async = false)
|
|
|
|
|
public void shutdownEventBus(ShutdownEventBusEvent shutdownEventBusEvent) {
|
|
|
|
|
if (eventBus != null) {
|
|
|
|
|
logger.info("shutdown event bus executor for {}, because of received ShutdownEventBusEvent", name);
|
|
|
|
|
eventBus.shutdown();
|
|
|
|
|
eventBus = null;
|
|
|
|
|
} else {
|
|
|
|
|
logger.warn("event bus was already shutdown");
|
2012-12-07 17:41:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-01-03 12:56:18 +01:00
|
|
|
|
2019-06-05 16:10:15 +02:00
|
|
|
@Subscribe(async = false)
|
|
|
|
|
public void recreateEventBus(RecreateEventBusEvent recreateEventBusEvent) {
|
2019-11-21 16:20:55 +01:00
|
|
|
if (eventBus != null) {
|
|
|
|
|
logger.info("shutdown event bus executor for {}, because of received RecreateEventBusEvent", name);
|
|
|
|
|
eventBus.shutdown();
|
|
|
|
|
}
|
|
|
|
|
logger.info("recreate event bus because of received RecreateEventBusEvent");
|
2021-03-24 15:54:29 +01:00
|
|
|
eventBus = create(null);
|
2019-06-05 16:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
2012-12-07 17:41:43 +01:00
|
|
|
}
|