为每次轮询使用JpaPollingChannelAdapter时为NamedQuery参数设置不同的值。

huangapple go评论62阅读模式
英文:

set different values for NamedQuery parameters when using JpaPollingChannelAdapter for each polling

问题

以下是翻译好的部分:

我想要对一个表进行定期的轮询,我希望选择出所有在上一次轮询之后创建的行。我在这里尝试使用 spring-integration-jpa,但是在大多数示例代码中,都是使用类似布尔值的列来选择符合条件的行。

这是 spring-integration 的配置代码:

@Configuration
@EnableIntegration
public class PollerConfiguration {

    @Autowired
    private EntityManager entityManager;

    @Payload
    private JpaExecutor getExecutor(){
        JpaExecutor executor = new JpaExecutor(this.entityManager);
        executor.setEntityClass(ChangeRecord.class);
        executor.setNamedQuery("ChangeRecord.findAllAfterLastPolled");
        //todo some magic here

        return executor;
    }

    @Bean
    @InboundChannelAdapter(value = "changeRecordChannel", poller = @Poller(fixedDelay = "10000"))
    public MessageSource<?> jpaInbound() {
        JpaPollingChannelAdapter adapter = new JpaPollingChannelAdapter(this.getExecutor());
        return adapter;
    }
}

这是查询的命名查询部分:

@NamedQuery(
        name = "ChangeRecord.findAllAfterLastPolled",
        query="select r from ChangeRecord r where r.changeDt > :lastPolled")
public class ChangeRecord implements Serializable {

我想知道是否有办法在每次轮询器执行轮询时为 :lastPolled 设置动态值。

英文:

I want to poll a table for some fixed delay, and I want to select all the rows that are created after last poll. I am trying to use spring-integration-jpa here, but in most of the sample codes use a boolean-like column for select the rows that fit the condition.

this is spring-integration config code

@Configuration
@EnableIntegration
public class PollerConfiguration {

    @Autowired
    private EntityManager entityManager;

    @Payload
    private JpaExecutor getExecutor(){
        JpaExecutor executor = new JpaExecutor(this.entityManager);
        executor.setEntityClass(ChangeRecord.class);
        executor.setNamedQuery(&quot;ChangeRecord.findAllAfterLastPolled&quot;);
        //todo some magic here

        return executor;
    }

    @Bean
    @InboundChannelAdapter(value = &quot;changeRecordChannel&quot;, poller = @Poller(fixedDelay = &quot;10000&quot;))
    public MessageSource&lt;?&gt; jpaInbound() {
        JpaPollingChannelAdapter adapter = new JpaPollingChannelAdapter(this.getExecutor());
        return adapter;
    }
}

,and this is the namedquery

@NamedQuery(
        name = &quot;ChangeRecord.findAllAfterLastPolled&quot;,
        query=&quot;select r from ChangeRecord r where r.changeDt &gt; :lastPolled&quot;)
public class ChangeRecord implements Serializable {

I want to know is there any way to set dynamic value for :lastPolled each time poller does poll.

答案1

得分: 0

看看 JpaExecutor

/**
 * 指定将用于提供附加参数的 {@link ParameterSource}。
 * @param parameterSource 不能为空。
 */
public void setParameterSource(ParameterSource parameterSource) {

因此,如果您有一些状态要获取 lastPolled 值,只需在 hasValue()/getValue() 中实现这样的逻辑即可。

英文:

See JpaExecutor:

/**
 * Specify the {@link ParameterSource} that would be used to provide
 * additional parameters.
 * @param parameterSource Must not be null.
 */
public void setParameterSource(ParameterSource parameterSource) {

So, if you have some state to take that lastPolled value, you can just implement such a logic in the hasValue()/getValue().

huangapple
  • 本文由 发表于 2020年10月7日 08:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定