com.netflix.eventbus.spi.InvalidSubscriberException Maven / Gradle / Ivy
package com.netflix.eventbus.spi;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
/**
* Thrown if the subscriber registered with the {@link EventBus} is invalid. A subscriber will be
* invalid if
*
- The method annotated with {@link Subscribe} does not contain one and only one argument.
- The method uses a batching strategy other than {@link com.netflix.eventbus.spi.Subscribe.BatchingStrategy#None} and
does not have the argument as {@link Iterable}
- If the subscriber uses batching and does not provide a batch size > 1
- If the subscriber uses batching and does not provide a batch age.
*
* @author Nitesh Kant ([email protected])
*/
public class InvalidSubscriberException extends Exception {
private static final long serialVersionUID = 4258884942423525335L;
private Class> subscriberClass;
private Set offendingMethods;
public InvalidSubscriberException(Class> subscriberClass, Map errors) {
super(getErrorMessage(subscriberClass, errors));
this.subscriberClass = subscriberClass;
this.offendingMethods = errors.keySet();
}
public Set getOffendingMethods() {
return offendingMethods;
}
public Class> getSubscriberClass() {
return subscriberClass;
}
private static String getErrorMessage(Class> subscriberClass, Map errors) {
StringBuilder errMsgBuilder = new StringBuilder();
errMsgBuilder.append("Invalid subscriber class: ");
errMsgBuilder.append(subscriberClass);
errMsgBuilder.append(". Errors: \n");
for (Map.Entry anEntry : errors.entrySet()) {
errMsgBuilder.append("Method: ");
errMsgBuilder.append(anEntry.getKey().toGenericString());
errMsgBuilder.append(" is invalid. Error: ");
errMsgBuilder.append(anEntry.getValue());
errMsgBuilder.append("\n");
}
return errMsgBuilder.toString();
}
}