All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.beta.dropwizard.ManagedCamelContext Maven / Gradle / Ivy

Go to download

Experimental code that might eventually move into kiwi, or just to try something out...

There is a newer version: 2.2.2
Show newest version
package org.kiwiproject.beta.dropwizard;

import static org.kiwiproject.base.KiwiPreconditions.requireNotBlank;
import static org.kiwiproject.base.KiwiPreconditions.requireNotNull;
import static org.kiwiproject.logging.LazyLogParameterSupplier.lazy;

import com.google.common.annotations.Beta;
import io.dropwizard.lifecycle.Managed;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.Route;

/**
 * Provides a Dropwizard {@link Managed} to start and stop a
 * {@link CamelContext} as your application is started and
 * stopped, respectively.
 */
@Beta
@Slf4j
public class ManagedCamelContext implements Managed {

    private final String name;
    private final CamelContext camelContext;

    /**
     * Creates a new instance with a default name.
     */
    public ManagedCamelContext(CamelContext camelContext) {
        this("camel-context-" + System.currentTimeMillis(), camelContext);
    }

    /**
     * Creates a new instance with the given name to be used in log messages.
     */
    public ManagedCamelContext(String name, CamelContext camelContext) {
        this.name = requireNotBlank(name);
        this.camelContext = requireNotNull(camelContext);
    }

    @Override
    public void start() {
        LOG.info("Starting Camel context {}", name);
        camelContext.start();

        LOG.info("Started Camel context {} has {} routes. Route IDs: {}",
                name,
                lazy(camelContext::getRoutesSize),
                lazy(() -> camelContext.getRoutes().stream().map(Route::getId).toList()));
    }

    @Override
    public void stop() {
        LOG.info("Stopping Camel context {}", name);
        camelContext.stop();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy