
org.killbill.bus.InMemoryPersistentBus Maven / Gradle / Ivy
/*
* Copyright 2010-2011 Ning, Inc.
*
* Ning licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.killbill.bus;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;
import com.google.common.eventbus.EventBus;
import org.killbill.bus.api.PersistentBusConfig;
import org.skife.jdbi.v2.sqlobject.mixins.Transmogrifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.killbill.bus.api.BusEvent;
import org.killbill.bus.api.PersistentBus;
public class InMemoryPersistentBus implements PersistentBus {
private static final Logger log = LoggerFactory.getLogger(InMemoryPersistentBus.class);
private final EventBusDelegate delegate;
private final AtomicBoolean isInitialized;
@Override
public boolean startQueue() {
return true;
}
@Override
public void stopQueue() {
}
@Override
public boolean isStarted() {
return true;
}
public class EventBusDelegate extends EventBus {
public EventBusDelegate() {
}
public void completeDispatch() {
}
// No way to really 'stop' an executor; what we do is:
// i) disallow any new events into the queue
// ii) empty the queue
//
// That will only work if the event submitter handles EventBusException correctly when posting.
//
public void stop() {
}
}
@Inject
public InMemoryPersistentBus(final PersistentBusConfig config) {
final ThreadGroup group = new ThreadGroup(EVENT_BUS_GROUP_NAME);
final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
return new Thread(group, r, config.getTableName() + "-th");
}
});
this.delegate = new EventBusDelegate();
this.isInitialized = new AtomicBoolean(false);
}
@Override
public void register(final Object handlerInstance) throws EventBusException {
checkInitialized("register");
delegate.register(handlerInstance);
}
@Override
public void unregister(final Object handlerInstance) throws EventBusException {
checkInitialized("unregister");
delegate.unregister(handlerInstance);
}
@Override
public void post(final BusEvent event) throws EventBusException {
checkInitialized("post");
delegate.post(event);
}
@Override
public void postFromTransaction(final BusEvent event, final Transmogrifier transmogrifier) throws EventBusException {
checkInitialized("postFromTransaction");
delegate.post(event);
}
@Override
public void start() {
if (isInitialized.compareAndSet(false, true)) {
log.info("InMemoryPersistentBus started...");
}
}
private void checkInitialized(final String operation) throws EventBusException {
if (!isInitialized.get()) {
throw new EventBusException(String.format("Attempting operation %s on an non initialized bus",
operation));
}
}
@Override
public void stop() {
if (isInitialized.compareAndSet(true, false)) {
log.info("InMemoryPersistentBus stopping...");
delegate.completeDispatch();
delegate.stop();
log.info("InMemoryPersistentBus stopped...");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy