My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.javasimon.examples;

import org.javasimon.*;
import org.javasimon.callback.Callback;
import org.javasimon.callback.CallbackSkeleton;
import org.javasimon.callback.CompositeFilterCallback;
import org.javasimon.callback.FilterRule;
import org.javasimon.utils.SimonUtils;

/**
* CallbackFilteringExample shows how filter works and how it can be set up programmaticaly.
*
* @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a>
*/
public final class CallbackFilteringExample {
/**
* Entry point to the Callback Filtering Example.
*
* @param args unused
*/
public static void main(String[] args) {
// manager with two stopwatches is created
Manager manager = new EnabledManager();
Stopwatch sw1 = manager.getStopwatch("org.javasimon.examples.stopwatch1");
Stopwatch sw2 = manager.getStopwatch("other.stopwatch2");

// simple callback printing actions to the stdout is created and installed
Callback stdoutCallback = new CallbackSkeleton() {
public void onStopwatchStart(Split split) {
System.out.println("Starting " + split.getStopwatch().getName());
}

public void onStopwatchStop(Split split, StopwatchSample sample) {
System.out.println("Stopped " + split.getStopwatch().getName() + " (" + SimonUtils.presentNanoTime(split.runningFor()) + ")");
}
};
manager.callback().addCallback(stdoutCallback);

// prints start/stop for both stopwatches
sw1.start().stop();
sw2.start().stop();
System.out.println();

// we need to remove old callback
manager.callback().removeCallback(stdoutCallback);
// alternatively you can call this if you want to remove all callbacks
SimonManager.callback().removeAllCallbacks();

// filter callback is created
CompositeFilterCallback filter = new CompositeFilterCallback();
// rule to filter out all Simons matching pattern "other.*" is added
filter.addRule(FilterRule.Type.MUST_NOT, null, "other.*");
// original callback is added after this callback
filter.addCallback(stdoutCallback);

// filter callback is installed to the manager (with printing callback behind)
manager.callback().addCallback(filter);

// start/stop is printed only for sw1 because sw2 matches other.* pattern that is excluded (MUST_NOT)
sw1.start().stop();
sw2.start().stop();
}
}

Change log

r524 by virgo47 on Apr 11, 2012   Diff
removed sample from onStopwatchStart
(waste of resources, so I decided :-)),
fixed missing enabled testing in
CounterImpl + sample is created for
callbacks only when there is at least one
in the manager
Go to: 
Project members, sign in to write a code review

Older revisions

r513 by virgo47 on Apr 6, 2012   Diff
added sample to onStopwatchStart as
well, revised Callback impls to
reflect this; simon patterns on
Manager list methods must be provided
as SimonPattern, not string (this
...
r512 by virgo47 on Apr 5, 2012   Diff
stopwatch/counter callback methods are
called OUT of synchronized blocks and
samples are provided where appropriate
(callback implementations don't use
the samples yet, I'll change this
...
r420 by virgo47 on Jan 11, 2012   Diff
fix http://code.google.com/p/javasimon
/issues/detail?id=65 + related javadoc
fixes
All revisions of this file

File info

Size: 2297 bytes, 63 lines
Powered by Google Project Hosting