You can implement a producer in one of the following ways:
Example 9.4 outlines how to implement a synchronous
producer. In this case, call to Producer.process()
blocks until a
reply is received.
Example 9.4. DefaultProducer Implementation
import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.Producer; import org.apache.camel.impl.DefaultProducer; public classCustomProducer
extends DefaultProducer {public
CustomProducer
(Endpoint endpoint) {super(endpoint); // Perform other initialization tasks... } public void process(Exchange exchange) throws Exception {
// Process exchange synchronously. // ... } }
Implement a custom synchronous producer class,
| |
Implement a constructor that takes a reference to the parent endpoint. | |
The
|
Example 9.5 outlines how to implement an
asynchronous producer. In this case, you must implement both a synchronous
process()
method and an asynchronous process()
method (which
takes an additional AsyncCallback
argument).
Example 9.5. CollectionProducer Implementation
import org.apache.camel.AsyncCallback; import org.apache.camel.AsyncProcessor; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.Producer; import org.apache.camel.impl.DefaultProducer; public classCustomProducer
extends DefaultProducer implements AsyncProcessor {public
CustomProducer
(Endpoint endpoint) {super(endpoint); // ... } public void process(Exchange exchange) throws Exception {
// Process exchange synchronously. // ... } public boolean process(Exchange exchange, AsyncCallback callback) {
// Process exchange asynchronously.
CustomProducer
Task task = newCustomProducer
Task(exchange, callback); // Process 'task' in a separate thread... // ... return false;} } public class
CustomProducer
Task implements Runnable {private Exchange exchange; private AsyncCallback callback; public
CustomProducer
Task(Exchange exchange, AsyncCallback callback) { this.exchange = exchange; this.callback = callback; } public void run() {// Process exchange. // ... callback.done(false); } }
Implement a custom asynchronous producer class,
| |
Implement a constructor that takes a reference to the parent endpoint. | |
Implement the synchronous | |
Implement the asynchronous | |
Normally, you return | |
The | |
The |