Search in sources :

Example 16 with Scheduler

use of org.wso2.siddhi.core.util.Scheduler in project carbon-business-process by wso2.

the class HumanTaskServer method initScheduler.

/**
 * Scheduler initialisation.
 */
private void initScheduler() {
    ThreadFactory threadFactory = new ThreadFactory() {

        private int threadNumber = 0;

        public Thread newThread(Runnable r) {
            threadNumber += 1;
            Thread t = new Thread(r, "HumanTaskServer-" + threadNumber);
            t.setDaemon(true);
            return t;
        }
    };
    ExecutorService executorService = Executors.newFixedThreadPool(serverConfig.getThreadPoolMaxSize(), threadFactory);
    SimpleScheduler simpleScheduler = new SimpleScheduler(new GUID().toString());
    simpleScheduler.setExecutorService(executorService);
    simpleScheduler.setTransactionManager(tnxManager);
    taskEngine.setScheduler(simpleScheduler);
    simpleScheduler.setJobProcessor(new JobProcessorImpl());
    // Start the scheduler within the HumanTaskSchedulerInitializer to ensure that all the tasks are deployed
    // when the scheduler actually starts.
    // simpleScheduler.start();
    scheduler = simpleScheduler;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) GUID(org.wso2.carbon.humantask.core.utils.GUID) SimpleScheduler(org.wso2.carbon.humantask.core.scheduler.SimpleScheduler) JobProcessorImpl(org.wso2.carbon.humantask.core.scheduler.JobProcessorImpl)

Example 17 with Scheduler

use of org.wso2.siddhi.core.util.Scheduler in project carbon-business-process by wso2.

the class BPELServerConfiguration method populateODESchedulerConfiguration.

/**
 * Populate ODE scheduler configuration.
 */
private void populateODESchedulerConfiguration() {
    if (bpsConfigDocument.getWSO2BPS().getODESchedulerConfiguration() != null) {
        SimpleSchedulerConfig config = bpsConfigDocument.getWSO2BPS().getODESchedulerConfiguration();
        odeSchedulerQueueLength = config.getODESchedulerQueueLength() > 0 ? config.getODESchedulerQueueLength() : odeSchedulerQueueLength;
        odeSchedulerImmediateInterval = config.getODESchedulerImmediateInterval() > 0 ? config.getODESchedulerImmediateInterval() : odeSchedulerImmediateInterval;
        odeSchedulerNearFutureInterval = config.getODESchedulerNearFutureInterval() > 0 ? config.getODESchedulerNearFutureInterval() : odeSchedulerNearFutureInterval;
        odeSchedulerStaleInterval = config.getODESchedulerStaleInterval() > 0 ? config.getODESchedulerStaleInterval() : odeSchedulerStaleInterval;
        odeSchedulerTransactionsPerSecond = config.getODESchedulerTransactionsPerSecond() > 0 ? config.getODESchedulerTransactionsPerSecond() : odeSchedulerTransactionsPerSecond;
        odeSchedulerWarningDelay = config.getODESchedulerWarningDelay() > 0 ? config.getODESchedulerWarningDelay() : odeSchedulerWarningDelay;
        odeSchedulerImmediateTransactionRetryLimit = config.getODESchedulerImmediateTransactionRetryLimit() > 0 ? config.getODESchedulerImmediateTransactionRetryLimit() : odeSchedulerImmediateTransactionRetryLimit;
        odeSchedulerImmediateTransactionRetryInterval = config.getODESchedulerImmediateInterval() > 0 ? config.getODESchedulerImmediateTransactionRetryInterval() : odeSchedulerImmediateTransactionRetryInterval;
    }
}
Also used : SimpleSchedulerConfig(org.wso2.carbon.bpel.config.SimpleSchedulerConfig)

Example 18 with Scheduler

use of org.wso2.siddhi.core.util.Scheduler in project carbon-business-process by wso2.

the class BPMNEngineServerStartupObserver method completedServerStartup.

