Search in sources :

Example 21 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class JunctionTestCase method junctionToReceiverTest.

@Test
public void junctionToReceiverTest() throws InterruptedException {
    log.info("junction to receiver");
    StreamDefinition streamA = StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
    StreamJunction streamJunctionA = new StreamJunction(streamA, executorService, 1024, siddhiAppContext);
    StreamJunction.Publisher streamPublisherA = streamJunctionA.constructPublisher();
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            count += streamEvents.length;
            eventArrived = true;
        }
    };
    streamJunctionA.subscribe(streamCallback);
    streamJunctionA.startProcessing();
    streamPublisherA.send(new StreamEvent(2, 2, 2));
    streamPublisherA.send(new StreamEvent(2, 2, 2));
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(2, count);
    streamJunctionA.stopProcessing();
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 22 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class JunctionTestCase method multiThreadedWithEventPoolTest.

@Test
public void multiThreadedWithEventPoolTest() throws InterruptedException {
    log.info("multi threaded test using event pool");
    final StreamEventPool streamEventPoolA1 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolA2 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolA3 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolB1 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolB2 = new StreamEventPool(2, 2, 2, 4);
    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, 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, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB1 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB2 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB3 = streamJunctionB.constructPublisher();
    StreamDefinition streamC = StreamDefinition.id("streamC").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionC = new StreamJunction(streamC, executorService, 1024, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherC1 = streamJunctionC.constructPublisher();
    final StreamJunction.Publisher streamPublisherC2 = streamJunctionC.constructPublisher();
    StreamCallback streamCallbackA1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolA1.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A1");
                innerStreamEvent.setOutputData(data);
                streamPublisherB1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolA2.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A2");
                innerStreamEvent.setOutputData(data);
                streamPublisherB2.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA3 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolA3.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A3");
                innerStreamEvent.setOutputData(data);
                streamPublisherB3.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolB1.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("B1");
                innerStreamEvent.setOutputData(data);
                streamPublisherC1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolB2.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("B2");
                innerStreamEvent.setOutputData(data);
                streamPublisherC2.send(innerStreamEvent);
            }
        }
    };
    final boolean[] eventsArrived = { false, false, false, false, false, false, false, false, false, false, false, false };
    StreamCallback streamCallbackC = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                count++;
                eventArrived = true;
                Object symbol = streamEvent.getData()[0];
                if (symbol.equals("IBMA1B1")) {
                    eventsArrived[0] = true;
                } else if (symbol.equals("IBMA1B2")) {
                    eventsArrived[1] = true;
                } else if (symbol.equals("IBMA2B1")) {
                    eventsArrived[2] = true;
                } else if (symbol.equals("IBMA2B2")) {
                    eventsArrived[3] = true;
                } else if (symbol.equals("IBMA3B1")) {
                    eventsArrived[4] = true;
                } else if (symbol.equals("IBMA3B2")) {
                    eventsArrived[5] = true;
                }
                if (symbol.equals("WSO2A1B1")) {
                    eventsArrived[6] = true;
                } else if (symbol.equals("WSO2A1B2")) {
                    eventsArrived[7] = true;
                } else if (symbol.equals("WSO2A2B1")) {
                    eventsArrived[8] = true;
                } else if (symbol.equals("WSO2A2B2")) {
                    eventsArrived[9] = true;
                } else if (symbol.equals("WSO2A3B1")) {
                    eventsArrived[10] = true;
                } else if (symbol.equals("WSO2A3B2")) {
                    eventsArrived[11] = true;
                }
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA1);
    streamJunctionA.subscribe(streamCallbackA2);
    streamJunctionA.subscribe(streamCallbackA3);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB1);
    streamJunctionB.subscribe(streamCallbackB2);
    streamJunctionB.startProcessing();
    streamJunctionC.subscribe(streamCallbackC);
    streamJunctionC.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(1000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(12, count);
    for (boolean arrived : eventsArrived) {
        AssertJUnit.assertTrue(arrived);
    }
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
    streamJunctionC.stopProcessing();
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) Event(org.ballerinalang.siddhi.core.event.Event) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 23 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class JunctionTestCase method multiThreadedTest2.

