liveBook · Manning

Channel

As explained in Spring in Action, Sixth Edition

A message channel is a Spring Integration messaging destination through which messages are passed between components in an integration flow.

A message channel is the path a message takes between components in a Spring Integration flow. Think of channels as pipes: a gateway sends a message into one, and a transformer, service activator, or adapter receives it at the other end. A channel connects components; the components do the processing. section 10.2 section 10.2.1

Follow a file-writing message

Calling `writeToFile(filename, data)` on a messaging gateway sends a message to `textInChannel`. The filename goes in the `file_name` header; the text goes in the payload. A transformer receives the message, uppercases its payload, and sends the result through `fileWriterChannel`. A file-writing handler then uses the header to name the file and writes the payload. The channel names identify where each component sends or receives messages. section 10.1 section 10.1.1

Listing 10.1 Message gateway interface to transform method invocations into messages
package sia6;
 
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.file.FileHeaders;
import org.springframework.messaging.handler.annotation.Header;
 
@MessagingGateway(defaultRequestChannel="textInChannel")   #1
public interface FileWriterGateway {
 
  void writeToFile(
      @Header(FileHeaders.FILENAME) String filename,       #2
      String data);
 
}

The gateway’s `defaultRequestChannel` sets the destination for messages created by method calls; `@Header` places the filename in a header rather than the payload. section 10.1

In Java configuration, `@Transformer(inputChannel="textInChannel", outputChannel="fileWriterChannel")` connects the transformer to both channels. A `@ServiceActivator` can then receive from `fileWriterChannel`. You need not declare those channels as beans: Spring Integration creates them when no beans with those names exist. Declare channel beans yourself when you need control over their implementation. Java DSL flows can likewise start from a named channel and add an explicit `.channel(...)` step when needed; XML can declare channels with `<int:channel>`. section 10.1.1 section 10.1.2 section 10.1.3

@Bean
public MessageChannel textInChannel() {
  return new DirectChannel();
}
...
@Bean
public MessageChannel fileWriterChannel() {
  return new DirectChannel();
}

These bean methods explicitly make the file-writing flow’s two named channels `DirectChannel` instances. section 10.1.2

Choose a channel implementation for the delivery behavior you need:

  • `DirectChannel` sends to one consumer in the sender’s thread; it is the default for automatically created input channels in Java and Java DSL configuration. section 10.2.1
  • `PublishSubscribeChannel` delivers to every consumer. `ExecutorChannel` dispatches through a `TaskExecutor` in a separate thread, unlike `DirectChannel`. section 10.2.1
  • `QueueChannel` holds messages until a consumer pulls them in FIFO order. `PriorityChannel` pulls by priority header; `RendezvousChannel` blocks the sender until a consumer receives the message. section 10.2.1
  • `FluxMessageChannel` is a Reactive Streams Publisher channel based on Project Reactor’s `Flux`. section 10.2.1

For a queued channel, configure the receiving service activator to poll: a `QueueChannel` stores messages until they are pulled, and `@Poller(fixedRate="1000")` supplies a polling interval in the shown configuration. Channel choice therefore affects not just where a message goes, but how its receiver gets it. section 10.2.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines channels as conduits, compares seven implementations, and shows implicit and explicit configuration; defers further treatment of reactive streams.
Examples
Traces an uppercase file-writing flow through two channels and shows Java beans and polling configuration.
What it assumes
Assumes basic Java and Spring Boot configuration while defining channels from their role as message conduits.
What you can do after
Operate/configure named channels in XML, Java annotations, or Java DSL, selecting delivery behavior and polling when appropriate.
How it connects
Links message gateways, transformers, routers, service activators, channel adapters, message headers, and integration flows.

Also explained in

Compare how 10 books explain channel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.