...
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\incident-123.txt Claus Ibsen |
Lessons learned
Okay I wanted to demonstrate how you can be in 100% control of the configuration and usage of Camel based on plain Java code with no hidden magic or special XML or other configuration files. Just add the camel-core.jar and you are ready to go.
...
You must have noticed that the code for sending a message to a given endpoint is the same for both the log and file, in fact any Camel endpoint. You as the client shouldn't bother with component specific code such as file stuff for file components, jms stuff for JMS messaging etc. This is what the Message Endpoint EIP pattern is all about and Camel solves this very very nice - a key pattern in Camel.
Reducing code lines
Now that you have been introduced to Camel and one of its masterpiece patterns solved elegantly with the Message Endpoint its time to give productive and show a solution in fewer code lines, in fact we can get it down to 5, 4, 3, 2 .. yes only 1 line of code.
The key is the ProducerTemplate that is a Spring'ish xxxTemplate based producer. Meaning that it has methods to send messages to any Camel endpoints. First of all we need to get hold of such a template and this is done from the CamelContext
Code Block | ||||
---|---|---|---|---|
| ||||
private ProducerTemplate template;
public ReportIncidentEndpointImpl() throws Exception {
...
// 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();
}
|
Now we can use template for sending payloads to any endpoint in Camel. So all the logging gabble can be reduced to:
Code Block | ||||
---|---|---|---|---|
| ||||
template.sendBody("log:com.mycompany.part2.easy", name);
|
And the same goes for the file, but we must also send the header to instruct what the output filename should be:
Code Block | ||||
---|---|---|---|---|
| ||||
String filename = "easy-incident-" + incidentId + ".txt";
template.sendBodyAndHeader("file://target/subfolder", name, FileComponent.HEADER_FILE_NAME, filename);
|