Search in sources :

Example 26 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class JunctionTestCase method multiThreadedTest1.

@Test
public void multiThreadedTest1() throws InterruptedException {
    log.info("multi threaded 1");
    StreamDefinition streamA = StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionA = new StreamJunction(streamA, executorService, 1024, null, siddhiAppContext);
    StreamJunction.Publisher streamPublisherA = streamJunctionA.constructPublisher();
    StreamDefinition streamB = StreamDefinition.id("streamB").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionB = new StreamJunction(streamB, executorService, 1024, null, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB1 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB2 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB3 = streamJunctionB.constructPublisher();
    StreamCallback streamCallbackA1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB2.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA3 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB3.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                count++;
                eventArrived = true;
                AssertJUnit.assertTrue(streamEvent.getData()[0].equals("IBM") || (streamEvent.getData()[0].equals("WSO2")));
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA1);
    streamJunctionA.subscribe(streamCallbackA2);
    streamJunctionA.subscribe(streamCallbackA3);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB);
    streamJunctionB.startProcessing();
    StreamEvent streamEvent1 = new StreamEvent(2, 2, 2);
    streamEvent1.setTimestamp(System.currentTimeMillis());
    streamEvent1.setOutputData(new Object[] { "IBM", 12 });
    StreamEvent streamEvent2 = new StreamEvent(2, 2, 2);
    streamEvent2.setTimestamp(System.currentTimeMillis());
    streamEvent2.setOutputData(new Object[] { "WSO2", 112 });
    streamPublisherA.send(streamEvent1);
    streamPublisherA.send(streamEvent2);
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(6, count);
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Event(io.siddhi.core.event.Event) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 27 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class CustomJoinWindowTestCase method testGroupByUseCase.

