This example demonstrates how you might use the Dynamic Router EIP component in a Spring Boot application. There are eleven routing participants that subscribe to a "numbers" channel of the Dynamic Router. The message includes:
-
A Predicate that examines the Exchange to determine if an Exchange is appropriate for the participant
-
A destination URI where the Dynamic Router will send Exchanges that match a participant’s Predicate
Name | Priority | Description |
---|---|---|
Tens |
1 |
Accepts multiples of 10. |
Nines |
2 |
Accepts multiples of 9. |
Eights |
3 |
Accepts multiples of 8. |
Sevens |
4 |
Accepts multiples of 7. |
Sixes |
5 |
Accepts multiples of 6. |
Fives |
6 |
Accepts multiples of 5. |
Fours |
7 |
Accepts multiples of 4. |
Threes |
8 |
Accepts multiples of 3. |
Even |
9 |
Accepts even numbers. |
Odd |
100 |
Accepts odd numbers. |
Prime |
10 |
Accepts prime numbers. |
Subscriptions with a lower priority value are evaluated earlier than subscriptions with a higher priority value. For this reason, even though the number "10" is a multiple of 5, it will be consumed by the Tens participant.
All participants inherit from the RoutingParticipant class, so they use a @Consume
annotation to consume from their registered destination URI. They add their received number to the ResultsService.
The NumbersService sends all numbers from one to one million through the Dynamic Router. After the messages have all been sent and processed, it instructs the ResultsService to print the results. It will look something like this:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.2)
INFO 23130 --- [main] o.a.c.e.s.n.service.NumbersService : Subscribing participants
INFO 23130 --- [main] o.a.c.e.s.n.service.NumbersService : Sending messages to the dynamic router
INFO 23130 --- [main] o.a.c.e.s.n.service.NumbersService : Finished
Dynamic Router Spring Boot Numbers Example Results:
odd: 150077
even: 114287
sevens: 101588
tens: 100000
nines: 100000
eights: 88889
primes: 78494
threes: 76191
fives: 76190
sixes: 57142
fours: 57142
Received count: 1000000 in 5709ms
You can run this example using:
$ mvn spring-boot:run
If you want to run the Dynamic Router in "allMatch" receiver mode, run the example with an argument specifying that configuration property:
$ mvn spring-boot:run -Dspring-boot.run.arguments="--camel.spring-boot.example.dynamic-router-eip.recipient-mode=allMatch"
The results should show up in less than ten seconds, and the program will immediately terminate.
You can run the tests using:
$mvn test
There are some demos that run during the test phase. Currently, there are three:
-
AllRecipientsApplicationTest: the recipient mode is set to "allMatch" and 1,000,000 messages are sent through the router.
-
FirstRecipientApplicationTest: the recipient mode is set to "firstMatch" and 1,000,000 messages are sent through the router.
-
LessExpectedApplicationTest: the recipient mode is set to "firstMatch", and 10,000 messages are sent through the router. The difference, here, is that the expected count is set to only 1,000 messages, so the results are calculated and displayed before all 10,000 messages are routed.
If you hit any problem using Camel or have some feedback, then please let us know.
We also love contributors, so please get involved
The Camel riders!