...
NotifyBuilder
Available as of Camel 2.2
The NotifierBuilder
NotifyBuilder
is a builder from the org.apache.camel.builder
package which allows you to build expressions and then test or wait for that condition to occur. The expressions is based around notifications about Exchange being routed. So what does that mean? It means that you can build an expressions which can tell you when Camel is finished with routing 5 messages etc.
...
Now you want to test this route without using mocks or the likes. Imagine the route being more complex and a production ready route.
We want to test that it could process a message send to that queue. By using the NotifierBuilder NotifyBuilder we can build an expression which expresses when that condition occurred.
Code Block | ||||
---|---|---|---|---|
| ||||
NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder().whenDone(1).create(); // now use some API to send a message etc. Maybe you cannot use Camel's ProducerTemplate // now we want to wait until the message has been routed and completed boolean done = notifiernotify.matches(10, TimeUnit.SECONDS); assertTrue("Should be done", done); // now maybe use some API to see that the message did as expected |
This is a very basic example with a simple builder expression. What we said that we want it to match when any Exchange is done. The builder have many more methods to set more complex expressions, which even can be stacked using and, or, not operations.
Methods
These methods is for building the expression:
...
We will most likely add additional methods in the future, so check out the NotifierBuilder
NotifyBuilder
for latest and greatest methods.
Difference between Done and Completed
The difference between done and completed is that done can also include failed messages, where as completed is only successful processed messages.
Examples
Code Block |
---|
NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder(context) .from("direct:foo").whenDone(5) .create(); |
Here we want to match when the direct:foo
endpoint have done 5 messages.
Code Block |
---|
NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder(context) .from("jms:*").whenDone(1) .create(); |
Here we just say that at least one message should be done received from any JMS endpoint (notice the wildcard matching).
Code Block |
---|
NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder(context) .from("direct:foo").whenDone(5) .and().from("direct:bar").whenDone(7) .create(); |
Here both 5 foo messages and 7 bar messages must be done. Notice the use of the and operator.
Code Block |
---|
NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder(context) .from("direct:foo").whenBodiesReceived("Hello World", "Bye World") .create(); |
Here we expect to receive two messages with Hello World
and Bye World
.
Code Block |
---|
NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder(context) .whenAnyReceivedMatches(body().contains("Camel")) .create(); |
...
Code Block |
---|
// lets use a mock to set the expressions as it got many great assertions for that // notice we use mock:assert which does NOT exist in the route, its just a pseudo name MockEndpoint mock = getMockEndpoint("mock:assert"); mock.expectedBodiesReceivedInAnyOrder("Hello World", "Bye World", "Hi World"); NotifierBuilderNotifyBuilder notifiernotify = new NotifierBuilderNotifyBuilder(context) .from("direct:foo").whenReceivedSatisfied(mock) .create(); |
...