【问题标题】:Spring Integration Java DSL IntegrationFlow as Gateway fails to startSpring Integration Java DSL IntegrationFlow 作为网关无法启动
【发布时间】:2018-10-12 11:37:21
【问题描述】:

我在 Spring Boot 应用程序中使用标准 Spring @RestController,该应用程序调用 Spring Integration 以启动消息流。据我了解,在这种情况下,与 Spring 集成的挂钩是使用网关 - 似乎有几种不同的方式可以使用 Java DSL 来完成。

我目前有两种不同的工作方式:

  1. 通过定义一个标有@MessagingGateway 注释的接口。
  2. 通过实例化new GatewayProxyFactoryBean(Consumer.class) 并设置通道。

这两种方法都感觉有点笨拙——似乎还有第三种更简洁的方法,它允许您不必注释或手动构造 GatewayProxyFactoryBean,而只需使用带有 bean 名称的内置功能接口。来自文档:

@Bean
public IntegrationFlow errorRecovererFlow() {
    return IntegrationFlows.from(Function.class, "errorRecovererFunction")
            .handle((GenericHandler<?>) (p, h) -> {
                throw new RuntimeException("intentional");
            }, e -> e.advice(null))
            .get();
}


@Autowired
@Qualifier("errorRecovererFunction")
private Function<String, String> errorRecovererFlowGateway;

但是,bean errorRecovererFunction 似乎没有注册,应用程序无法启动。

Field errorRecovererFlowGateway in MyController required a bean of type 'java.util.function.Function' that could not be found.

我错过了什么吗?

提前致谢。

【问题讨论】:

  • 有没有机会和我们分享一下 GitHub 上的简单项目,让我们玩和复制?
  • 到目前为止一切都很好,我希望你在某个地方有一个@EnableIntegration,或者这是一个 Spring Boot 应用程序。还要确保控制器和集成流在同一个上下文中。

标签: spring-integration spring-integration-dsl


【解决方案1】:

我做了这个申请:

@SpringBootApplication
@RestController
public class So52778707Application {

    @Autowired
    @Qualifier("errorRecovererFunction")
    @Lazy
    private Function<String, String> errorRecovererFlowGateway;

    @GetMapping("/errorRecoverer")
    public String getFromIntegrationGateway(@RequestParam("param") String param) {
        return this.errorRecovererFlowGateway.apply(param);
    }

    @Bean
    public IntegrationFlow errorRecovererFlow() {
        return IntegrationFlows.from(Function.class, "errorRecovererFunction")
                .handle((p, h) -> {
                    throw new RuntimeException("intentional");
                })
                .get();
    }

    public static void main(String[] args) {
        SpringApplication.run(So52778707Application.class, args);
    }

}

注意errorRecovererFlowGateway自动装配属性上的@Lazy注解。关于这个注释的文档说:

如果您想影响某些 bean 的启动创建顺序,请考虑将其中一些声明为 @Lazy(用于在首次访问而不是启动时创建)或 @DependsOn 某些其他 bean(确保特定的其他bean 在当前 bean 之前创建,超出了后者的直接依赖关系)。

我认为我们需要在 Spring Integration Reference Manual 中澄清在 IntegrationFlow 解析期间创建的 bean 不能按原样注入,但 @Lazy 注释应该考虑用于延迟 bean 解析。

【讨论】:

  • 谢谢阿特姆!这现在工作正常。对我来说,这种类型的设置是从常规 Spring Boot 应用程序连接到 Spring Integration 的最清晰和最干净的方法,并且应该在文档中突出显示。从 Java DSL 文档中了解如何从应用程序代码进入和退出流程可能非常困难。
猜你喜欢
  • 1970-01-01
  • 2016-11-26
  • 2016-03-06
  • 2018-09-27
  • 2016-05-07
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 2016-06-10
相关资源
最近更新 更多