-
Notifications
You must be signed in to change notification settings - Fork 1
Handling around syncExec with Interceptors
There might be various reasons why you would like to be able to inject some code before and after syncExec processing which is used to run some more or less individual operations in UI thread. RedDeer uses its own syncExec() implementation wrapping SWT Display.syncExec(). Now interceptors are introduced so everyone can implement own custom interceptor around UI actions that are called synchronously.
Possible usage might be:
- bug diagnostic
- performance diagnostic
- additional logging
- etc.
Implementing own interceptor is easy as implementing single interface ISyncInterceptor in this case. Let's implement simple interceptor for performance and ui responsiveness diagnostic.
class MyInterceptor implements ISyncInterceptor {
long start = 0;
long opTime = 0;
@Override
public void beforeSyncOp() {
begin = System.currentTimeMillis();
}
@Override
public void afterSyncOp() {
opTime = System.currentTimeMillis() - start;
log.debug("Operation took " + end - begin + "ms");
Math.max(maxOpTime,opTime); // maxTime can be somewhere in testClass
}
}Code in beforeSyncOp is by default in you curren (working) thread. But you can call also code in UI thread by calling Display.syncExec(). You don't have to be worry about infinite loop there as interceptors are not called from interceptors itself. If you need to analyze your interceptor code by another interceptor you just need to execute it outside interceptor and use proper interceptor to analyze it or multiple of them.
Once you have implemented your interceptor you just need to register it by SyncInterceptorManager()
SyncInterceptorManager sim = SyncInterceptorManager.getInstance();
sim.register("myInterceptor", new MyInterceptor());That's it.
Anytime you want you can unregistr it by calling unregister method();
sim.unregister("myInterceptor");You can also register multiple interceptors if you want by calling register method multiple times
sim.register("myInterceptor1", new MyInterceptor1());
sim.register("myInterceptor2", new MyInterceptor2());You can also unregister all registered interceptors (even those you didn't register) by calling unregisterAll() method
sim.unregisterAll();
You might want to handle your interceptor from commandline for purpose of configuration or disabling/enabling from outer environment. In that case I suggest to implement your own property that can be read by return System.getProperty();
boolean enabled = System.getProperty("myInterceptorEnabled", true);
if (enabled) sim.register("myInterceptor", new MyInterceptor());
JBoss Red Deer - Quick Links
- Home
- [Release notes] (/jboss-reddeer/reddeer/wiki/Release-notes)
- Articles
- JavaDoc
- User guide
- Contributor Guide
- FAQ