3

I can't find a useful example for polling a JPA source for inbound data. I know how to do this in XML but can't figure out how do it in DSL.

In short what I want to do is periodically poll a JPA repository for records then put the records into a flow that will do the usual filtering/transforming/executing.

Kind regards

David Smith

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
user1232555
  • 1,099
  • 3
  • 11
  • 18

2 Answers2

3

You are right: there is no yet JPA components support in the Spring Integration Java DSL. Feel free to raise a JIRA (JavaDSL component) on the matter and we'll take care about this demand. Feel free to contribute as well!

Meanwhile I can help you to figure out how to do that without high-level API.

The <int-jpa:inbound-channel-adapter> is based on the JpaPollingChannelAdapter and JpaExecutor objects (exactly them we will use for DSL API). You just must to configure @Bean for JpaExecutor and use it like this:

@Bean
public JpaExecutor jpaExecutor(EntityManagerFactory entityManagerFactory) {
     JpaExecutor jpaExecutor = new JpaExecutor(entityManagerFactory);
     jpaExecutor.setJpaQuery("from Foo");
     ....
     return jpaExecutor;
}

@Bean
public IntegrationFlow jpaFlow(JpaExecutor jpaExecutor) {
    return IntegrationFlows.from(new JpaPollingChannelAdapter(jpaExecutor))
                      .split()
                      .transform()
       ....
}

Everything else will be done by framework as usual for existing DSL components API.

UPDATE

How to provide auto-startup= property when creating JpaPollingChannelAdapter programmatically? Also, is it possible to get this bean and invoke .start(), .stop() using control-bus?

See, Gary's answer. The Lifecycle control is a responsibility of Endpoint in our case it is SourcePollingChannelAdapter. So, you should specify that second Lambda argument, configure the .autoStartup() and .id() there to be able to inject the SourcePollingChannelAdapter for your JpaPollingChannelAdapter and operate with it for your purpose. That id really can be used from control-bus to start()/stop() at runtime.

Yes, I agree JpaPollingChannelAdapter is unfortunate name for that class because it is really a MessageSource implementation.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • How to provide `auto-startup=` property when creating `JpaPollingChannelAdapter` programmatically? Also, is it possible to get this bean and invoke `.start()`, `.stop()` using control-bus? – kisileno Feb 26 '16 at 00:26
  • 1
    Please, see an UPDATE in my answer. – Artem Bilan Feb 26 '16 at 00:32
  • @ArtemBilan - Is there any other repo where we can have basics of spring integration dsl with JPA stuff . could you please tell us .. – SakthiSureshAnand Apr 15 '20 at 13:25
  • We have some code snippets in Docs: https://docs.spring.io/spring-integration/docs/5.3.0.M4/reference/html/jpa.html#configuring-with-the-java-dsl-2. Plus there are some tests in the project: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/dsl/JpaTests.java – Artem Bilan Apr 15 '20 at 14:38
2

Wire up a JpaPollingChannelAdapter as a @Bean and use

IntegrationFlows.from(jpaMessageSource(), 
                      c -> c.poller(Pollers.fixedDelay(1000)))
                .transform(...)
                ...

See the DSL Reference for configuration options.

This one's near the top (with a different message source).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179