Skip to content

Instantly share code, notes, and snippets.

@Steve973
Last active November 13, 2023 21:56
Show Gist options
  • Save Steve973/8c1288f09900c1d8eab754011f7df4fd to your computer and use it in GitHub Desktop.
Save Steve973/8c1288f09900c1d8eab754011f7df4fd to your computer and use it in GitHub Desktop.
Test snippet where I am trying to load YAML routes
private void loadRoute(String name) {
try (CamelYamlRoutesBuilderLoader loader = new CamelYamlRoutesBuilderLoader();
DefaultResourceResolvers.ClasspathResolver resolver = new DefaultResourceResolvers.ClasspathResolver()) {
resolver.setCamelContext(camelContext);
Resource resource = resolver.resolve("classpath:" + name);
loader.loadRoutesBuilder(resource);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dynamic-router" class="org.apache.camel.component.dynamicrouter.DynamicRouterComponent"/>
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring" />
</beans>
package org.apache.camel.component.dynamicrouter.integration;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.dsl.yaml.CamelYamlRoutesBuilderLoader;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesBuilderLoader;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.test.spring.junit5.CamelSpringTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import java.util.concurrent.TimeUnit;
@CamelSpringTest
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class DynamicRouterYamlRoutesIT {
@Autowired
CamelContext camelContext;
@EndpointInject("mock:result-1")
MockEndpoint mockOne;
@EndpointInject("mock:result-2")
MockEndpoint mockTwo;
@Test
public void testYamlRoutes() throws Exception {
mockOne.setExpectedCount(1);
mockTwo.setExpectedCount(1);
Resource yamlResource = ResourceHelper.resolveResource(camelContext, "classpath:predicate-body-test-routes.yaml");
try (RoutesBuilderLoader yamlRoutesBuilderLoader = new CamelYamlRoutesBuilderLoader()) {
RoutesBuilder yamlRoutesBuilder = yamlRoutesBuilderLoader.loadRoutesBuilder(yamlResource);
yamlRoutesBuilder.addRoutesToCamelContext(camelContext);
}
camelContext.start();
MockEndpoint.assertIsSatisfied(camelContext, 10, TimeUnit.SECONDS);
}
}
- route:
id: "subscribe-route-1"
from:
uri: "direct://subscribe-route-1"
steps:
- set-body:
groovy: >
return org.apache.camel.builder.PredicateBuilder.toPredicate(new org.apache.camel.model.language.SimpleExpression('${header.n} == "1"'));
- to:
uri: "dynamic-router:control/subscribe/testrouter"
parameters:
subscription-id: "subscription-1"
priority: 1
destination-uri: "direct:route-1"
- route:
id: "subscribe-route-2"
from:
uri: "direct://subscribe-route-2"
steps:
- set-body:
groovy: >
return org.apache.camel.builder.PredicateBuilder.toPredicate(new org.apache.camel.model.language.SimpleExpression('${header.n} == "2"'));
- to:
uri: "dynamic-router:control/subscribe/testrouter"
parameters:
subscription-id: "subscription-2"
priority: 1
destination-uri: "direct:route-2"
- route:
id: "route-1"
from:
uri: "direct:route-1"
steps:
- to:
uri: "mock:result-1"
- route:
id: "route-2"
from:
uri: "direct:route-2"
steps:
- to:
uri: "mock:result-2"
- route:
id: "subscribe-route"
from:
timer:
name: "start-subscribe"
delay: 0
repeat-count: 1
steps:
- to: "direct:subscribe-route-1"
- to: "direct:subscribe-route-2"
- route:
id: "main-route"
from:
timer:
name: "start"
delay: 1
repeat-count: 1
steps:
- setBody:
constant: "testbody"
- setHeader:
name: n
constant: "1"
- to: "dynamic-router:testrouter?recipientMode=allMatch"
- setHeader:
name: n
constant: "2"
- to: "dynamic-router:testrouter?recipientMode=allMatch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment