com.hmsonline.storm.cassandra.exceptions.MappedExceptionHandler Maven / Gradle / Ivy
package com.hmsonline.storm.cassandra.exceptions;
import backtype.storm.topology.FailedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import storm.trident.operation.TridentCollector;
import java.util.HashSet;
public class MappedExceptionHandler implements ExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(MappedExceptionHandler.class);
public static enum Action{
FAIL_TUPLE, REPORT_ERROR, KILL_WORKER, LOG
}
private HashSet> failTupleExceptions = new HashSet>();
private HashSet> reportErrorExceptions = new HashSet>();
private HashSet> killWorkerExceptions = new HashSet>();
private Action defaultAction = Action.LOG;
protected final void setDefaultAction(Action action){
this.defaultAction = action;
}
protected final void addFailOn(Class extends Exception>... exceptions) {
for (Class extends Exception> e : exceptions) {
if (reportErrorExceptions.contains(e) || killWorkerExceptions.contains(e)) {
throw new IllegalStateException("Exception " + e.getName() + " has already been registered.");
}
this.failTupleExceptions.add(e);
}
}
protected final void addReportOn(Class extends Exception>... exceptions) {
for (Class extends Exception> e : exceptions) {
if (failTupleExceptions.contains(e) || killWorkerExceptions.contains(e)) {
throw new IllegalStateException("Exception " + e.getName() + " has already been registered.");
}
this.reportErrorExceptions.add(e);
}
}
protected final void addDieOn(Class extends Exception>... exceptions) {
for (Class extends Exception> e : exceptions) {
if (failTupleExceptions.contains(e) || reportErrorExceptions.contains(e)) {
throw new IllegalStateException("Exception " + e.getName() + " has already been registered.");
}
this.killWorkerExceptions.add(e);
}
}
protected final Action actionFor(Exception e){
Class extends Exception> c = (Class extends Exception>)e.getClass();
if(this.failTupleExceptions.contains(c)){
return Action.FAIL_TUPLE;
} else if(this.killWorkerExceptions.contains(c)){
return Action.KILL_WORKER;
} else if(this.reportErrorExceptions.contains(c)){
return Action.REPORT_ERROR;
} else{
return this.defaultAction;
}
}
public MappedExceptionHandler failOn(Class extends Exception>... exceptions) {
addFailOn(exceptions);
return this;
}
public MappedExceptionHandler dieOn(Class extends Exception>... exceptions) {
addDieOn(exceptions);
return this;
}
public MappedExceptionHandler reportOn(Class extends Exception>... exceptions) {
addReportOn(exceptions);
return this;
}
public MappedExceptionHandler withDefaultAction(Action action) {
setDefaultAction(action);
return this;
}
@Override
public void onException(Exception e, TridentCollector collector) {
switch (actionFor(e)) {
case FAIL_TUPLE:
LOG.info("Failing batch based on registered exception " + e.getClass().getName() + ", Message: '"
+ e.getMessage() + "'", e);
throw new FailedException(e);
case REPORT_ERROR:
LOG.info(
"Reporting error based on registered exception " + e.getClass().getName() + ", Message: '"
+ e.getMessage() + "'", e);
if(collector != null){
collector.reportError(e);
}
break;
case KILL_WORKER:
LOG.warn("Killing worker/executor based on registered exception " + e.getClass().getName()
+ ", Message: '" + e.getMessage() + "'", e);
// wrap in RuntimeException in case FailedException has been
// explicitly registered.
throw new RuntimeException(e);
default:
LOG.warn("Inoring exception " + e.getClass().getName() + " and acknowledging batch. Message: '"
+ e.getMessage() + "'", e);
break;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy