Search in sources :

Example 6 with StreamJunction

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

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.ballerinalang.siddhi.core.table.Table) SourceHandlerManager(org.ballerinalang.siddhi.core.stream.input.source.SourceHandlerManager) RecordTableHandler(org.ballerinalang.siddhi.core.table.record.RecordTableHandler) EternalReferencedHolder(org.ballerinalang.siddhi.core.util.extension.holder.EternalReferencedHolder) Source(org.ballerinalang.siddhi.core.stream.input.source.Source) Sink(org.ballerinalang.siddhi.core.stream.output.sink.Sink) StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) SinkHandlerManager(org.ballerinalang.siddhi.core.stream.output.sink.SinkHandlerManager) RecordTableHandlerManager(org.ballerinalang.siddhi.core.table.record.RecordTableHandlerManager)

Example 7 with StreamJunction

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

the class SiddhiAppRuntime method addCallback.

public void addCallback(String streamId, StreamCallback streamCallback) {
    streamCallback.setStreamId(streamId);
    StreamJunction streamJunction = streamJunctionMap.get(streamId);
    if (streamJunction == null) {
        throw new DefinitionNotExistException("No stream found with name: " + streamId);
    }
    streamCallback.setStreamDefinition(streamDefinitionMap.get(streamId));
    streamCallback.setContext(siddhiAppContext);
    streamJunction.subscribe(streamCallback);
}
Also used : StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) DefinitionNotExistException(org.ballerinalang.siddhi.core.exception.DefinitionNotExistException)

Example 8 with StreamJunction

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

the class DefinitionParserHelper method addEventTrigger.

public static void addEventTrigger(TriggerDefinition triggerDefinition, ConcurrentMap<String, Trigger> eventTriggerMap, ConcurrentMap<String, StreamJunction> streamJunctionMap, SiddhiAppContext siddhiAppContext) {
    if (!eventTriggerMap.containsKey(triggerDefinition.getId())) {
        Trigger trigger;
        if (triggerDefinition.getAtEvery() != null) {
            trigger = new PeriodicTrigger();
        } else if (triggerDefinition.getAt().trim().equalsIgnoreCase(SiddhiConstants.TRIGGER_START)) {
            trigger = new StartTrigger();
        } else {
            trigger = new CronTrigger();
        }
        StreamJunction streamJunction = streamJunctionMap.get(triggerDefinition.getId());
        trigger.init(triggerDefinition, siddhiAppContext, streamJunction);
        siddhiAppContext.addEternalReferencedHolder(trigger);
        eventTriggerMap.putIfAbsent(trigger.getId(), trigger);
    }
}
Also used : CronTrigger(org.ballerinalang.siddhi.core.trigger.CronTrigger) Trigger(org.ballerinalang.siddhi.core.trigger.Trigger) StartTrigger(org.ballerinalang.siddhi.core.trigger.StartTrigger) CronTrigger(org.ballerinalang.siddhi.core.trigger.CronTrigger) PeriodicTrigger(org.ballerinalang.siddhi.core.trigger.PeriodicTrigger) StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) StartTrigger(org.ballerinalang.siddhi.core.trigger.StartTrigger) PeriodicTrigger(org.ballerinalang.siddhi.core.trigger.PeriodicTrigger)

Example 9 with StreamJunction

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

the class SiddhiAppRuntimeBuilder method addQuery.

public String addQuery(QueryRuntime queryRuntime) {
    QueryRuntime oldQueryRuntime = queryProcessorMap.put(queryRuntime.getQueryId(), queryRuntime);
    if (oldQueryRuntime != null) {
        throw new SiddhiAppCreationException("Multiple queries with name '" + queryRuntime.getQueryId() + "' defined in Siddhi App '" + siddhiAppContext.getName() + "'", queryRuntime.getQuery().getQueryContextStartIndex(), queryRuntime.getQuery().getQueryContextEndIndex());
    }
    StreamRuntime streamRuntime = queryRuntime.getStreamRuntime();
    for (SingleStreamRuntime singleStreamRuntime : streamRuntime.getSingleStreamRuntimes()) {
        ProcessStreamReceiver processStreamReceiver = singleStreamRuntime.getProcessStreamReceiver();
        if (processStreamReceiver.toStream()) {
            StreamJunction streamJuction = streamJunctionMap.get(processStreamReceiver.getStreamId());
            if (streamJuction != null) {
                streamJuction.subscribe(processStreamReceiver);
            } else {
                throw new SiddhiAppCreationException("Expecting a stream, but provided '" + processStreamReceiver.getStreamId() + "' is not a stream");
            }
        }
    }
    OutputCallback outputCallback = queryRuntime.getOutputCallback();
    if (outputCallback != null && outputCallback instanceof InsertIntoStreamCallback) {
        InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) outputCallback;
        StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
        streamDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
        DefinitionParserHelper.validateOutputStream(streamDefinition, streamDefinitionMap.get(streamDefinition.getId()));
        StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoStreamCallback.init(streamJunctionMap.get(insertIntoStreamCallback.getOutputStreamDefinition().getId()));
    } else if (outputCallback != null && outputCallback instanceof InsertIntoWindowCallback) {
        InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) outputCallback;
        StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
        windowDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
        DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(streamDefinition.getId()));
        StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
    }
    return queryRuntime.getQueryId();
}
Also used : ProcessStreamReceiver(org.ballerinalang.siddhi.core.query.input.ProcessStreamReceiver) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) InsertIntoWindowCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoWindowCallback) QueryRuntime(org.ballerinalang.siddhi.core.query.QueryRuntime) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) StreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) InsertIntoStreamCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoStreamCallback) OutputCallback(org.ballerinalang.siddhi.core.query.output.callback.OutputCallback)

Example 10 with StreamJunction

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

the class InputManager method constructInputHandler.

public InputHandler constructInputHandler(String streamId) {
    InputHandler inputHandler = new InputHandler(streamId, inputHandlerMap.size(), inputEntryValve);
    StreamJunction streamJunction = streamJunctionMap.get(streamId);
    if (streamJunction == null) {
        throw new DefinitionNotExistException("Stream with stream ID " + streamId + " has not been defined");
    }
    inputDistributor.addInputProcessor(streamJunctionMap.get(streamId).constructPublisher());
    inputHandlerMap.put(streamId, inputHandler);
    return inputHandler;
}
Also used : StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) DefinitionNotExistException(org.ballerinalang.siddhi.core.exception.DefinitionNotExistException)

Aggregations

StreamJunction (org.ballerinalang.siddhi.core.stream.StreamJunction)10 QueryRuntime (org.ballerinalang.siddhi.core.query.QueryRuntime)3 InsertIntoStreamCallback (org.ballerinalang.siddhi.core.query.output.callback.InsertIntoStreamCallback)3 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)3 DefinitionNotExistException (org.ballerinalang.siddhi.core.exception.DefinitionNotExistException)2 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)2 StreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime)2 InsertIntoStream (org.ballerinalang.siddhi.query.api.execution.query.output.stream.InsertIntoStream)2 ArrayList (java.util.ArrayList)1 ProcessStreamReceiver (org.ballerinalang.siddhi.core.query.input.ProcessStreamReceiver)1 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)1 InsertIntoWindowCallback (org.ballerinalang.siddhi.core.query.output.callback.InsertIntoWindowCallback)1 OutputCallback (org.ballerinalang.siddhi.core.query.output.callback.OutputCallback)1 Source (org.ballerinalang.siddhi.core.stream.input.source.Source)1 SourceHandlerManager (org.ballerinalang.siddhi.core.stream.input.source.SourceHandlerManager)1 Sink (org.ballerinalang.siddhi.core.stream.output.sink.Sink)1 SinkHandlerManager (org.ballerinalang.siddhi.core.stream.output.sink.SinkHandlerManager)1 Table (org.ballerinalang.siddhi.core.table.Table)1 RecordTableHandler (org.ballerinalang.siddhi.core.table.record.RecordTableHandler)1 RecordTableHandlerManager (org.ballerinalang.siddhi.core.table.record.RecordTableHandlerManager)1