Exchange class in Java
An "Exchange" class typically refers to a plain Java class used to encapsulate a message or a data exchange between components (commonly seen in messaging frameworks like Apache Camel). It is not a built-in Java SE class but a design pattern/POJO used to carry payload, metadata, and processing state.
Common responsibilities
- Payload: holds the main message/data (object or byte[]).
- Headers/Properties: stores metadata (key-value pairs).
- Exchange ID: unique identifier for the exchange instance.
- Pattern/Type: indicates message exchange pattern (e.g., InOnly, InOut).
- Attachments: optional binary or additional parts.
- Exception/Status: holds processing exceptions or status flags.
- Routing/Endpoint info: source/destination identifiers.
Typical structure (example)
public class Exchange {
private final String id;
private Object body;
private Map<String, Object> headers = new HashMap<>();
private Exception exception;
public Exchange(String id, Object body) {
this.id = id;
this.body = body;
}
public String getId() { return id; }
public Object getBody() { return body; }
public void setBody(Object body) { this.body = body; }
public Map<String, Object> getHeaders() { return headers; }
public void setHeader(String name, Object value) { headers.put(name, value); }
public Object getHeader(String name) { return headers.get(name); }
public Exception getException() { return exception; }
public void setException(Exception exception) { this.exception = exception; }
}
Top comments (0)