LibraryLink ToToggle FramesPrintFeedback

Implementing the Exchange Interface

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 class CustomExchange extends DefaultExchange { 1

    public CustomExchange(CamelContext camelContext, ExchangePattern pattern) { 2
        super(camelContext, pattern);
        // Set other member variables...
    }

    public CustomExchange(CamelContext camelContext) { 3
        super(camelContext);
        // Set other member variables...
    }

    public CustomExchange(DefaultExchange parent) { 4
        super(parent);
        // Set other member variables...
    }

    @Override
    public Exchange newInstance() { 5
        Exchange e = new CustomExchange(this);
        // Copy custom member variables from current instance...
        return e;
    }

    @Override
    protected Message createInMessage() { 6
        return new CustomMessage();
    }

    @Override
    protected Message createOutMessage() {
        return new CustomMessage();
    }

    @Override
    protected Message createFaultMessage() {
        return new CustomMessage();
    }

    @Override
    protected void configureMessage(Message message) { 7
        super.configureMessage(message);
        // Perform custom message configuration...
    }
}

1

Implements a custom exchange class, CustomExchange, by extending the org.apache.camel.impl.DefaultExchange class.

2

You usually need a constructor that lets you specify the exchange pattern explicitly, as shown here.

3

This constructor, taking only a CamelContext argument, context, implicitly sets the exchange pattern to InOnly as defined in the DefaultExchange constructor.

4

This constructor copies the exchange pattern and the unit of work from the specified exchange object, parent.

5

The newInstance() method is called from inside the DefaultExchange.copy() method. Customization of the newInstance() method should focus on copying all of the custom properties of the current exchange instance into the new exchange instance. The DefaultExchange.copy() method copies the generic exchange properties (by calling copyFrom()).

6

(Optional) Only needed if you implement a custom message type. The createInMessage(), createOutMessage(), and createFaultMessage() methods are implemented to support lazy message creation when you are using a custom message type. In this example, createInMessage() returns a message of CustomMessage type. When a new In message is created by a call to getIn(), the default getIn() implementation calls createInMessage() to create the new message.

7

In the body of configureMessage() you can put code to configure all message types (In, Out, and Fault). The DefaultExchange class uses configureMessage() to configure a message whenever you call setIn(), setOut(), or setFault(), and whenever a message is created by lazy instantiation.

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) { 1
        super(camelContext, pattern);
        setIn(new FileMessage(file));
        this.file = file;
    }

    public FileExchange(DefaultExchange parent, File file) { 2
        super(parent);
        this.file = file;
    }

    public File getFile() { 3
        return this.file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public Exchange newInstance() { 4
        return new FileExchange(this, getFile());
    }
}

1

In addition to letting you specify the Camel context, camelContext, and the exchange pattern, pattern, this constructor also specifies the custom property, file.

2

This constructor gets called by the newInstance() method. This constructor copies the unit of work and the exchange pattern from parent (implemented by the super-constructor) and initializes the file property with the specified value.

3

The getFile() and setFile() methods access the file property, which represents the file from which the exchange object reads the In message.

4

The newInstance() method is overridden to ensure that the DefaultExchange.copy() method works properly. The form of constructor called here ensures that the file property gets copied into the new instance.