Example 9.2 outlines how to implement an exchange by
extending the DefaultExchange class.
Example 9.2. Custom Exchange Implementation
import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; import org.apache.camel.impl.DefaultExchange; public classCustomExchangeextends DefaultExchange {public
CustomExchange(CamelContext camelContext, ExchangePattern pattern) {super(camelContext, pattern); // Set other member variables... } public
CustomExchange(CamelContext camelContext) {super(camelContext); // Set other member variables... } public
CustomExchange(DefaultExchange parent) {super(parent); // Set other member variables... } @Override public Exchange newInstance() {
Exchange e = new
CustomExchange(this); // Copy custom member variables from current instance... return e; } @Override protected Message createInMessage() {return new
CustomMessage(); } @Override protected Message createOutMessage() { return newCustomMessage(); } @Override protected Message createFaultMessage() { return newCustomMessage(); } @Override protected void configureMessage(Message message) {super.configureMessage(message); // Perform custom message configuration... } }
Implements a custom exchange class, | |
You usually need a constructor that lets you specify the exchange pattern explicitly, as shown here. | |
This constructor, taking only a | |
This constructor copies the exchange pattern and the unit of work from the specified
exchange object, | |
The | |
(Optional) Only needed if you implement a custom message type.
The | |
In the body of |
Example 9.3 shows the implementation of the
FileExchange class, which is taken from the FUSE Mediation Router file component
implementation. The FileExchange implementation is characterised by two things:
It has an additional file property, which references the file containing the
In message,
It only supports the InOnly exchange pattern.
Example 9.3. FileExchange Implementation
package org.apache.camel.component.file;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.impl.DefaultExchange;
import java.io.File;
public class FileExchange extends DefaultExchange {
private File file;
public FileExchange(CamelContext camelContext, ExchangePattern pattern, File file) {
super(camelContext, pattern);
setIn(new FileMessage(file));
this.file = file;
}
public FileExchange(DefaultExchange parent, File file) {
super(parent);
this.file = file;
}
public File getFile() {
return this.file;
}
public void setFile(File file) {
this.file = file;
}
public Exchange newInstance() {
return new FileExchange(this, getFile());
}
}In addition to letting you specify the Camel context, | |
This constructor gets called by the | |
The | |
The |