This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
/
CounterApp.java
99 lines (75 loc) · 2.79 KB
/
CounterApp.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.apache.s4.example.edsl.counter;
import java.util.concurrent.TimeUnit;
import org.apache.s4.base.Event;
import org.apache.s4.edsl.BuilderS4DSL;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* This is a sample application to test the S4 embedded domain-specific language (EDSL).
*
* <p>
* Grammar:
*
* <pre>
* (pe , type , prop* , (fireOn , afterInterval? , afterNumEvents?)? , (timer, withPeriod)? ,
* (cache, size , expires? )? , asSingleton? , (emit, onField?,
* (withKey|withKeyFinder)?, to )* )+ , build
* </pre>
*
* <p>
* See the <a href="http://code.google.com/p/diezel">Diezel</a> project for details.
*
*/
final public class CounterApp extends BuilderS4DSL {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new Module());
CounterApp myApp = injector.getInstance(CounterApp.class);
/* Normally. the container will handle this but this is just a test. */
myApp.init();
myApp.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myApp.close();
}
@Override
protected void onInit() {
pe("Print").type(PrintPE.class).asSingleton().
pe("User Count").type(CounterPE.class).fireOn(Event.class).afterInterval(100, TimeUnit.MILLISECONDS)
.emit(CountEvent.class).withKeyFinder(CountKeyFinder.class).to("Print").
pe("Gender Count").type(CounterPE.class).fireOn(Event.class).afterInterval(100, TimeUnit.MILLISECONDS)
.emit(CountEvent.class).withKeyFinder(CountKeyFinder.class).to("Print").
pe("Age Count").type(CounterPE.class).fireOn(Event.class).afterInterval(100, TimeUnit.MILLISECONDS)
.emit(CountEvent.class).withKeyFinder(CountKeyFinder.class).to("Print").
pe("Generate User Event").type(GenerateUserEventPE.class).timer().withPeriod(1, TimeUnit.MILLISECONDS)
.asSingleton().
emit(UserEvent.class).withKeyFinder(UserIDKeyFinder.class).to("User Count").
emit(UserEvent.class).withKey("gender").to("Gender Count").
emit(UserEvent.class).withKeyFinder(AgeKeyFinder.class).to("Age Count").
build();
}
/*
* Create and send 200 dummy events of type UserEvent.
*
* @see io.s4.App#start()
*/
@Override
protected void onStart() {
}
@Override
protected void onClose() {
System.out.println("Bye.");
}
// Make hooks public for testing. Normally this is handled by the container.
public void init() {
super.init();
}
public void start() {
super.start();
}
public void close() {
super.close();
}
}