当我使用DSL动态创建IntegrationFlow时遇到一个问题。 如果将throwChannel定义为消息通道对象,并且过滤器返回false,则什么也不会发生(消息不会发送到指定的丢弃通道) 来源是:
@Autowired
@Qualifier("SIMPLE_CHANNEL")
private MessageChannel simpleChannel;
IntegrationFlow integrationFlow = IntegrationFlows.from("channelName")
.filter(simpleMessageSelectorImpl, e -> e.discardChannel(simpleChannel))
.get();
...
@Autowired
@Qualifier("SIMPLE_CHANNEL")
private MessageChannel simpleChannel;
@Bean
public IntegrationFlow simpleFlow() {
return IntegrationFlows.from(simpleChannel)
.handle(m -> System.out.println("Hello world"))
.get();
@Bean(name = "SIMPLE_CHANNEL")
public MessageChannel simpleChannel() {
return new DirectChannel();
}
但是,如果将废弃通道定义为通道名称,则一切正常。 调试时,我发现上面提到的代码部分:
IntegrationFlow integrationFlow = IntegrationFlows.from("channelName")
.filter(simpleMessageSelectorImpl, e -> e.discardChannel(simpleChannel))
.get();
返回流对象,该流对象具有与integrationComponents映射的组件,并且其中一个组件FilterEndpointSpec具有类型为MessageFilter的“ handler”字段,其discardChannel = null,discardChannelName = null; 但是,如果将丢弃通道定义为该通道的名称,则所提及的字段“ handler”的值将为discardChannel = null,但discardChannelName =“ SIMPLE_CHANNEL”,结果一切正常。
这是我正在运行的应用程序的行为。我还编写了测试,并且在测试中,两种情况下的所有工作都很好(该测试不能在所有spring上下文中运行,因此可能与那里的任何冲突有关)
也许有人知道这可能是什么。
spring boot版本是2.1.8.RELEASE,spring集成是5.1.7.RELEASE
谢谢
答案 0 :(得分:3)
您描述的行为确实是不正确的,让我感到奇怪,但是经过测试,我似乎无法重现它,因此您提供的信息中可能缺少某些内容。无论如何,这是我为您效仿的完整应用程序,可以正常使用。因此,也许您可以比较一下,看看是否有跳跃的地方:
@SpringBootApplication
public class IntegrationBootApp {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(IntegrationBootApp.class, args);
MessageChannel channel = context.getBean("channelName", MessageChannel.class);
PollableChannel resultChannel = context.getBean("resultChannel", PollableChannel.class);
PollableChannel discardChannel = context.getBean("SIMPLE_CHANNEL", PollableChannel.class);
channel.send(MessageBuilder.withPayload("foo").build());
System.out.println("SUCCESS: " + resultChannel.receive());
channel.send(MessageBuilder.withPayload("bar").build());
System.out.println("DISCARD: " + discardChannel.receive());
}
@Autowired
@Qualifier("SIMPLE_CHANNEL")
private PollableChannel simpleChannel;
@Bean
public IntegrationFlow integrationFlow() {
IntegrationFlow integrationFlow = IntegrationFlows.from("channelName")
.filter(v -> v.equals("foo"), e -> e.discardChannel(simpleChannel))
.channel("resultChannel")
.get();
return integrationFlow;
}
@Bean(name = "SIMPLE_CHANNEL")
public PollableChannel simpleChannel() {
return new QueueChannel();
}
@Bean
public PollableChannel resultChannel() {
return new QueueChannel(10);
}
}
有输出
- 成功:GenericMessage [有效载荷= foo,标头= {id = cf7e2ef1-e49d-1ecb-9c92-45224d0d91c1,时间戳= 1576219339077}]
- 磁盘:GenericMessage [有效载荷=栏,标头= {id = bf209500-c3cd-9a7c-0216-7d6f51cd5f40,timestamp = 1576219339078}]