Skip to content

Instantly share code, notes, and snippets.

@tallpsmith
Created August 17, 2011 05:06
Show Gist options
  • Save tallpsmith/1150846 to your computer and use it in GitHub Desktop.
Save tallpsmith/1150846 to your computer and use it in GitHub Desktop.
reason for needing bindInterceptor
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("create", Engine.Create.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("index", Engine.Index.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("flush", Engine.Flush.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("delete", Engine.Delete.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("delete", Engine.DeleteByQuery.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("optimize", Engine.Optimize.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("refresh", Engine.Refresh.class));
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("snapshot", Engine.SnapshotHandler.class));...
bindEngineMethodToCounterAndAction(parfaitService, RobinEngine.class.getMethod("create", Engine.Create.class));
...
private void bindClassMethodToCounterAndAction(ParfaitService parfaitService, Class<?> clazz, Matcher<Object> methodMatcher, String eventGroup, String action) {
bindInterceptor(Matchers.subclassesOf(clazz), methodMatcher, newProfiledShardBasedMethodCounter(parfaitService, eventGroup, action));
}
private ProfiledShardBasedMethodCounter newProfiledShardBasedMethodCounter(ParfaitService parfaitService, String eventGroup, String action) {
return new ProfiledShardBasedMethodCounter(parfaitService, eventGroup, action);
}
.....
class ProfiledShardBasedMethodCounter implements MethodInterceptor {
private final String eventGroup;
private final EventMetricCollector collector;
private final String action;
private final ConcurrentMap<ShardId, MonitoredCounter> counterMap;
ProfiledShardBasedMethodCounter(final ParfaitService parfaitService, String eventGroup, final String action) {
this.eventGroup = eventGroup;
this.action = action;
this.collector = parfaitService.getEventTimerForGroup(eventGroup).getCollector();
this.counterMap = new MapMaker().makeComputingMap(new Function<ShardId, MonitoredCounter>() {
@Override public MonitoredCounter apply(ShardId from) {
return parfaitService.forShard(from).count(action);
}
});
}
@Override public Object invoke(MethodInvocation methodInvocation) throws Throwable {
collector.startTiming(eventGroup, action);
try {
Engine engine = (Engine)methodInvocation.getThis();
counterMap.get(engine.shardId()).inc();
return methodInvocation.proceed();
} finally {
collector.stopTiming();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment