org.apache.camel.impl.engine.DefaultRoute Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.impl.engine;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Route;
import org.apache.camel.Service;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.TimeUtils;
/**
* Default implementation of {@link Route}.
*
* Use the API from {@link org.apache.camel.CamelContext} to control the lifecycle of a route,
* such as starting and stopping using the {@link org.apache.camel.spi.RouteController#startRoute(String)}
* and {@link org.apache.camel.spi.RouteController#stopRoute(String)} methods.
*/
public abstract class DefaultRoute extends ServiceSupport implements Route {
private final Endpoint endpoint;
private final Map properties = new HashMap<>();
private final List services = new ArrayList<>();
private final RouteContext routeContext;
private Date startDate;
public DefaultRoute(RouteContext routeContext, Endpoint endpoint) {
this.routeContext = routeContext;
this.endpoint = endpoint;
}
public DefaultRoute(RouteContext routeContext, Endpoint endpoint, Service... services) {
this(routeContext, endpoint);
for (Service service : services) {
addService(service);
}
}
@Override
public String toString() {
return "Route " + getId();
}
@Override
public String getId() {
return (String) properties.get(Route.ID_PROPERTY);
}
@Override
public String getGroup() {
return (String) properties.get(Route.GROUP_PROPERTY);
}
@Override
public String getUptime() {
long delta = getUptimeMillis();
if (delta == 0) {
return "";
}
return TimeUtils.printDuration(delta);
}
@Override
public long getUptimeMillis() {
if (startDate == null) {
return 0;
}
return new Date().getTime() - startDate.getTime();
}
@Override
public Endpoint getEndpoint() {
return endpoint;
}
@Override
public RouteContext getRouteContext() {
return routeContext;
}
@Override
public CamelContext getCamelContext() {
return routeContext.getCamelContext();
}
@Override
public Map getProperties() {
return properties;
}
@Override
public String getDescription() {
Object value = properties.get(Route.DESCRIPTION_PROPERTY);
return value != null ? value.toString() : null;
}
@Override
public void onStartingServices(List services) throws Exception {
addServices(services);
}
@Override
public List getServices() {
return services;
}
@Override
public void addService(Service service) {
if (!services.contains(service)) {
services.add(service);
}
}
@Override
public void warmUp() {
getServices().clear();
}
/**
* Do not invoke this method directly, use {@link org.apache.camel.spi.RouteController#startRoute(String)} to start a route.
*/
@Override
public void start() {
super.start();
}
/**
* Do not invoke this method directly, use {@link org.apache.camel.spi.RouteController#stopRoute(String)} to stop a route.
*/
@Override
public void stop() {
super.stop();
}
/**
* Strategy method to allow derived classes to lazily load services for the route
*/
protected void addServices(List services) throws Exception {
}
@Override
protected void doStart() throws Exception {
startDate = new Date();
}
@Override
protected void doStop() throws Exception {
// and clear start date
startDate = null;
}
@Override
protected void doShutdown() throws Exception {
// and clear start date
startDate = null;
// clear services when shutting down
services.clear();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy