java 如何使SpringIntegrations的Jdbc入站通道适配器仅在某个事件之后开始轮询?

6gpjuf90  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(133)

在我的Sping Boot 应用程序中,我写了下面的内容来轮询来自DB的数据。当Spring启动应用程序启动时,数据被轮询。我如何开始轮询某个事件的数据?

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:int="http://www.springframework.org/schema/integration"
        xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
           http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
     
        <int:channel id="fromdb"/>

         <int:channel id="outChannel">
            <int:dispatcher task-executor="executorPool"/>
         </int:channel/>

        <task:executor id="executorPool" pool-size="10"/> 
        
        <int-jdbc:inbound-channel-adapter
            channel="fromdb" data-source="dataSource"
            max-rows-per-poll="100"
            query="SELECT * FROM Items WHERE INVENTORY_STATUS = 0"
            update="UPDATE Items SET INVENTORY_STATUS = 1">
            <int:poller fixed-delay="4000" />
        </int-jdbc:inbound-channel-adapter>
             
         <int:bridge input-channel="fromdb" output-channel="outChannel"/>

         <int:service-activator input-channel="outChannel"
            ref="jdbcMessageHandler" method="onMessage" />
</beans>

以上配置在应用程序启动时开始轮询。但是,我希望在调用以下API时进行轮询

@RestController
public class FileConsumerController {

    private final Logger logger = LoggerFactory.getLogger(FileConsumerController.class);

    @GetMapping(value = "/inventoryStatus")
    public ResponseEntity<String> getInventoryStatus(@RequestParam("accId") String accId){
         // ONLY after this API is invoked, I want above int-jdbc:inbound-channel-adapter to start polling

    }    
}

只有在这个API被调用之后,我才希望上面的int-jdbc:inbound-channel-adapter开始轮询。

sczxawaw

sczxawaw1#

请参阅此问题的答案:How to invoke Spring channel adapter from rest api?
入站通道适配器按计划执行其逻辑,并且在一个独立于其余应用程序逻辑的循环中发生。听起来确实像是在调用getInventoryStatus时需要从DB获取数据。因此,具有请求-应答功能的JDBC出站网关正是您所需要的。
任何入站通道适配器都是对目标存储中的数据变化的自动React:它对人类发起的事件不起作用。
更多信息请参见文档:
https://www.enterpriseintegrationpatterns.com/patterns/messaging/PollingConsumer.html
https://docs.spring.io/spring-integration/reference/html/core.html#pollable-message-source

相关问题