@Test
public void multiThreadedTest2() throws InterruptedException {
    log.info("multi threaded 2");
    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, 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, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB1 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB2 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB3 = streamJunctionB.constructPublisher();
    StreamDefinition streamC = StreamDefinition.id("streamC").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionC = new StreamJunction(streamC, executorService, 1024, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherC1 = streamJunctionC.constructPublisher();
    final StreamJunction.Publisher streamPublisherC2 = streamJunctionC.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());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A1");
                innerStreamEvent.setOutputData(data);
                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());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A2");
                innerStreamEvent.setOutputData(data);
                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());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A3");
                innerStreamEvent.setOutputData(data);
                streamPublisherB3.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("B1");
                innerStreamEvent.setOutputData(data);
                streamPublisherC1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("B2");
                innerStreamEvent.setOutputData(data);
                streamPublisherC2.send(innerStreamEvent);
            }
        }
    };
    final boolean[] eventsArrived = { false, false, false, false, false, false, false, false, false, false, false, false };
    StreamCallback streamCallbackC = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                count++;
                eventArrived = true;
                Object symbol = streamEvent.getData()[0];
                if (symbol.equals("IBMA1B1")) {
                    eventsArrived[0] = true;
                } else if (symbol.equals("IBMA1B2")) {
                    eventsArrived[1] = true;
                } else if (symbol.equals("IBMA2B1")) {
                    eventsArrived[2] = true;
                } else if (symbol.equals("IBMA2B2")) {
                    eventsArrived[3] = true;
                } else if (symbol.equals("IBMA3B1")) {
                    eventsArrived[4] = true;
                } else if (symbol.equals("IBMA3B2")) {
                    eventsArrived[5] = true;
                }
                if (symbol.equals("WSO2A1B1")) {
                    eventsArrived[6] = true;
                } else if (symbol.equals("WSO2A1B2")) {
                    eventsArrived[7] = true;
                } else if (symbol.equals("WSO2A2B1")) {
                    eventsArrived[8] = true;
                } else if (symbol.equals("WSO2A2B2")) {
                    eventsArrived[9] = true;
                } else if (symbol.equals("WSO2A3B1")) {
                    eventsArrived[10] = true;
                } else if (symbol.equals("WSO2A3B2")) {
                    eventsArrived[11] = true;
                }
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA1);
    streamJunctionA.subscribe(streamCallbackA2);
    streamJunctionA.subscribe(streamCallbackA3);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB1);
    streamJunctionB.subscribe(streamCallbackB2);
    streamJunctionB.startProcessing();
    streamJunctionC.subscribe(streamCallbackC);
    streamJunctionC.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(12, count);
    for (boolean arrived : eventsArrived) {
        AssertJUnit.assertTrue(arrived);
    }
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
    streamJunctionC.stopProcessing();
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) Event(org.ballerinalang.siddhi.core.event.Event) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 24 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class InMemoryTransportTestCase method inMemoryTestCase7.

@Test
public void inMemoryTestCase7() throws InterruptedException {
    log.info("Test inMemory 7");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='test', fail='true', " + "   @map(type='passThrough', @attributes(symbol='trp:symbol'," + "        volume='volume',price='trp:price'))) " + "define stream FooStream (symbol string, price string, volume long); " + "define stream BarStream (symbol string, price string, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            wso2Count.incrementAndGet();
            for (Event event : events) {
                AssertJUnit.assertArrayEquals(event.getData(), new Object[] { "hi", "test", 100L });
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", "in", 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of events", 0, wso2Count.get());
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 25 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class TriggerTestCase method testFilterQuery7.

@Test
public void testFilterQuery7() throws InterruptedException {
    log.info("testTrigger7 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String plan = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "define trigger triggerStream at '*/1 * * * * ?' ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(plan);
    siddhiAppRuntime.addCallback("triggerStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                long timestamp = event.getTimestamp();
                count++;
                if (count > 1) {
                    float triggerTimeDiff = timestamp / 1000 - lastTimeStamp / 1000;
                    AssertJUnit.assertTrue(1.0f == triggerTimeDiff);
                }
                lastTimeStamp = timestamp;
            }
            eventArrived = true;
        }
    });
    siddhiAppRuntime.start();
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)209 Test (org.testng.annotations.Test)204 Event (org.ballerinalang.siddhi.core.event.Event)203 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)201 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)201 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)190 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)13 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)6 Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)6 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)6 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)5 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)2 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 Constructor (java.lang.reflect.Constructor)1