使用Apache Camel时,我想在路由的每个处理器/组件之前自动保留交换信息,而不在路由中使用窃听处理器,其中 errorHandlers 也应用于路由。我尝试使用interceptStrategy,但是在路由中应用errorHandlers时不会触发拦截器。我还了解到,使用自定义 InterceptStrategy 并不适合最终用户。有人知道这个的替代方案吗?
我们目前正在研究EventNotifierSupport
@RomanVottner 提到的实施。通过下面的实现,我们注意到ExchangeSentEvent
某些处理器/端点的发送是重复的。
为什么 ExchangeSentEvent 会针对同一个处理器操作多次发送?
context.getManagementStrategy().addEventNotifier(new RouteHistoryEventNotifier());
public class RouteHistoryEventNotifier extends EventNotifierSupport {
private int retryCounter = 0;
private String lastID = "";
@Override
public void notify(EventObject eventObject) throws Exception {
long startTime = System.currentTimeMillis();
log.info("notify eventname: " + eventObject.getClass().getName() );
if (eventObject instanceof ExchangeRedeliveryEvent) {
ExchangeRedeliveryEvent event = (ExchangeRedeliveryEvent) eventObject;
Exchange exchange = event.getExchange();
String id = getProcessorName(exchange);
retryCounter = event.getAttempt();
log.info("notify eventname: " + event.getClass().getName() + " - retry: " + retryCounter );
}
if (eventObject instanceof ExchangeSentEvent) {
ExchangeSentEvent event = (ExchangeSentEvent) eventObject;
Exchange exchange = event.getExchange();
String id = getProcessorName(exchange);
if(retryCounter > 0) {
id = String.format("%s (%s)", id, retryCounter );
}
// write Exhange with timeTaken and processorId to Database
myRepo.saveStep(id , exchange,startTime, event.getTimeTaken());
}
}
private String getProcessorName(Exchange exchange) {
List<MessageHistory> list = exchange.getProperty(Exchange.MESSAGE_HISTORY, List.class);
MessageHistory msgHistory = list.get(list.size() - 1);
String id = msgHistory.getNode().getId();
if (!lastID.equalsIgnoreCase(id) ) {
lastID = id;
retryCounter = 0;
}
return id;
}
@Override
public boolean isEnabled(EventObject event) {
return true;
}
}
控制台输出:
Line 4762: 2018-06-26 14:58:44.503 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.CamelContextStartingEvent
Line 4950: 2018-06-26 14:58:44.607 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteAddedEvent
Line 4978: 2018-06-26 14:58:44.617 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteAddedEvent
Line 4996: 2018-06-26 14:58:44.619 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteStartedEvent
Line 5002: 2018-06-26 14:58:44.619 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteStartedEvent
Line 5005: 2018-06-26 14:58:44.619 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.CamelContextStartedEvent
Line 5010: 2018-06-26 14:58:44.620 [d763f7b:1624c6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSendingEvent
Line 5011: 2018-06-26 14:58:44.620 [d763f7b:1624c6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeCreatedEvent
Line 5014: 2018-06-26 14:58:44.620 [d763f7b:513548] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSendingEvent
Line 5018: 2018-06-26 14:58:44.621 [d763f7b:61dde6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSentEvent
Line 5020: 2018-06-26 14:58:44.622 [d763f7b:783f41] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSendingEvent
Line 5024: 2018-06-26 14:58:44.625 [d763f7b:61dde6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSentEvent
Line 5026: 2018-06-26 14:58:44.626 [d763f7b:9d0511] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSendingEvent
Line 5030: 2018-06-26 14:58:44.626 [d763f7b:4be680] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSendingEvent
Line 5118: 2018-06-26 14:58:45.353 [d763f7b:acf181] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSentEvent
Line 5123: 2018-06-26 14:58:45.353 [d763f7b:61dde6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSentEvent
Line 5126: 2018-06-26 14:58:45.354 [d763f7b:ac8552] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSendingEvent
Line 5130: 2018-06-26 14:58:45.360 [d763f7b:61dde6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSentEvent
Line 5133: 2018-06-26 14:58:45.360 [d763f7b:1624c6] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeCompletedEvent
Line 5136: 2018-06-26 14:58:45.360 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.ExchangeSentEvent
Line 5138: 2018-06-26 14:58:45.360 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.CamelContextStoppingEvent
Line 5183: 2018-06-26 14:58:45.364 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteStoppedEvent
Line 5185: 2018-06-26 14:58:45.364 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteRemovedEvent
Line 5262: 2018-06-26 14:58:45.365 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteStoppedEvent
Line 5286: 2018-06-26 14:58:45.366 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.RouteRemovedEvent
Line 5301: 2018-06-26 14:58:45.367 [d763f7b:d7637b] [ntExecutorGroup] INFO fiers.RouteHistoryEventNotifier - notify eventname: org.apache.camel.management.event.CamelContextStoppedEvent
您还可以查看OpenTracingEventNotifier或ZipkinEventNotifier作为示例