In Spring Integration, what is a channel adapter, and what is the difference between an inbound and an outbound channel adapter?
answer
- Adapter = one-way bridge to external system
- Inbound = MessageSource + poller (or message-driven push)
- Outbound = MessageHandler, returns nothing
- Two-way = gateway, not adapter
- File/JMS-send = adapter; HTTP/TCP = often gateway
basics
~20 sA channel adapter connects an external system to a Spring Integration message channel. Inbound adapters bring external data IN as messages; outbound adapters send message payloads OUT to an external system. Both are one-way (no reply).
solid answer
~40 sA channel adapter is a one-way bridge between an external system (file, JMS, HTTP, TCP, etc.) and a message channel. An inbound channel adapter reads from the outside world and publishes each item as a Spring Message onto a channel; internally it is a MessageSource that a poller polls (or, for message-driven sources like JMS, a listener container pushes). An outbound channel adapter is a MessageHandler that consumes messages from a channel and writes the payload to the external system, returning nothing. The defining trait is directionality plus one-way semantics: no reply flows back. When you need request-reply (send and get an answer), you use a gateway instead of an adapter.
code
java · 24 lines@Configuration
public class FileFlowConfig {
// INBOUND channel adapter: polls a directory, emits each file as a Message
@Bean
public IntegrationFlow inboundFileFlow() {
return IntegrationFlow
.from(Files.inboundAdapter(new File("/data/in"))
.autoCreateDirectory(true),
e -> e.poller(Pollers.fixedDelay(1000)))
.channel("filesChannel")
.get();
}
// OUTBOUND channel adapter: writes payloads out to disk, returns nothing
@Bean
public IntegrationFlow outboundFileFlow() {
return IntegrationFlow
.from("toWriteChannel")
.handle(Files.outboundAdapter(new File("/data/out"))
.fileExistsMode(FileExistsMode.REPLACE))
.get();
}
}go deeper
Must know: adapter connects to an external system and is one-way; inbound brings data in, outbound sends data out.
Should explain the MessageSource+poller (inbound) vs MessageHandler (outbound) internals and the polled-vs-message-driven distinction.
Frames adapter-vs-gateway as one-way-vs-request-reply and knows which transports default to which.
Reasons about backpressure, poller tuning, and transactional boundaries around inbound adapters and how adapter choice affects delivery guarantees.
**Spring Integration in one paragraph.** Spring Integration implements the Enterprise Integration Patterns (EIP). Components communicate by passing `Message<T>` objects (a payload plus a `MessageHeaders` map) over `MessageChannel`s. Endpoints connect channels to logic or to the outside world. A **channel adapter** is the endpoint type that connects a message channel to an *external system* — a file system, a JMS broker, an HTTP endpoint, a TCP socket, a database, etc. **Directionality.** - An **inbound channel adapter** moves data *from the external world into the flow*. It is backed by a `MessageSource<T>` (e.g. `FileReadingMessageSource`, `JdbcPollingChannelAdapter`). A **poller** (`@Poller` / `PollerMetadata`) calls `receive()` on the source on a schedule; each returned item becomes a `Message` placed on the adapter's output channel. Some inbound adapters are instead **message-driven** (push, not poll) — e.g. the JMS message-driven channel adapter uses a listener container, so there is no poller. - An **outbound channel adapter** moves data *from the flow out to the external world*. It is backed by a `MessageHandler` (e.g. `FileWritingMessageHandler`, `JmsSendingMessageHandler`). It consumes a message and performs a side effect (write file, publish JMS message) and returns **nothing**. **One-way is the key trait.** The word *adapter* signals **one-way** semantics. An inbound adapter only receives; an outbound adapter only sends. Neither produces a reply. This is the crucial contrast with a **gateway**, which is **two-way / request-reply**: an inbound gateway receives a request *and* sends a reply back to the caller; an outbound gateway sends a request *and* waits for a response. Choose adapter for fire-and-forget, gateway for request-reply. HTTP and TCP, being naturally request-reply, are most often used as gateways; file and (send-side) JMS are commonly adapters. **Java DSL vs XML vs annotations.** You can declare adapters three ways: XML namespaces (`<int-file:inbound-channel-adapter>`), the Java DSL (`IntegrationFlow` with `Files.inboundAdapter(...)` / `.handle(...)`), or annotations (`@InboundChannelAdapter` on a bean method returning a `MessageSource`, plus `@Poller`). All three produce the same runtime endpoints. **Gotchas.** - Forgetting a poller on a *polled* inbound adapter means nothing ever fires — there must be a default poller bean or an explicit `@Poller`. Message-driven adapters (JMS listener, TCP receiving) don't need one. - An outbound *adapter* silently discards any return value; if you actually needed the external system's response, you picked the wrong component and want an outbound *gateway*. - Adapters are one-way, so you cannot attach a reply channel to them.
- When would you use a gateway instead of a channel adapter?When you need request-reply: the caller expects a response. An inbound gateway receives a request and returns a reply; an outbound gateway sends a request and waits for the answer. Adapters are one-way only.
- How does a polled inbound adapter differ from a message-driven one?A polled adapter (e.g. file, JDBC) wraps a MessageSource that a poller calls on a schedule. A message-driven adapter (e.g. JMS listener container, TCP receiving adapter) is pushed by the external system, so it needs no poller.
saying these in an interview costs you the question
- Claiming channel adapters can do request-reply
- Saying an outbound adapter returns the external system's response
- Thinking all inbound adapters must be polled (JMS/TCP are message-driven)
- Confusing adapter (one-way) with gateway (two-way)