首页 > 解决方案 > 如何在 Spring Integration 中使用临时 Channel?

问题描述

我是 Spring Integration 的新手,我正在尝试从临时频道获取消息。

阅读文档,spring 有一个临时通道使用。我猜它的名字NullChannel

我需要gateway从临时通道返回值。

http controller -> gateway -> direct channel -> activator 1 -> queue channel -> activator 2

所以我activator 2会将新值放入临时通道,因此gateway将从临时通道中检索值

在此处输入图像描述

@MessageEndpoint
public class Activator2 {
    
@Autowired
private NullChannel nullChannel;

@ServiceActivator(inputChannel = "asyncChannel")
public void plus(Integer message){
    try {
        message++;
        Thread.sleep(2000);
        nullChannel.send(MessageBuilder.withPayload(message).build());
        log.info("Activator 2: " +message );

    } catch (InterruptedException e) {
        log.error("I don't want to sleep");
    }
    
}
}

它不工作。我不确定一切是否连接良好

标签: javaspringspring-bootspring-integration

解决方案


NullChannel就像/dev/null在 Unix 中一样;它只是丢弃了该值。

@ServiceActivator(inputChannel = "asyncChannel")
public Integer plus(Integer message){
    return message;
}

会自动做你想做的。

如果没有outputChannel,框架将结果发送到replyChannel标头。


推荐阅读