com.lmax.disruptor.biz.event.handler.AbstractRouteableEventHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of disruptor-biz Show documentation
Show all versions of disruptor-biz Show documentation
Disruptor Event Chain Integration
The newest version!
/*
* Copyright (c) 2018 (https://github.com/hiwepy).
*
* Licensed 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 com.lmax.disruptor.biz.event.handler;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lmax.disruptor.biz.event.DisruptorEvent;
import com.lmax.disruptor.biz.event.handler.chain.HandlerChain;
import com.lmax.disruptor.biz.event.handler.chain.HandlerChainResolver;
import com.lmax.disruptor.biz.exception.EventHandleException;
public class AbstractRouteableEventHandler extends AbstractEnabledEventHandler {
private static final Logger LOG = LoggerFactory.getLogger(AbstractRouteableEventHandler.class);
/*
* 用来判定使用那个HandlerChian
*/
protected HandlerChainResolver handlerChainResolver;
public AbstractRouteableEventHandler() {
super();
}
public AbstractRouteableEventHandler(HandlerChainResolver handlerChainResolver) {
super();
this.handlerChainResolver = handlerChainResolver;
}
@Override
protected void doHandlerInternal(T event, HandlerChain handlerChain) throws Exception {
Throwable t = null;
try {
this.executeChain(event, handlerChain);
} catch (Throwable throwable) {
t = throwable;
}
if (t != null) {
if (t instanceof IOException) {
throw (IOException) t;
}
String msg = "Handlered event failed.";
throw new EventHandleException(msg, t);
}
}
protected HandlerChain getExecutionChain(T event, HandlerChain origChain) {
HandlerChain chain = origChain;
HandlerChainResolver resolver = getHandlerChainResolver();
if (resolver == null) {
LOG.debug("No HandlerChainResolver configured. Returning original HandlerChain.");
return origChain;
}
HandlerChain resolved = resolver.getChain(event, origChain);
if (resolved != null) {
LOG.trace("Resolved a configured HandlerChain for the current event.");
chain = resolved;
} else {
LOG.trace("No HandlerChain configured for the current event. Using the default.");
}
return chain;
}
protected void executeChain(T event, HandlerChain origChain) throws Exception {
HandlerChain chain = getExecutionChain(event, origChain);
chain.doHandler(event);
}
public HandlerChainResolver getHandlerChainResolver() {
return handlerChainResolver;
}
public void setHandlerChainResolver(HandlerChainResolver handlerChainResolver) {
this.handlerChainResolver = handlerChainResolver;
}
}