// Each processor in a route will have its own performance counter
// The performance counter are MBeans that we register with MBeanServer.
// These performance counter will be embedded
// to InstrumentationProcessor and wrap the appropriate processor
// by InstrumentationInterceptStrategy.
RouteType route = routeContext.getRoute();
for (ProcessorType processor : route.getOutputs()) {
ObjectName name = null;
try {
// get the mbean name
name = getNamingStrategy().getObjectName(routeContext, processor);
// register mbean wrapped in the performance counter mbean
PerformanceCounter pc = new PerformanceCounter();
agent.register(pc, name);
// add to map now that it has been registered
counterMap.put(processor, pc);
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create MBean name: " + name, e);
} catch (JMException e) {
LOG.warn("Could not register PerformanceCounter MBean: " + name, e);
}
}
// TODO: align this code with InstrumentationLifecycleStrategy
routeContext.addInterceptStrategy(new InstrumentationInterceptStrategy(counterMap));
routeContext.setErrorHandlerWrappingStrategy(
new InstrumentationErrorHandlerWrappingStrategy(counterMap));
// Add an InstrumentationProcessor at the beginning of each route and
// set up the interceptorMap for onRoutesAdd() method to register the
// ManagedRoute MBeans.
RouteType routeType = routeContext.getRoute();
if (routeType.getInputs() != null && !routeType.getInputs().isEmpty()) {
if (routeType.getInputs().size() > 1) {
LOG.warn("Addding InstrumentationProcessor to first input only.");
}
Endpoint endpoint = routeType.getInputs().get(0).getEndpoint();
List<ProcessorType<?>> exceptionHandlers = new ArrayList<ProcessorType<?>>();
List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
// separate out the exception handers in the outputs
for (ProcessorType output : routeType.getOutputs()) {
if (output instanceof ExceptionType) {
exceptionHandlers.add(output);
} else {
outputs.add(output);
}
}
// clearing the outputs
routeType.clearOutput();
// add exception handlers as top children
routeType.getOutputs().addAll(exceptionHandlers);
// add an interceptor
InstrumentationProcessor processor = new InstrumentationProcessor();
routeType.intercept(processor);
// add the output
for (ProcessorType<?> processorType : outputs) {
routeType.addOutput(processorType);
}
interceptorMap.put(endpoint, processor);
}