@Test
public void testGroupByUseCase() throws InterruptedException {
    log.info("Test joining a single window by multiple streams");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream SensorStream (name string, value float, roomNo int, deviceID string); " + "define window SensorWindow (name string, value float, roomNo int, deviceID string) timeBatch(1 " + "second); ";
    String query = "" + "@info(name = 'query0') " + "from SensorStream " + "insert into SensorWindow; " + "@info(name = 'query1') " + "from SensorWindow  " + "select name, max(value) as maxValue, roomNo " + "group by name, roomNo " + "insert into MaxSensorReadingPerRoomStream; " + "@info(name = 'query2') " + "from SensorWindow  " + "select name, max(value) as maxValue " + "group by name " + "insert into OverallMaxSensorReadingStream; " + "@info(name = 'query3') " + "from SensorWindow  " + "select name, avg(value) as avgValue " + "group by name " + "insert into OverallAverageSensorReadingStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("MaxSensorReadingPerRoomStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("MaxSensorReadingPerRoomStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.addCallback("OverallMaxSensorReadingStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("OverallMaxSensorReadingStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.addCallback("OverallAverageSensorReadingStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("OverallAverageSensorReadingStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.start();
    InputHandler sensorStreamInputHandler = siddhiAppRuntime.getInputHandler("SensorStream");
    sensorStreamInputHandler.send(new Object[] { "Temperature", 23.0f, 1, "T001A" });
    sensorStreamInputHandler.send(new Object[] { "Pressure", 20.0f, 1, "P001" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 25.0f, 2, "T002" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 24.0f, 3, "T003" });
    sensorStreamInputHandler.send(new Object[] { "Pressure", 45.0f, 3, "P003" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 23.5f, 1, "T001B" });
    sensorStreamInputHandler.send(new Object[] { "Humidity", 15.0f, 2, "H002" });
    sensorStreamInputHandler.send(new Object[] { "Humidity", 10.0f, 3, "H003" });
    Thread.sleep(1000);
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 28 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow15.

@Test
public void testExternalTimeBatchWindow15() throws Exception {
    log.info("ExternalTimeBatchWindow test15");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stream = "define stream jmxMetric(cpu int, memory int, bytesIn long, bytesOut long, timestamp long); " + "define window jmxMetricWindow(cpu int, memory int, bytesIn long, bytesOut long, timestamp long) " + "externalTimeBatch(timestamp, 10 sec) output current events; ";
    String query = "" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name = 'downSample') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, max(cpu) as maxCpu, min(cpu) as minCpu, " + " '|' as s, " + " avg(memory) as avgMem, max(memory) as maxMem, min(memory) as minMem, " + " '|' as s1, " + " avg(bytesIn) as avgBytesIn, max(bytesIn) as maxBytesIn, min(bytesIn) as minBytesIn, " + " '|' as s2, " + " avg(bytesOut) as avgBytesOut, max(bytesOut) as maxBytesOut, min(bytesOut) as minBytesOut, " + " '|' as s3, " + " timestamp as timeWindowEnds, " + " '|' as s4, " + " count() as metric_count " + " INSERT INTO tmp;";
    SiddhiManager sm = new SiddhiManager();
    SiddhiAppRuntime plan = sm.createSiddhiAppRuntime(stream + query);
    InputHandler input = plan.getInputHandler("jmxMetric");
    // stream call back doesn't follow the counter
    final AtomicInteger counter = new AtomicInteger();
    {
        // stream callback
        plan.addCallback("jmxMetric", new StreamCallback() {

            @Override
            public void receive(Event[] arg0) {
                counter.addAndGet(arg0.length);
            }
        });
    }
    final AtomicInteger queryWideCounter = new AtomicInteger();
    {
        plan.addCallback("downSample", new QueryCallback() {

            @Override
            public void receive(long timeStamp, Event[] inevents, Event[] removevents) {
                int currentCount = queryWideCounter.addAndGet(inevents.length);
                log.info(MessageFormat.format("Round {0} ====", currentCount));
                log.info(" events count " + inevents.length);
                EventPrinter.print(inevents);
            }
        });
    }
    plan.start();
    int round = 4;
    int eventsPerRound = 0;
    long externalTs = System.currentTimeMillis();
    for (int i = 0; i < round; i++) {
        eventsPerRound = sendEvent(input, i, externalTs);
        Thread.sleep(3000);
    }
    // trigger next round
    sendEvent(input, round, externalTs);
    plan.shutdown();
    Thread.sleep(1000);
    assertEquals(round * eventsPerRound + eventsPerRound, counter.get());
    assertEquals(round, queryWideCounter.get());
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 29 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class LengthBatchWindowTestCase method testLengthBatchWindow3.

@Test
public void testLengthBatchWindow3() throws InterruptedException {
    log.info("Testing length batch window with no of events greater than window size");
    final int length = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(" + length + ") output " + "all events; ";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if ((count / length) % 2 == 1) {
                    removeEventCount++;
                    AssertJUnit.assertEquals("Remove event order", removeEventCount, event.getData(2));
                    if (removeEventCount == 1) {
                        AssertJUnit.assertEquals("Expired event triggering position", length, inEventCount);
                    }
                } else {
                    inEventCount++;
                    AssertJUnit.assertEquals("In event order", inEventCount, event.getData(2));
                }
                count++;
            }
            AssertJUnit.assertEquals("No of emitted events at window expiration", inEventCount - length, removeEventCount);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(500);
    AssertJUnit.assertEquals("In event count", 6, inEventCount);
    AssertJUnit.assertEquals("Remove event count", 4, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 30 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class LengthBatchWindowTestCase method testLengthBatchWindow4.

@Test
public void testLengthBatchWindow4() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(4); ";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow " + "select symbol,sum(price) as sumPrice,volume " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                AssertJUnit.assertEquals("Events cannot be expired", false, event.isExpired());
                inEventCount++;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(100.0, event.getData(1));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 10f, 0 });
    inputHandler.send(new Object[] { "WSO2", 20f, 1 });
    inputHandler.send(new Object[] { "IBM", 30f, 0 });
    inputHandler.send(new Object[] { "WSO2", 40f, 1 });
    inputHandler.send(new Object[] { "IBM", 50f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60f, 1 });
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(1, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (io.siddhi.core.stream.output.StreamCallback)311 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)305 SiddhiManager (io.siddhi.core.SiddhiManager)305 Event (io.siddhi.core.event.Event)303 Test (org.testng.annotations.Test)295 InputHandler (io.siddhi.core.stream.input.InputHandler)289 StreamEvent (io.siddhi.core.event.stream.StreamEvent)16 ComplexEvent (io.siddhi.core.event.ComplexEvent)13 UnitTestAppender (io.siddhi.core.UnitTestAppender)12 Logger (org.apache.logging.log4j.core.Logger)12 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)10 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 SiddhiApp (io.siddhi.query.api.SiddhiApp)9 Partition (io.siddhi.query.api.execution.partition.Partition)8 Query (io.siddhi.query.api.execution.query.Query)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintStream (java.io.PrintStream)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)7 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)7