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@UriEndpoint(scheme = "scheduler", syntax = "scheduler:name", consumerOnly = true, consumerClass = SchedulerConsumer.class, label = "core,scheduling")
031public class SchedulerEndpoint extends ScheduledPollEndpoint {
032
033    @UriPath @Metadata(required = "true")
034    private String name;
035    @UriParam(defaultValue = "1")
036    private int concurrentTasks = 1;
037
038    public SchedulerEndpoint(String uri, SchedulerComponent component, String remaining) {
039        super(uri, component);
040        this.name = remaining;
041    }
042
043    @Override
044    public SchedulerComponent getComponent() {
045        return (SchedulerComponent) super.getComponent();
046    }
047
048    @Override
049    public Producer createProducer() throws Exception {
050        throw new UnsupportedOperationException("Scheduler cannot be used as a producer");
051    }
052
053    @Override
054    public Consumer createConsumer(Processor processor) throws Exception {
055        SchedulerConsumer consumer = new SchedulerConsumer(this, processor);
056        configureConsumer(consumer);
057        return consumer;
058    }
059
060    @Override
061    public boolean isSingleton() {
062        return true;
063    }
064
065    public String getName() {
066        return name;
067    }
068
069    /**
070     * The name of the scheduler
071     */
072    public void setName(String name) {
073        this.name = name;
074    }
075
076    public int getConcurrentTasks() {
077        return concurrentTasks;
078    }
079
080    /**
081     * Number of threads used by the scheduling thread pool.
082     * <p/>
083     * Is by default using a single thread
084     */
085    public void setConcurrentTasks(int concurrentTasks) {
086        this.concurrentTasks = concurrentTasks;
087    }
088
089    public void onConsumerStart(SchedulerConsumer consumer) {
090        // if using default scheduler then obtain thread pool from component which manages their lifecycle
091        if (consumer.getScheduler() == null && consumer.getScheduledExecutorService() == null) {
092            ScheduledExecutorService scheduler = getComponent().addConsumer(consumer);
093            consumer.setScheduledExecutorService(scheduler);
094        }
095    }
096
097    public void onConsumerStop(SchedulerConsumer consumer) {
098        getComponent().removeConsumer(consumer);
099    }
100}