...
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:
...
This can also be reduced. All the standard components in Camel is auto disconvered discovered on-the-fly so we can remove these code lines and we are down to 3 lines.
Info | ||
---|---|---|
| ||
When an endpoint is requested with a scheme that Camel hasn't seen before it will try to look for it in the classpath. It will do so by looking for special Camel component marker files that reside in the folder
The class property defines the component implementation. Tip: End-users can create their 3rd party components using the same technique and have them been auto discovered on-the-fly. |
Okay back to the 3 code lines:
Code Block | ||||
---|---|---|---|---|
| ||||
camel = new DefaultCamelContext(); template = camel.createProducerTemplate(); camel.start(); |
...