@Override
public void completedServerStartup() {
    if (!CarbonUtils.isChildNode() && TRUE.equalsIgnoreCase(BPMNActivitiConfiguration.getInstance().getBPMNPropertyValue(BPMNConstants.SUBSTITUTION_CONFIG, BPMNConstants.SUBSTITUTION_ENABLED))) {
        String activationIntervalString = BPMNActivitiConfiguration.getInstance().getBPMNPropertyValue(BPMNConstants.SUBSTITUTION_CONFIG, BPMNConstants.SUBSTITUTION_SCHEDULER_INTERVAL);
        long interval = BPMNConstants.DEFAULT_SUBSTITUTION_INTERVAL_IN_MINUTES * 60 * 1000;
        if (activationIntervalString != null) {
            interval = Long.parseLong(activationIntervalString) * 60 * 1000;
        }
        BPMNServerHolder.getInstance().setSubstitutionScheduler(new SubstitutionScheduler(interval));
        BPMNServerHolder.getInstance().getSubstitutionScheduler().start();
        log.info("BPMN Substitution scheduler started.");
    }
}
Also used : SubstitutionScheduler(org.wso2.carbon.bpmn.people.substitution.scheduler.SubstitutionScheduler)

Example 19 with Scheduler

use of org.wso2.siddhi.core.util.Scheduler in project carbon-apimgt by wso2.

