Part 2
TODO: links to Camel documentation for Log and File component
Adding Camel
In this part we will introduce Camel so we start by adding Camel to our pom.xml:
...
Here at first we want Camel to log the givenName and familyName parameters we receive, so we add the LogComponent
with the key log. And we must start Camel before its ready to act.
Tip | ||
---|---|---|
| ||
The Log and File components is documented as well, just click on the links. Just return to this documentation later when you must use these components for real. |
Then we change the code in the method that is invoked by Apache CXF when a webservice request arrives. We get the name and let Camel have a go at it in the new method we create sendToCamel:
...
Code Block | ||||
---|---|---|---|---|
| ||||
private void sendToCamelLog(String name) { try { // get the log component Component component = camel.getComponent("log"); // create an endpoint and configure it. // Notice the URI parameters this is a common pratice in Camel to configure // endpoints based on URI. // com.mycompany.part2 = the log category used. Will log at INFO level as default Endpoint endpoint = component.createEndpoint("log:com.mycompany.part2"); // create an Exchange that we want to send to the endpoint Exchange exchange = endpoint.createExchange(); // set the in message payload (=body) with the name parameter exchange.getIn().setBody(name); // now we want to send the exchange to this endpoint and we then need a producer // for this, so we create and start the producer. Producer producer = endpoint.createProducer(); producer.start(); // process the exchange will send the exchange to the log component, that will process // the exchange and yes log the payload producer.process(exchange); // stop the producer, we want to be nice and cleanup producer.stop(); } catch (Exception e) { // we ignore any exceptions and just rethrow as runtime throw new RuntimeException(e); } } |
Okay there are code comments in the code block above that should explain what is happening. We run the code by invoking our unit test with maven mvn test
, and we should get this log line:
...
Code Block |
---|
D:\demo\part-two>type target\subfolder\mail-incident-123.txt 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. |
First part of the solution
What we have seen here is actually what it takes to build the first part of the integration flow. Receiving a request from a webservice, transform it to a mail body and store it to a file, and return an OK response to the webservice. All possible within 10 lines of code. So lets wrap it up here is what it takes:
Code Block | ||||
---|---|---|---|---|
| ||||
/**
* The webservice we have implemented.
*/
public class ReportIncidentEndpointImpl implements ReportIncidentEndpoint {
private CamelContext camel;
private ProducerTemplate template;
public ReportIncidentEndpointImpl() throws Exception {
// create the camel context that is the "heart" of Camel
camel = new DefaultCamelContext();
// get the ProducerTemplate thst is a Spring'ish xxxTemplate based producer for very
// easy sending exchanges to Camel.
template = camel.createProducerTemplate();
// start Camel
camel.start();
}
public OutputReportIncident reportIncident(InputReportIncident parameters) {
// transform the request into a mail body
Object mailBody = template.sendBody("velocity:MailBody.vm", parameters);
// store the mail body in a file
String filename = "mail-incident-" + parameters.getIncidentId() + ".txt";
template.sendBodyAndHeader("file://target/subfolder", mailBody, FileComponent.HEADER_FILE_NAME, filename);
// return an OK reply
OutputReportIncident out = new OutputReportIncident();
out.setCode("OK");
return out;
}
}
|
Okay I missed by one, its in fact only 9 lines of java code and 2 fields.
End of part 2
I know this is a bit different introduction to Camel to how you can start using it in your projects just as a plain java .jar framework that isn't invasive at all. I took you through the coding parts that requires 6 - 10 lines to send a message to an endpoint, buts it's important to show the Message Endpoint EIP pattern in action and how its implemented in Camel. Yes of course Camel also has to one liners that you can use, and will use in your projects for sending messages to endpoints. This part has been about good old plain java, nothing fancy with Spring, XML files, auto discovery, OGSi or other new technologies. I wanted to demonstrate the basic building blocks in Camel and how its setup in pure god old fashioned Java. There are plenty of eye catcher examples with one liners that does more than you can imagine - we will come there in the later parts.
Okay part 3 is about building the last pieces of the solution and now it gets interesting since we have to wrestle with the event driven consumer.
Brew a cup of coffee, tug the kids and kiss the wife, for now we will have us some fun with the Camel. See you in part 3.