skip to content

Channel Adapters & Messaging Gateways

Channel adapters bridge files, JMS, HTTP and TCP into and out of the flow, while a messaging gateway hides all of it behind an ordinary interface. That gateway is what keeps messaging from leaking into your domain code.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

explore

questions

5

In Spring Integration, what is a channel adapter, and what is the difference between an inbound and an outbound channel adapter?

level: juniorimportance: must knowfreq 60%

answer

  1. Adapter = one-way bridge to external system
  2. Inbound = MessageSource + poller (or message-driven push)
  3. Outbound = MessageHandler, returns nothing
  4. Two-way = gateway, not adapter
  5. File/JMS-send = adapter; HTTP/TCP = often gateway

basics

~20 s

A 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 s

A 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
java
@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

for a junior

Must know: adapter connects to an external system and is one-way; inbound brings data in, outbound sends data out.

for a middle

Should explain the MessageSource+poller (inbound) vs MessageHandler (outbound) internals and the polled-vs-message-driven distinction.

for a senior

Frames adapter-vs-gateway as one-way-vs-request-reply and knows which transports default to which.

for a principal

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)

context

open as a page

What does @MessagingGateway do, and how does it let application code call an integration flow through a plain Java interface?

level: middleimportance: must knowfreq 55%

basics

~20 s

@MessagingGateway on an interface makes Spring create a proxy. Calling an interface method turns the arguments into a Message sent to a request channel; the reply Message's payload is returned. Your code never touches MessageChannel or Message APIs.

open as a page

How does a file inbound channel adapter work, and how do you stop it from re-processing files it has already picked up?

level: middleimportance: should knowfreq 40%

basics

~20 s

A file inbound channel adapter uses FileReadingMessageSource, polled on a schedule, to emit each new file as a Message. To avoid re-reading the same file, add a FileListFilter such as AcceptOnceFileListFilter (in-memory) or a persistent one backed by a MetadataStore.

open as a page

Contrast an inbound gateway with an outbound gateway using HTTP as the example, and explain why HTTP is usually modeled as a gateway rather than a channel adapter.

level: seniorimportance: should knowfreq 35%

basics

~20 s

HTTP is request-reply, so it maps to gateways. An inbound HTTP gateway (HttpRequestHandlingMessagingGateway) receives an HTTP request, runs the flow, and writes the reply as the HTTP response. An outbound HTTP gateway (HttpRequestExecutingMessageHandler) calls a remote HTTP endpoint and puts the response back on the flow.

open as a page

Explain how Spring Integration bridges raw TCP sockets — connection factories, the adapter-pair vs gateway choice, and how replies are correlated on a shared server connection.

level: principalimportance: nice to knowfreq 18%

basics

~20 s

TCP endpoints share a connection factory (server or client) that owns sockets plus a serializer/deserializer defining message framing. Use TcpInboundGateway/TcpOutboundGateway for request-reply, or a TcpReceivingChannelAdapter + TcpSendingMessageHandler pair sharing one factory for asynchronous flows, correlating replies by the connection id header.

open as a page