the class ThrottleStreamProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater) {
    synchronized (this) {
        if (expireEventTime == -1) {
            long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
            if (startTime != -1) {
                expireEventTime = addTimeShift(currentTime);
            } else {
                expireEventTime = siddhiAppContext.getTimestampGenerator().currentTime() + timeInMilliSeconds;
            }
            if (scheduler != null) {
                scheduler.notifyAt(expireEventTime);
            } else {
                log.error("scheduler is not initiated");
            }
        }
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        boolean sendEvents;
        if (currentTime >= expireEventTime) {
            expireEventTime += timeInMilliSeconds;
            if (scheduler != null) {
                scheduler.notifyAt(expireEventTime);
            } else {
                log.error("scheduler is not initiated");
            }
            sendEvents = true;
        } else {
            sendEvents = false;
        }
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            if (streamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            complexEventPopulater.populateComplexEvent(streamEvent, new Object[] { expireEventTime });
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedStreamEvent.setType(StreamEvent.Type.EXPIRED);
            clonedStreamEvent.setTimestamp(expireEventTime);
            expiredEventChunk.add(clonedStreamEvent);
        }
        if (sendEvents) {
            expiredEventChunk.reset();
            if (expiredEventChunk.getFirst() != null) {
                streamEventChunk.add(expiredEventChunk.getFirst());
            }
            expiredEventChunk.clear();
        }
    }
    if (streamEventChunk.getFirst() != null) {
        streamEventChunk.setBatch(true);
        nextProcessor.process(streamEventChunk);
        streamEventChunk.setBatch(false);
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 20 with Scheduler

use of org.wso2.siddhi.core.util.Scheduler in project siddhi by wso2.

the class SiddhiAppParser method parse.

/**
 * Parse an SiddhiApp returning SiddhiAppRuntime
 *
 * @param siddhiApp       plan to be parsed
 * @param siddhiAppString content of Siddhi application as string
 * @param siddhiContext   SiddhiContext  @return SiddhiAppRuntime
 *
 * @return SiddhiAppRuntimeBuilder
 */
public static SiddhiAppRuntimeBuilder parse(SiddhiApp siddhiApp, String siddhiAppString, SiddhiContext siddhiContext) {
    SiddhiAppContext siddhiAppContext = new SiddhiAppContext();
    siddhiAppContext.setSiddhiContext(siddhiContext);
    siddhiAppContext.setSiddhiAppString(siddhiAppString);
    try {
        Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_NAME, null, siddhiApp.getAnnotations());
        if (element != null) {
            siddhiAppContext.setName(element.getValue());
        } else {
            siddhiAppContext.setName(UUID.randomUUID().toString());
        }
        Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ENFORCE_ORDER, siddhiApp.getAnnotations());
        if (annotation != null) {
            siddhiAppContext.setEnforceOrder(true);
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ASYNC, siddhiApp.getAnnotations());
        if (annotation != null) {
            throw new SiddhiAppCreationException("@Async not supported in SiddhiApp level, " + "instead use @Async with streams", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STATISTICS, siddhiApp.getAnnotations());
        List<Element> statisticsElements = new ArrayList<>();
        if (annotation != null) {
            statisticsElements = annotation.getElements();
        }
        if (siddhiContext.getStatisticsConfiguration() != null) {
            siddhiAppContext.setStatisticsManager(siddhiContext.getStatisticsConfiguration().getFactory().createStatisticsManager(siddhiContext.getStatisticsConfiguration().getMetricPrefix(), siddhiAppContext.getName(), statisticsElements));
        }
        Element statStateEnableElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_ENABLE, siddhiApp.getAnnotations());
        if (statStateEnableElement != null && Boolean.valueOf(statStateEnableElement.getValue())) {
            siddhiAppContext.setStatsEnabled(true);
        } else {
            Element statStateElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, null, siddhiApp.getAnnotations());
            // where sp uses @app:statistics('true').
            if (annotation != null && (statStateElement == null || Boolean.valueOf(statStateElement.getValue()))) {
                siddhiAppContext.setStatsEnabled(true);
            }
        }
        Element statStateIncludElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
        siddhiAppContext.setIncludedMetrics(generateIncludedMetrics(statStateIncludElement));
        siddhiAppContext.setThreadBarrier(new ThreadBarrier());
        siddhiAppContext.setExecutorService(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-executor-thread-%d").build()));
        siddhiAppContext.setScheduledExecutorService(Executors.newScheduledThreadPool(5, new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-scheduler-thread-%d").build()));
        // Select the TimestampGenerator based on playback mode on/off
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PLAYBACK, siddhiApp.getAnnotations());
        if (annotation != null) {
            String idleTime = null;
            String increment = null;
            EventTimeBasedMillisTimestampGenerator timestampGenerator = new EventTimeBasedMillisTimestampGenerator(siddhiAppContext.getScheduledExecutorService());
            // Get the optional elements of playback annotation
            for (Element e : annotation.getElements()) {
                if (SiddhiConstants.ANNOTATION_ELEMENT_IDLE_TIME.equalsIgnoreCase(e.getKey())) {
                    idleTime = e.getValue();
                } else if (SiddhiConstants.ANNOTATION_ELEMENT_INCREMENT.equalsIgnoreCase(e.getKey())) {
                    increment = e.getValue();
                } else {
                    throw new SiddhiAppValidationException("Playback annotation accepts only idle.time and " + "increment but found " + e.getKey());
                }
            }
            // idleTime and increment are optional but if one presents, the other also should be given
            if (idleTime != null && increment == null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but increment not found");
            } else if (idleTime == null && increment != null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but idle.time does not found");
            } else if (idleTime != null) {
                // The fourth case idleTime == null && increment == null are ignored because it means no heartbeat.
                try {
                    timestampGenerator.setIdleTime(SiddhiCompiler.parseTimeConstantDefinition(idleTime).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid idle.time constant '" + idleTime + "' in playback " + "annotation", ex);
                }
                try {
                    timestampGenerator.setIncrementInMilliseconds(SiddhiCompiler.parseTimeConstantDefinition(increment).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid increment constant '" + increment + "' in playback " + "annotation", ex);
                }
            }
            siddhiAppContext.setTimestampGenerator(timestampGenerator);
            siddhiAppContext.setPlayback(true);
        } else {
            siddhiAppContext.setTimestampGenerator(new SystemCurrentTimeMillisTimestampGenerator());
        }
        siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
        siddhiAppContext.setPersistenceService(new PersistenceService(siddhiAppContext));
        siddhiAppContext.setElementIdGenerator(new ElementIdGenerator(siddhiAppContext.getName()));
    } catch (DuplicateAnnotationException e) {
        throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Siddhi app " + siddhiApp.toString(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
    }
    SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder = new SiddhiAppRuntimeBuilder(siddhiAppContext);
    defineStreamDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getStreamDefinitionMap(), siddhiAppContext);
    defineTableDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTableDefinitionMap(), siddhiAppContext);
    defineWindowDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getWindowDefinitionMap(), siddhiAppContext);
    defineFunctionDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getFunctionDefinitionMap(), siddhiAppContext);
    defineAggregationDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getAggregationDefinitionMap(), siddhiAppContext);
    for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
        try {
            window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId());
        } catch (Throwable t) {
            ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
            throw t;
        }
    }
    int queryIndex = 1;
    for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
        if (executionElement instanceof Query) {
            try {
                QueryRuntime queryRuntime = QueryParser.parse((Query) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex));
                siddhiAppRuntimeBuilder.addQuery(queryRuntime);
                queryIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
                throw t;
            }
        } else {
            try {
                PartitionRuntime partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), queryIndex);
                siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
                siddhiAppContext.getSnapshotService().addSnapshotable("partition", partitionRuntime);
                queryIndex += ((Partition) executionElement).getQueryList().size();
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Partition) executionElement, siddhiAppContext);
                throw t;
            }
        }
    }
    // Done last as they have to be started last
    defineTriggerDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTriggerDefinitionMap(), siddhiAppContext);
    return siddhiAppRuntimeBuilder;
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) Element(org.wso2.siddhi.query.api.annotation.Element) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement) ArrayList(java.util.ArrayList) ElementIdGenerator(org.wso2.siddhi.core.util.ElementIdGenerator) EventTimeBasedMillisTimestampGenerator(org.wso2.siddhi.core.util.timestamp.EventTimeBasedMillisTimestampGenerator) ThreadBarrier(org.wso2.siddhi.core.util.ThreadBarrier) QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Window(org.wso2.siddhi.core.window.Window) SnapshotService(org.wso2.siddhi.core.util.snapshot.SnapshotService) Partition(org.wso2.siddhi.query.api.execution.partition.Partition) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) Annotation(org.wso2.siddhi.query.api.annotation.Annotation) PersistenceService(org.wso2.siddhi.core.util.persistence.PersistenceService) SiddhiAppRuntimeBuilder(org.wso2.siddhi.core.util.SiddhiAppRuntimeBuilder) DuplicateAnnotationException(org.wso2.siddhi.query.api.exception.DuplicateAnnotationException) SiddhiParserException(org.wso2.siddhi.query.compiler.exception.SiddhiParserException) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement) SystemCurrentTimeMillisTimestampGenerator(org.wso2.siddhi.core.util.timestamp.SystemCurrentTimeMillisTimestampGenerator) SiddhiAppContext(org.wso2.siddhi.core.config.SiddhiAppContext) PartitionRuntime(org.wso2.siddhi.core.partition.PartitionRuntime)

Aggregations

StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)13 Scheduler (org.wso2.siddhi.core.util.Scheduler)6 EntryValveProcessor (org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor)5 ArrayList (java.util.ArrayList)4 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)3 SingleStreamRuntime (org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime)3 SubstitutionScheduler (org.wso2.carbon.bpmn.people.substitution.scheduler.SubstitutionScheduler)2 OperationNotSupportedException (org.wso2.siddhi.core.exception.OperationNotSupportedException)2 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)2 SchedulingProcessor (org.wso2.siddhi.core.query.processor.SchedulingProcessor)2 WindowProcessor (org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor)2 Attribute (org.wso2.siddhi.query.api.definition.Attribute)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 AbstractMap (java.util.AbstractMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 SimpleSchedulerConfig (org.wso2.carbon.bpel.config.SimpleSchedulerConfig)1