Search in sources :

Example 76 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class RouteBuilder method fromF.

/**
     * Creates a new route from the given URI input
     *
     * @param uri  the String formatted from uri
     * @param args arguments for the string formatting of the uri
     * @return the builder
     */
public RouteDefinition fromF(String uri, Object... args) {
    getRouteCollection().setCamelContext(getContext());
    RouteDefinition answer = getRouteCollection().from(String.format(uri, args));
    configureRoute(answer);
    return answer;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition)

Example 77 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class RouteBuilder method checkInitialized.

// Implementation methods
// -----------------------------------------------------------------------
@SuppressWarnings("deprecation")
protected void checkInitialized() throws Exception {
    if (initialized.compareAndSet(false, true)) {
        // Set the CamelContext ErrorHandler here
        ModelCamelContext camelContext = getContext();
        if (camelContext.getErrorHandlerBuilder() != null) {
            setErrorHandlerBuilder(camelContext.getErrorHandlerBuilder());
        }
        configure();
        // a route builder prepares the route definitions correctly already
        for (RouteDefinition route : getRouteCollection().getRoutes()) {
            route.markPrepared();
        }
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 78 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class ReloadStrategySupport method onReloadXml.

@Override
public void onReloadXml(CamelContext camelContext, String name, InputStream resource) {
    log.debug("Reloading routes from XML resource: {}", name);
    Document dom;
    String xml;
    try {
        xml = camelContext.getTypeConverter().mandatoryConvertTo(String.class, resource);
        // the JAXB model expects the spring namespace (even for blueprint)
        dom = XmlLineNumberParser.parseXml(new ByteArrayInputStream(xml.getBytes()), null, "camelContext,routes", "http://camel.apache.org/schema/spring");
    } catch (Exception e) {
        failed++;
        log.warn("Cannot load the resource " + name + " as XML");
        return;
    }
    ResourceState state = cache.get(name);
    if (state == null) {
        state = new ResourceState(name, dom, xml);
        cache.put(name, state);
    }
    String oldXml = state.getXml();
    List<Integer> changed = StringHelper.changedLines(oldXml, xml);
    // find all <route> which are the routes
    NodeList list = dom.getElementsByTagName("route");
    // collect which routes are updated/skipped
    List<RouteDefinition> routes = new ArrayList<>();
    if (list != null && list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            // what line number are this within
            String lineNumber = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER);
            String lineNumberEnd = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
            if (lineNumber != null && lineNumberEnd != null && !changed.isEmpty()) {
                int start = Integer.valueOf(lineNumber);
                int end = Integer.valueOf(lineNumberEnd);
                boolean within = withinChanged(start, end, changed);
                if (within) {
                    log.debug("Updating route in lines: {}-{}", start, end);
                } else {
                    log.debug("No changes to route in lines: {}-{}", start, end);
                    continue;
                }
            }
            try {
                // load from XML -> JAXB model and store as routes to be updated
                RoutesDefinition loaded = ModelHelper.loadRoutesDefinition(camelContext, node);
                if (!loaded.getRoutes().isEmpty()) {
                    routes.addAll(loaded.getRoutes());
                }
            } catch (Exception e) {
                failed++;
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }
    }
    if (!routes.isEmpty()) {
        try {
            boolean unassignedRouteIds = false;
            CollectionStringBuffer csb = new CollectionStringBuffer(",");
            // collect route ids and force assign ids if not in use
            for (RouteDefinition route : routes) {
                unassignedRouteIds |= route.hasCustomIdAssigned();
                String id = route.idOrCreate(camelContext.getNodeIdFactory());
                csb.append(id);
            }
            log.debug("Reloading routes: [{}] from XML resource: {}", csb, name);
            if (unassignedRouteIds) {
                log.warn("Routes with no id's detected. Its recommended to assign id's to your routes so Camel can reload the routes correctly.");
            }
            // update the routes (add will remove and shutdown first)
            camelContext.addRouteDefinitions(routes);
            log.info("Reloaded routes: [{}] from XML resource: {}", csb, name);
        } catch (Exception e) {
            failed++;
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    // update cache
    state = new ResourceState(name, dom, xml);
    cache.put(name, state);
    succeeded++;
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) RouteDefinition(org.apache.camel.model.RouteDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition)

Example 79 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class AdviceWithWeaveByToUriCBRTest method testAdviceCBR.

public void testAdviceCBR() throws Exception {
    RouteDefinition route = context.getRouteDefinitions().get(0);
    route.adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            weaveByToUri("direct:branch*").replace().to("mock:foo");
            mockEndpointsAndSkip("direct:branch*");
        }
    });
    getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 80 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class AdviceWithIssueTest method testAdviceWithInterceptFrom.

public void testAdviceWithInterceptFrom() throws Exception {
    RouteDefinition route = context.getRouteDefinitions().get(0);
    route.adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptFrom().to("mock:from");
        }
    });
    getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
    getMockEndpoint("mock:from").expectedBodiesReceived("World");
    getMockEndpoint("mock:from").expectedHeaderReceived(Exchange.INTERCEPTED_ENDPOINT, "direct://start");
    template.sendBody("direct:start", "World");
    assertMockEndpointsSatisfied();
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Aggregations

RouteDefinition (org.apache.camel.model.RouteDefinition)102 AdviceWithRouteBuilder (org.apache.camel.builder.AdviceWithRouteBuilder)17 RouteBuilder (org.apache.camel.builder.RouteBuilder)17 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 FromDefinition (org.apache.camel.model.FromDefinition)9 HashMap (java.util.HashMap)6 Processor (org.apache.camel.Processor)6 ConnectException (java.net.ConnectException)5 Exchange (org.apache.camel.Exchange)5 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)5 IOException (java.io.IOException)4 Map (java.util.Map)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)4 HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)4 HystrixDefinition (org.apache.camel.model.HystrixDefinition)4 LogDefinition (org.apache.camel.model.LogDefinition)4 ModelCamelContext (org.apache.camel.model.ModelCamelContext)4