JpaPollingChannelAdapter with entity update at end

Viewed 18

I am working on the integration of a Vendor software that uses for interface a DB table mimicking a queue. Here is the JPA entity representation of such table:

@Data
@Entity
@Table(name = "TASK")
public static class Task implements Serializable {

    @Id
    @GeneratedValue(generator = "TaskId")
    @SequenceGenerator(name = "TaskId", sequenceName = "TASK_SEQ", allocationSize = 50)
    @Column(name = "ID")
    private BigInteger id;

    private Status status;

    private LocalDate processedDate;

    public enum Status {
        NEW, PROCESSED, ERROR
    }
}

I would like to use Spring integration to poll NEW records from this table, handle them (typical use case is to transfom and post on a JMS queue), and then udpate the record with either:

  • status PROCESSED if everything went fine
  • status ERROR if an exception occured

I tried to do so with JpaExecutor + JpaPollingChannelAdapter without much success. How would you recommend to tackle this case ?

Here is how I started:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.handler.GenericHandler;
import org.springframework.integration.jpa.core.JpaExecutor;
import org.springframework.integration.jpa.inbound.JpaPollingChannelAdapter;
import org.springframework.stereotype.Component;

@Component
@EnableIntegration
public class ExampleJob {

    @Autowired
    EntityManager entityManager;

    @Bean
    IntegrationFlow taskExecutorFlow() {
        JpaExecutor selectExecutor = new JpaExecutor(entityManager);
        selectExecutor.setJpaQuery("from Task where status = 'NEW'");

        JpaPollingChannelAdapter adapter = new JpaPollingChannelAdapter(selectExecutor);

        return IntegrationFlows
                .from(adapter, c -> c.poller(Pollers.fixedDelay(Duration.ofMinutes(5))).autoStartup(true))
                .handle((task, headers) -> task)
                .get();
    }
1 Answers
Related