Chapter 49. Exchange Interface


Abstract

This chapter describes the Exchange interface. Since the refactoring of the camel-core module performed in Apache Camel 2.0, there is no longer any necessity to define custom exchange types. The DefaultExchange implementation can now be used in all cases.

49.1. The Exchange Interface

Overview

An instance of org.apache.camel.Exchange type encapsulates the current message passing through a route, with additional metadata encoded as exchange properties.
Figure 49.1, “Exchange Inheritance Hierarchy” shows the inheritance hierarchy for the exchange type. The default implementation, DefaultExchange, is always used.

Figure 49.1. Exchange Inheritance Hierarchy

Exchange inheritance hierarchy

The Exchange interface

Example 49.1, “Exchange Interface” shows the definition of the org.apache.camel.Exchange interface.

Example 49.1. Exchange Interface

package org.apache.camel;

import java.util.Map;

import org.apache.camel.spi.Synchronization;
import org.apache.camel.spi.UnitOfWork;

public interface Exchange {
    // Exchange property names (string constants)
    // (Not shown here)
    ...

    ExchangePattern getPattern();
    void setPattern(ExchangePattern pattern);

    Object getProperty(String name);
    Object getProperty(String name, Object defaultValue);
    <T> T  getProperty(String name, Class<T> type);
    <T> T  getProperty(String name, Object defaultValue, Class<T> type);
    void   setProperty(String name, Object value);
    Object removeProperty(String name);
    Map<String, Object> getProperties();
    boolean hasProperties();

    Message getIn();
    <T> T   getIn(Class<T> type);
    void    setIn(Message in);

    Message getOut();
    <T> T   getOut(Class<T> type);
    void    setOut(Message out);
    boolean hasOut();

    Throwable getException();
    <T> T     getException(Class<T> type);
    void      setException(Throwable e);

    boolean isFailed();

    boolean isTransacted();

    boolean isRollbackOnly();

    CamelContext getContext();

    Exchange copy();

    Endpoint getFromEndpoint();
    void     setFromEndpoint(Endpoint fromEndpoint);

    String getFromRouteId();
    void   setFromRouteId(String fromRouteId);

    UnitOfWork getUnitOfWork();
    void setUnitOfWork(UnitOfWork unitOfWork);

    String getExchangeId();
    void setExchangeId(String id);

    void addOnCompletion(Synchronization onCompletion);
    void handoverCompletions(Exchange target);
}

Exchange methods

The Exchange interface defines the following methods:
  • getPattern(), setPattern()—The exchange pattern can be one of the values enumerated in org.apache.camel.ExchangePattern. The following exchange pattern values are supported:
    • InOnly
    • RobustInOnly
    • InOut
    • InOptionalOut
    • OutOnly
    • RobustOutOnly
    • OutIn
    • OutOptionalIn
  • setProperty(), getProperty(), getProperties(), removeProperty(), hasProperties()—Use the property setter and getter methods to associate named properties with the exchange instance. The properties consist of miscellaneous metadata that you might need for your component implementation.
  • setIn(), getIn()—Setter and getter methods for the In message.
    The getIn() implementation provided by the DefaultExchange class implements lazy creation semantics: if the In message is null when getIn() is called, the DefaultExchange class creates a default In message.
  • setOut(), getOut(), hasOut()—Setter and getter methods for the Out message.
    The getOut() method implicitly supports lazy creation of an Out message. That is, if the current Out message is null, a new message instance is automatically created.
  • setException(), getException()—Getter and setter methods for an exception object (of Throwable type).
  • isFailed()—Returns true, if the exchange failed either due to an exception or due to a fault.
  • isTransacted()—Returns true, if the exchange is transacted.
  • isRollback()—Returns true, if the exchange is marked for rollback.
  • getContext()—Returns a reference to the associated CamelContext instance.
  • copy()—Creates a new, identical (apart from the exchange ID) copy of the current custom exchange object. The body and headers of the In message, the Out message (if any), and the Fault message (if any) are also copied by this operation.
  • setFromEndpoint(), getFromEndpoint()—Getter and setter methods for the consumer endpoint that orginated this message (which is typically the endpoint appearing in the from() DSL command at the start of a route).
  • setFromRouteId(), getFromRouteId()—Getters and setters for the route ID that originated this exchange. The getFromRouteId() method should only be called internally.
  • setUnitOfWork(), getUnitOfWork()—Getter and setter methods for the org.apache.camel.spi.UnitOfWork bean property. This property is only required for exchanges that can participate in a transaction.
  • setExchangeId(), getExchangeId()—Getter and setter methods for the exchange ID. Whether or not a custom component uses and exchange ID is an implementation detail.
  • addOnCompletion()—Adds an org.apache.camel.spi.Synchronization callback object, which gets called when processing of the exchange has completed.
  • handoverCompletions()—Hands over all of the OnCompletion callback objects to the specified exchange object.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.