Search in sources :

Example 61 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryTestCase6.

@Test
public void inMemoryTestCase6() throws InterruptedException {
    log.info("Test inMemory 6");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='test', " + "   @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 }));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "IBM", "in", 100L }));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", "in", 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of events", 3, wso2Count.get());
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 62 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryTestCase3.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void inMemoryTestCase3() throws InterruptedException {
    log.info("Test inMemory 3");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='inMemory', topic='Foo', @map(type='passThrough', @attributes(symbol='symbol'))) " + "define stream FooStream (symbol string, price float, volume long); " + "@sink(type='inMemory', topic='{{symbol}}', @map(type='passThrough')) " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 63 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryTestCase5.

@Test
public void inMemoryTestCase5() throws InterruptedException {
    log.info("Test inMemory 5");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='inMemory', topic='Foo', @map(type='passThrough', @attributes(symbol='symbol'," + "volume='volume',price='price'))) " + "define stream FooStream (symbol string, price float, volume long); " + "@sink(type='inMemory', topic='{{symbol}}', @map(type='passThrough')) " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 64 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class SiddhiAppRuntime method persist.

public PersistenceReference persist() {
    try {
        // first, pause all the event sources
        sourceMap.values().forEach(list -> list.forEach(Source::pause));
        // take snapshots of execution units
        byte[] snapshots = siddhiAppContext.getSnapshotService().snapshot();
        // start the snapshot persisting task asynchronously
        AsyncSnapshotPersistor asyncSnapshotPersistor = new AsyncSnapshotPersistor(snapshots, siddhiAppContext.getSiddhiContext().getPersistenceStore(), siddhiAppContext.getName());
        String revision = asyncSnapshotPersistor.getRevision();
        Future future = siddhiAppContext.getExecutorService().submit(asyncSnapshotPersistor);
        return new PersistenceReference(future, revision);
    } finally {
        // at the end, resume the event sources
        sourceMap.values().forEach(list -> list.forEach(Source::resume));
    }
}
Also used : AsyncSnapshotPersistor(org.wso2.siddhi.core.util.snapshot.AsyncSnapshotPersistor) Future(java.util.concurrent.Future) PersistenceReference(org.wso2.siddhi.core.util.snapshot.PersistenceReference)

Example 65 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class SiddhiAppRuntime method shutdown.

public synchronized void shutdown() {
    SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
    for (List<Source> sources : sourceMap.values()) {
        for (Source source : sources) {
            try {
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.unregisterSourceHandler(source.getMapper().getHandler().getElementId());
                }
                source.shutdown();
            } catch (Throwable t) {
                log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down source '" + StringUtil.removeCRLFCharacters(source.getType()) + "' at '" + StringUtil.removeCRLFCharacters(source.getStreamDefinition().getId()) + "' on Siddhi App '" + siddhiAppContext.getName() + "'.", t);
            }
        }
    }
    for (Table table : tableMap.values()) {
        try {
            table.shutdown();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down table '" + StringUtil.removeCRLFCharacters(table.getTableDefinition().getId()) + "' on Siddhi App '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
        }
    }
    SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
    for (List<Sink> sinks : sinkMap.values()) {
        for (Sink sink : sinks) {
            try {
                if (sinkHandlerManager != null) {
                    sinkHandlerManager.unregisterSinkHandler(sink.getHandler().getElementId());
                }
                sink.shutdown();
            } catch (Throwable t) {
                log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down sink '" + StringUtil.removeCRLFCharacters(sink.getType()) + "' at '" + StringUtil.removeCRLFCharacters(sink.getStreamDefinition().getId()) + "' on Siddhi App '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
            }
        }
    }
    for (Table table : tableMap.values()) {
        RecordTableHandlerManager recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
        if (recordTableHandlerManager != null) {
            String elementId = null;
            RecordTableHandler recordTableHandler = table.getHandler();
            if (recordTableHandler != null) {
                elementId = recordTableHandler.getElementId();
            }
            if (elementId != null) {
                recordTableHandlerManager.unregisterRecordTableHandler(elementId);
            }
        }
        table.shutdown();
    }
    for (EternalReferencedHolder eternalReferencedHolder : siddhiAppContext.getEternalReferencedHolders()) {
        try {
            eternalReferencedHolder.stop();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error while stopping EternalReferencedHolder '" + StringUtil.removeCRLFCharacters(eternalReferencedHolder.toString()) + "' down Siddhi app '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
        }
    }
    inputManager.disconnect();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            for (StreamJunction streamJunction : streamJunctionMap.values()) {
                streamJunction.stopProcessing();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            siddhiAppContext.getScheduledExecutorService().shutdownNow();
            siddhiAppContext.getExecutorService().shutdownNow();
        }
    }, "Siddhi-SiddhiApp-" + siddhiAppContext.getName() + "-Shutdown-Cleaner");
    thread.start();
    if (siddhiAppRuntimeMap != null) {
        siddhiAppRuntimeMap.remove(siddhiAppContext.getName());
    }
    if (siddhiAppContext.getStatisticsManager() != null) {
        if (siddhiAppContext.isStatsEnabled()) {
            siddhiAppContext.getStatisticsManager().stopReporting();
        }
        siddhiAppContext.getStatisticsManager().cleanup();
    }
    running = false;
}
Also used : Table(org.wso2.siddhi.core.table.Table) SourceHandlerManager(org.wso2.siddhi.core.stream.input.source.SourceHandlerManager) RecordTableHandler(org.wso2.siddhi.core.table.record.RecordTableHandler) EternalReferencedHolder(org.wso2.siddhi.core.util.extension.holder.EternalReferencedHolder) Source(org.wso2.siddhi.core.stream.input.source.Source) Sink(org.wso2.siddhi.core.stream.output.sink.Sink) StreamJunction(org.wso2.siddhi.core.stream.StreamJunction) SinkHandlerManager(org.wso2.siddhi.core.stream.output.sink.SinkHandlerManager) RecordTableHandlerManager(org.wso2.siddhi.core.table.record.RecordTableHandlerManager)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6