Skip to content

Commit 2de5e68

Browse files
committed
Siddhi 4.0.0 early access
1 parent 48c076b commit 2de5e68

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

  • wso2/cep/siddhi/migrationguide/3-4/siddhimigrationdemo
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.javahelps</groupId>
8+
<artifactId>siddhimigrationdemo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<siddhi.version>4.0.0-SNAPSHOT</siddhi.version>
13+
<siddhi.execution.unique.version>3.1.3-SNAPSHOT</siddhi.execution.unique.version>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
<!--Siddhi -->
20+
<dependency>
21+
<groupId>org.wso2.siddhi</groupId>
22+
<artifactId>siddhi-query-api</artifactId>
23+
<version>${siddhi.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.wso2.siddhi</groupId>
27+
<artifactId>siddhi-core</artifactId>
28+
<version>${siddhi.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.wso2.siddhi</groupId>
32+
<artifactId>siddhi-query-compiler</artifactId>
33+
<version>${siddhi.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.wso2.extension.siddhi.execution.unique</groupId>
37+
<artifactId>siddhi-execution-unique</artifactId>
38+
<version>${siddhi.execution.unique.version}</version>
39+
</dependency>
40+
</dependencies>
41+
42+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.javahelps.siddhimigrationdemo;
2+
3+
import java.util.Random;
4+
5+
import org.wso2.siddhi.core.ExecutionPlanRuntime;
6+
import org.wso2.siddhi.core.SiddhiManager;
7+
import org.wso2.siddhi.core.event.Event;
8+
import org.wso2.siddhi.core.stream.input.InputHandler;
9+
import org.wso2.siddhi.core.stream.output.StreamCallback;
10+
11+
public class Main {
12+
// SiddhiDe
13+
private static final String[] CUSTOMERS = {"Alice", "Barby", "Carol", "Diana"};
14+
private static final String[] ITEMS = {"Cocoa-Butter Lotion", "Purse-XL", "Purse-L", "Beer", "Biscuit",
15+
"Chocolate", "ZMA"};
16+
private static final Random RANDOM = new Random();
17+
18+
public static void main(String[] args) throws InterruptedException {
19+
SiddhiManager siddhiManager = new SiddhiManager();
20+
21+
String streams = "define stream purchaseStream (customerName string, item string, timestamp long); ";
22+
String query = "partition with (customerName of purchaseStream) " +
23+
"begin " +
24+
"from purchaseStream[item == 'Cocoa-Butter Lotion' OR item == 'Purse-XL' OR item == 'ZMA']#window" +
25+
".unique:externalTimeBatch(item, timestamp, 500 milliseconds) " +
26+
"select customerName, convert(count(item), 'double') / 3.0 * 100.0 as noOfPurchases insert into " +
27+
"possiblePregnant; " +
28+
"end ";
29+
30+
// Create ExecutionPlanRuntime using stream definition and query
31+
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(streams + query);
32+
33+
try {
34+
// Receive the output events
35+
executionPlanRuntime.addCallback("possiblePregnant", new StreamCallback() {
36+
@Override
37+
public void receive(Event[] events) {
38+
String output = String.format("\t\t\t%s is pregnant with %.2f%% confidence.", events[0].getData(0),
39+
events[0].getData(1));
40+
System.out.println(output);
41+
}
42+
});
43+
44+
// Send input events
45+
InputHandler purchaseStream = executionPlanRuntime.getInputHandler("purchaseStream");
46+
executionPlanRuntime.start();
47+
for (int i = 0; i < 1000; i++) {
48+
Object[] event = generateEvent();
49+
purchaseStream.send(event);
50+
Thread.sleep(10); // Delay for 10 milliseconds
51+
}
52+
} finally {
53+
executionPlanRuntime.shutdown();
54+
}
55+
}
56+
57+
private static Object[] generateEvent() {
58+
String name = CUSTOMERS[RANDOM.nextInt(CUSTOMERS.length)];
59+
String item = ITEMS[RANDOM.nextInt(ITEMS.length)];
60+
long time = System.currentTimeMillis(); // Current time
61+
62+
System.out.println(name + " buys " + item);
63+
Object[] event = new Object[]{name, item, time};
64+
return event;
65+
}
66+
67+
}

0 commit comments

Comments
 (0)