001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.scheduler;
018
019import java.util.concurrent.ScheduledExecutorService;
020
021import org.apache.camel.Consumer;
022import org.apache.camel.Processor;
023import org.apache.camel.Producer;
024import org.apache.camel.impl.ScheduledPollEndpoint;
025import org.apache.camel.spi.Metadata;
026import org.apache.camel.spi.UriEndpoint;
027import org.apache.camel.spi.UriParam;
028import org.apache.camel.spi.UriPath;
029
030/**
031 * The scheduler component is used for generating message exchanges when a scheduler fires.
032 *
033 * This component is similar to the timer component, but it offers more functionality in terms of scheduling.
034 * Also this component uses JDK ScheduledExecutorService. Where as the timer uses a JDK Timer.
035 */
036@UriEndpoint(firstVersion = "2.15.0", scheme = "scheduler", title = "Scheduler", syntax = "scheduler:name",
037    consumerOnly = true, consumerClass = SchedulerConsumer.class, label = "core,scheduling")
038public class SchedulerEndpoint extends ScheduledPollEndpoint {
039
040    @UriPath @Metadata(required = "true")
041    private String name;
042    @UriParam(defaultValue = "1", label = "scheduler")
043    private int concurrentTasks = 1;
044
045    public SchedulerEndpoint(String uri, SchedulerComponent component, String remaining) {
046        super(uri, component);
047        this.name = remaining;
048    }
049
050    @Override
051    public SchedulerComponent getComponent() {
052        return (SchedulerComponent) super.getComponent();
053    }
054
055    @Override
056    public Producer createProducer() throws Exception {
057        throw new UnsupportedOperationException("Scheduler cannot be used as a producer");
058    }
059
060    @Override
061    public Consumer createConsumer(Processor processor) throws Exception {
062        SchedulerConsumer consumer = new SchedulerConsumer(this, processor);
063        configureConsumer(consumer);
064        return consumer;
065    }
066
067    @Override
068    public boolean isSingleton() {
069        return true;
070    }
071
072    public String getName() {
073        return name;
074    }
075
076    /**
077     * The name of the scheduler
078     */
079    public void setName(String name) {
080        this.name = name;
081    }
082
083    public int getConcurrentTasks() {
084        return concurrentTasks;
085    }
086
087    /**
088     * Number of threads used by the scheduling thread pool.
089     * <p/>
090     * Is by default using a single thread
091     */
092    public void setConcurrentTasks(int concurrentTasks) {
093        this.concurrentTasks = concurrentTasks;
094    }
095
096    public void onConsumerStart(SchedulerConsumer consumer) {
097        // if using default scheduler then obtain thread pool from component which manages their lifecycle
098        if (consumer.getScheduler() == null && consumer.getScheduledExecutorService() == null) {
099            ScheduledExecutorService scheduler = getComponent().addConsumer(consumer);
100            consumer.setScheduledExecutorService(scheduler);
101        }
102    }
103
104    public void onConsumerStop(SchedulerConsumer consumer) {
105        getComponent().removeConsumer(consumer);
106    }
107}