Search code examples
javaspringspring-bootspring-integration

How to make Spring Integrations's Jdbc inbound-channel-adapter to start polling only after a certain event?


In my Spring boot application, I have written below to poll Data from DB . The data is polled when the Spring boot application is started.. How can I start polling the data on some event?

    <?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>

Above configuration start polling when app starts. However, I want polling to when below API is invoked

@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

    }    
}

ONLY after this API is invoked, I want above int-jdbc:inbound-channel-adapter to start polling. How can this be achieved?


Solution

  • See an answer to this question: How to invoke Spring channel adapter from rest api?

    The inbound channel adapter does its logic on a schedule and that happens in a cycle independently of the rest application logic. It really sounds like you'd need to get data from DB when that getInventoryStatus is called. So, a JDBC outbound Gateway with request-reply capabilities is what you need.

    any inbound channel adapter is an automatic reaction to data changes in the target store: it does nothing with human initiated events.

    See more info in docs:

    https://www.enterpriseintegrationpatterns.com/patterns/messaging/PollingConsumer.html

    https://docs.spring.io/spring-integration/reference/html/core.html#pollable-message-source