Sprint 集成流程无法启动 [英] Sprint integration flow won't start
问题描述
这对我来说似乎是一个初学者问题.
This looks like a beginner question to me.
为了便于复制,我已经从这个流程中去除了几乎所有的逻辑.它看起来什么都不做的原因是因为它实际上从 JDBC 端点获取数据并且什么也不做.
For the sake of easy reproduction, I have stripped out virtually all of my logic from this flow. The reason it looks like it does nothing is because it, quite literally, fetches data from a JDBC endpoint and does exactly nothing.
setupAdapter = new JdbcPollingChannelAdapter(dbprx.getDatasource(), sql.getSql());
setupAdapter.setRowMapper(rowMapper);
StandardIntegrationFlow flow = IntegrationFlows
.from(setupAdapter,
c -> c.poller(Pollers.fixedRate(1000L, TimeUnit.MILLISECONDS).maxMessagesPerPoll(1)))
.enrichHeaders(h -> h.headerExpression("aaa", "payload[0].get(\"aaa\")")
.headerExpression("bbb", "payload[0].get(\"bbb\")")
)
.get();
flow.start();
我希望在启动时(或之后不久)构建和触发流.相反,流被构建并且没有任何反应.我在 JDBCPollingChannelAdapter.doPoll() 的开头设置了一个断点,如果一切正常,它应该每秒触发一次.
I expect the flow to be constructed and fire when (or shortly after) the is started. Instead, the flow is constructed and nothing happens. I have set a breakpoint at the beginning of JDBCPollingChannelAdapter.doPoll() , which should be tripped once per second if all is running correctly.
欢迎提出任何建议.
更正了代码片段,在尝试开始之前显示了 spring 上下文中 hte 流的注册(请注意,这不是一个完整的示例,它只是演示了我的过程的上下文中的部分更正,基于我自己的动态创建流动态来源).更常见的解决方案是使用@Bean 注释静态创建流:
corrected code snip, showing registration of hte flow in the spring context before attempting to start (note this is not a complete example, it simply demonstrates the partial correction in the context for my process dynamically creating flows based on my own dynamic sources). A more common solution is to create the flows statically with @Bean annotations:
setupAdapter = new JdbcPollingChannelAdapter(dbprx.getDatasource(), sql.getSql());
setupAdapter.setRowMapper(rowMapper);
StandardIntegrationFlow flow = IntegrationFlows
.from(setupAdapter,
c -> c.poller(Pollers.fixedRate(1000L, TimeUnit.MILLISECONDS).maxMessagesPerPoll(1)))
.enrichHeaders(h -> h.headerExpression("aaa", "payload[0].get(\"aaa\")")
.headerExpression("bbb", "payload[0].get(\"bbb\")")
)
.get();
flowContext.registration(flow).id(this.getId().toString()).register();
flow.start();
推荐答案
你忽略了 Spring 的要点——控制反转容器.
You are missing the main point of Spring - Inversion of Control container.
IntegrationFlow
本身什么都不是.它必须作为 bean 添加到 ApplicationContext
,然后这个带有 Spring 容器和它的 BeanFactory
的上下文将从 IntegrationFlow
做我们想要的一切抽象.目前,您只需自己完成所有 new
的工作,而无需将其与任何 AplicationContext
连接起来.
The IntegrationFlow
is nothing by itself. It has to be added to the ApplicationContext
as a bean, then this context with Spring container and its BeanFactory
will do everything we want from that IntegrationFlow
abstraction. At the moment you just do all the new
stuff by yourself and you don't wire it up with any AplicationContext
.
我不确定您的逻辑是什么,但是您需要考虑将此流程作为某个 @Configuration
类中的 @Bean
.应用程序上下文将解析这个 IntegraitonFlow
,创建所需的 bean,将它们与其他 bean 自动装配(如果需要)并为我们启动流程.自动.
I'm not sure what is your logic, but you need to consider to make this flow as a @Bean
in some @Configuration
class. The application context will parse this IntegraitonFlow
, create required beans, autowire them with others (if required) and start the flow for us. Automaticaly.
请了解什么是 Spring 应用程序上下文:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
Please, learn what is Spring application context: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
以及如何将 IntegrationFlow
制作为 bean:https://docs.spring.io/spring-integration/reference/html/dsl.html#java-dsl
And how to make an IntegrationFlow
as a bean: https://docs.spring.io/spring-integration/reference/html/dsl.html#java-dsl
这篇关于Sprint 集成流程无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!