Sprint integration flow won't start

余生长醉 提交于 2021-02-11 13:11:25

问题


This looks like a beginner question to me.

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();

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.

Any and all suggestions will be welcome.


Edit: 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();

回答1:


You are missing the main point of Spring - Inversion of Control container.

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.

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.

Please, learn what is Spring application context: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

And how to make an IntegrationFlow as a bean: https://docs.spring.io/spring-integration/reference/html/dsl.html#java-dsl



来源:https://stackoverflow.com/questions/65499125/sprint-integration-flow-wont-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!