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.controlbus;
018
019import org.apache.camel.Component;
020import org.apache.camel.Consumer;
021import org.apache.camel.LoggingLevel;
022import org.apache.camel.Processor;
023import org.apache.camel.Producer;
024import org.apache.camel.RuntimeCamelException;
025import org.apache.camel.impl.DefaultEndpoint;
026import org.apache.camel.spi.Language;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.UriEndpoint;
029import org.apache.camel.spi.UriParam;
030import org.apache.camel.spi.UriPath;
031import org.apache.camel.util.CamelLogger;
032
033/**
034 * The controlbus component provides easy management of Camel applications based on the Control Bus EIP pattern.
035 *
036 * For example, by sending a message to an Endpoint you can control the lifecycle of routes, or gather performance statistics.
037 */
038@UriEndpoint(firstVersion = "2.11.0", scheme = "controlbus", title = "Control Bus", syntax = "controlbus:command:language", producerOnly = true, label = "core,monitoring")
039public class ControlBusEndpoint extends DefaultEndpoint {
040
041    @UriPath(description = "Command can be either route or language", enums = "route,language") @Metadata(required = "true")
042    private String command;
043    @UriPath(enums = "bean,constant,el,exchangeProperty,file,groovy,header,jsonpath,jxpath,mvel,ognl,ref,simple,spel,sql,terser,tokenize,xpath,xquery,xtokenize")
044    private Language language;
045    @UriParam
046    private String routeId;
047    @UriParam(enums = "start,stop,suspend,resume,restart,status,stats")
048    private String action;
049    @UriParam(defaultValue = "1000")
050    private int restartDelay = 1000;
051    @UriParam
052    private boolean async;
053    @UriParam(defaultValue = "INFO")
054    private LoggingLevel loggingLevel = LoggingLevel.INFO;
055
056    public ControlBusEndpoint(String endpointUri, Component component) {
057        super(endpointUri, component);
058    }
059
060    @Override
061    public Producer createProducer() throws Exception {
062        CamelLogger logger = new CamelLogger(ControlBusProducer.class.getName(), loggingLevel);
063        return new ControlBusProducer(this, logger);
064    }
065
066    @Override
067    public Consumer createConsumer(Processor processor) throws Exception {
068        throw new RuntimeCamelException("Cannot consume from a ControlBusEndpoint: " + getEndpointUri());
069    }
070
071    @Override
072    public boolean isSingleton() {
073        // we dont want to be enlisted in JMX, so lets just be non-singleton
074        return false;
075    }
076
077    @Override
078    public ControlBusComponent getComponent() {
079        return (ControlBusComponent) super.getComponent();
080    }
081
082    public Language getLanguage() {
083        return language;
084    }
085
086    /**
087     * Allows you to specify the name of a Language to use for evaluating the message body.
088     * If there is any result from the evaluation, then the result is put in the message body.
089     */
090    public void setLanguage(Language language) {
091        this.language = language;
092    }
093
094    public String getRouteId() {
095        return routeId;
096    }
097
098    /**
099     * To specify a route by its id.
100     * The special keyword "current" indicates the current route.
101     */
102    public void setRouteId(String routeId) {
103        this.routeId = routeId;
104    }
105
106    public String getAction() {
107        return action;
108    }
109
110    /**
111     * To denote an action that can be either: start, stop, or status.
112     * <p/>
113     * To either start or stop a route, or to get the status of the route as output in the message body.
114     * You can use suspend and resume from Camel 2.11.1 onwards to either suspend or resume a route.
115     * And from Camel 2.11.1 onwards you can use stats to get performance statics returned in XML format;
116     * the routeId option can be used to define which route to get the performance stats for, if routeId is not defined,
117     * then you get statistics for the entire CamelContext. The restart action will restart the route.
118     */
119    public void setAction(String action) {
120        this.action = action;
121    }
122
123    public int getRestartDelay() {
124        return restartDelay;
125    }
126
127    /**
128     * The delay in millis to use when restarting a route.
129     */
130    public void setRestartDelay(int restartDelay) {
131        this.restartDelay = restartDelay;
132    }
133
134    public boolean isAsync() {
135        return async;
136    }
137
138    /**
139     * Whether to execute the control bus task asynchronously.
140     * <p/>
141     * Important: If this option is enabled, then any result from the task is not set on the Exchange.
142     * This is only possible if executing tasks synchronously.
143     */
144    public void setAsync(boolean async) {
145        this.async = async;
146    }
147
148    public LoggingLevel getLoggingLevel() {
149        return loggingLevel;
150    }
151
152    /**
153     * Logging level used for logging when task is done, or if any exceptions occurred during processing the task.
154     */
155    public void setLoggingLevel(LoggingLevel loggingLevel) {
156        this.loggingLevel = loggingLevel;
157    }
158}