This commit is contained in:
parent
b9c8a43ff0
commit
795f39d238
@ -19,10 +19,29 @@
|
|||||||
<artifactId>validation-api</artifactId>
|
<artifactId>validation-api</artifactId>
|
||||||
<version>${javax.validation.validation-api.version}</version>
|
<version>${javax.validation.validation-api.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-core</artifactId>
|
||||||
|
<version>${camel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-test-junit5</artifactId>
|
||||||
|
<version>${camel.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-main</artifactId>
|
||||||
|
<version>${camel.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
||||||
|
<camel.version>4.3.0</camel.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.dynamicrouter;
|
||||||
|
|
||||||
|
import org.apache.camel.ExchangeProperties;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DynamicRouterBean {
|
||||||
|
public String route(String body, @ExchangeProperties Map<String, Object> properties) {
|
||||||
|
int invoked = 0;
|
||||||
|
Integer current = (Integer) properties.get("invoked");
|
||||||
|
if (current != null) {
|
||||||
|
invoked = current;
|
||||||
|
}
|
||||||
|
invoked++;
|
||||||
|
properties.put("invoked", invoked);
|
||||||
|
|
||||||
|
if (body.equalsIgnoreCase("mock") && invoked == 1) {
|
||||||
|
return "mock:dynamicRouter";
|
||||||
|
} else if (body.equalsIgnoreCase("direct") && invoked == 1) {
|
||||||
|
return "mock:directDynamicRouter";
|
||||||
|
} else if (body.equalsIgnoreCase("seda") && invoked == 1) {
|
||||||
|
return "mock:sedaDynamicRouter";
|
||||||
|
} else if (body.equalsIgnoreCase("book") && invoked == 1) {
|
||||||
|
return "mock:bookDynamicRouter";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.dynamicrouter;
|
||||||
|
|
||||||
|
import org.apache.camel.builder.RouteBuilder;
|
||||||
|
|
||||||
|
public class DynamicRouterRoute extends RouteBuilder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure() {
|
||||||
|
|
||||||
|
from("direct:dynamicRouter").dynamicRouter(method(DynamicRouterBean.class, "route"));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package dynamicrouter;
|
||||||
|
|
||||||
|
import com.baeldung.dynamicrouter.DynamicRouterRoute;
|
||||||
|
import org.apache.camel.RoutesBuilder;
|
||||||
|
import org.apache.camel.component.mock.MockEndpoint;
|
||||||
|
import org.apache.camel.test.junit5.CamelTestSupport;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class DynamicRouterRouteUnitTest extends CamelTestSupport {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RoutesBuilder createRouteBuilder() {
|
||||||
|
return new DynamicRouterRoute();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndMockAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = getMockEndpoint("mock:dynamicRouter");
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("mock"));
|
||||||
|
context.start();
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndDirectAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = context.getEndpoint("mock:directDynamicRouter", MockEndpoint.class);
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("direct"));
|
||||||
|
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndSedaAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = context.getEndpoint("mock:sedaDynamicRouter", MockEndpoint.class);
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("seda"));
|
||||||
|
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndBookAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = getMockEndpoint("mock:bookDynamicRouter");
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("book"));
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.camel.apache.dynamicrouting;
|
||||||
|
|
||||||
|
public class DynamicRouteBuilder {
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.camel.apache.dynamicrouting;
|
||||||
|
|
||||||
|
public class DynamicRouterBean {
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.apache.dynamicrouter;
|
||||||
|
|
||||||
|
public class DynamicRouterUnitTest {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user