If you need to write code that executes both before and after an exchange is delegated to the next processor, you can implement a delegate processor, as explained in this section. Delegate processors conform to the chaining model for building routes.
Example 2.3 shows a partial outline of the
org.apache.camel.processor.DelegateProcessor class. The main difference
between the DelegateProcessor class and the Processor class is
that the DelegateProcessor class has a bean property, processor,
which holds a reference to the next processor in the chain. This makes it possible for you
to call the next processor explicitly when you write the code for the process()
method. The most convenient way to call the next processor in the chain is to call the
processNext() method.
Example 2.3. DelegateProcessor Class
package org.apache.camel.processor;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.ServiceSupport;
import org.apache.camel.spi.Policy;
import org.apache.camel.util.ServiceHelper;
public class DelegateProcessor extends ServiceSupport implements Processor {
protected Processor processor;
public DelegateProcessor() {
}
public Processor getProcessor() {
return processor;
}
public void setProcessor(Processor processor) {
this.processor = processor;
}
...
public void process(Exchange exchange) throws Exception {
processNext(exchange);
}
...
protected void processNext(Exchange exchange) throws Exception {
if (processor != null) {
processor.process(exchange);
}
}
}Where the setProcessor() method enables the route builder to inject a
reference to the next processor in the chain and the process() method must be
overridden by your custom delegate processor class.
You implement a delegate processor by extending DelegateProcessor and
implementing the process() method. Example 2.4 shows the outline of a delegate processor
implementation.
Example 2.4. Delegate Processor Implementation
// Java
import org.apache.camel.processor.DelegateProcessor;
public class MyDelegateProcessor extends DelegateProcessor {
public MyProcessor() { }
public void process(Exchange exchange) throws Exception
{
// Insert code that gets executed *before* delegating
// to the next processor in the chain.
...
processNext(exchange);
// Insert code that gets executed *after* delegating
// to the next processor in the chain.
...
}
}Where the process() method contains code that gets executed
before delegating to the next processor in the chain, as well as code
that gets executed after delegating. If the processors in your route
are chained according to the chaining model (see Chaining model), this means that you can access request messages before the call to
processNext() and access reply messages after the call.
For examples of how to access the message body and header values inside a simple processor, see Accessing Message Content.
To insert a delegate processor into a route, use the intercept() DSL
command. Create an instance of your custom processor and then pass this instance as an
argument to the intercept() method, as follows:
// Java
org.apache.camel.processor.DelegateProcessor myProc = new MyDelegateProcessor();
from("SourceURL").intercept(myProc).to("TargetURL");