Part 3
TODO: We use a Polling Consumer and not an Event Driven Consumer!!!
Recap
Lets just recap on the solution we have now:
...
The consumer needs to be consuming from an endpoint so we grab the endpoint from Camel we want to consume. It's file://target/subfolder
. Don't be fooled this endpoint doesn't have to 100% identical to the producer, i.e. the endpoint we used in the previous part to create and store the files. We could change the URL to include some options, and to make it more clear that it's possible we setup a delay value to 10 seconds, and the first poll starts after 2 seconds. This is done by adding ?consumer.delay=10000&consumer.initialDelay=2000
to the URL.
Tip | ||
---|---|---|
| ||
The URL configuration in Camel endpoints is just like regular URL we know from the Internet. You use ? and & to set the options. |
...
Note | ||
---|---|---|
| ||
Why don't we just cast it as we always do in Java? Well the biggest advantage when you provide the type as a parameter you tell Camel what type you want and Camel can automatically convert it for you, using its flexible Type Converter mechanism. This is a great advantage, and you should try to use this instead of regular type casting. |
...
Code Block | ||||
---|---|---|---|---|
| ||||
private void addMailSendConsumer() throws Exception { // getGrab the endpoint where we wantshould toconsume. consume,Option we- willthe consumefirst allpoll filesstarts inafter this2 folderseconds Endpoint endpint = camel.getEndpoint("file://target/subfolder?consumer.delayinitialDelay=100002000"); // create the event driven consumer // the Processor is the code what should happen when there is an event // (think it as the onMessage method) Consumer consumer = endpint.createConsumer(new Processor() { public void process(Exchange exchange) throws Exception { // get the mail body as a String String mailBody = exchange.getIn().getBody(String.class); // okay now we are read to send it as an email System.out.println("Sending email..." + mailBody); } }); // star the consumer, it will listen for files consumer.start(); } |
Before we test it we need to be aware that our unit test is only catering for the first part of the solution, receiving the message with webservice, transforming it using Velocity and then storing it as a file - it doesn't test the Event Driven Consumer we just added. As we are eager to see it in action, we just do a common trick adding some sleep in our unit test, that gives our Event Driven Consumer time to react and print to System.out. We will later refine the test:
Code Block | ||||
---|---|---|---|---|
| ||||
public void testRendportIncident() throws Exception { ... OutputReportIncident out = client.reportIncident(input); assertEquals("Response code is wrong", "OK", out.getCode()); // give the event driven consumer time to react Thread.sleep(5000); } |
We run the test with mvn clean test
and have eyes fixed on the console output.
During all the output in the console, we see that our consumer has been triggered, as we want.
Code Block |
---|
2008-07-19 12:09:24,140 [mponent@1f12c4e] DEBUG FileProcessStrategySupport - Locking the file: target\subfolder\mail-incident-123.txt using the lock file name: D:\demo\part-three\target\subfolder\mail-incident-123.txt.cameLock
Sending email...Incident 123 has been reported on the 2008-07-16 by Claus Ibsen.
The person can be contact by:
- email: davsclaus@apache.org
- phone: +45 2962 7576
Summary: bla bla
Details:
more bla bla
This is an auto generated email. You can not reply.
2008-07-19 12:09:24,156 [mponent@1f12c4e] DEBUG FileConsumer - Done processing file: target\subfolder\mail-incident-123.txt. Status is: OK
|