简体   繁体   中英

Spring Integration DSL Cafe example — how are channels wired?

The Spring DSL documentation provides a sample project -- café

I'm unsure of a couple of aspects of how this works. Pasting the relevant excerpts here: (Full source at the above link)

@Configuration
@EnableAutoConfiguration
@IntegrationComponentScan
public class Application {

public static void main(String[] args) throws InterruptedException {
    ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);

    Cafe cafe = ctx.getBean(Cafe.class);
    for (int i = 1; i <= 100; i++) {
        Order order = new Order(i);
        order.addItem(DrinkType.LATTE, 2, false);
        order.addItem(DrinkType.MOCHA, 3, true);
        cafe.placeOrder(order);
    }

    Thread.sleep(60000);

    ctx.close();
}

@MessagingGateway
public interface Cafe {

    @Gateway(requestChannel = "orders.input")
    void placeOrder(Order order);

}


@Bean
public IntegrationFlow orders() {
    return f -> f
            .split(Order.class, Order::getItems)
            .channel(c -> c.executor(Executors.newCachedThreadPool()))
            // SNIP
}

Reading this example, I'm unclear on a couple of points:

  • The Cafe interface exposes a @Gateway that connects to requestChannel = "orders.input" . However, this channnel is not defined anywhere. How does this work?

  • The DSL snippet does is not wired to consume from any channels, nor does it refer to the Cafe::placeOrder method -- how does this get connected to the orders.input channel to receive the inbound Order ?

We just published (yesterday) a line-by-line tutorial for the cafe dsl sample which goes into a lot of details about the internals.

When using the lambda version ( f -> f.split()... ) the framework declares an implicit DirectChannel with the bean name ( "orders" ) + ".input" as its id.

You can also use return IntegrationFlows.from("myChannel"). ... .get() return IntegrationFlows.from("myChannel"). ... .get() instead of the lambda expression and, again, the framework will auto-generate the channel if not declared as a bean already.

See the InterationFlows javadoc for more information.

cafe.placeOrder() is invoked in the last line in the for loop in the main method. The framework creates a proxy for the interface that wraps the Order object in a message and sends it to the gateway's request channel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM