繁体   English   中英

使用 Spring Integration 防止消息被多次轮询?

[英]Prevent a message to be polled more than once using Spring Integration?

我正在使用 Spring Integration 和JpaPollingChannelAdapter从数据库中轮询具有特定状态的条目。 如果找到条目,则处理这些条目。

我想知道是否有任何方法可以防止条目被轮询两次?

我尝试使用JpaOutboundGateway更新 JPA 实体上的状态,但如果轮询频率很高,则条目在更新之前会被轮询多次。

这是我的配置:

@Bean
public MessageChannel defaultInputChannel() {
    return new DirectChannel();
}

@Bean
public JpaExecutor jpaFindAllExecutor() {
    JpaExecutor jpaExecutor = new JpaExecutor(this.entityManagerFactory);
    jpaExecutor.setNamedQuery("SELECT o FROM MyEntity o WHERE o.status=0");
    return jpaExecutor;
}

@Bean
@InboundChannelAdapter(poller = @Poller(maxMessagesPerPoll = "${poller.maxMessagesPerPoll}", fixedDelay = "${poller.interval}", receiveTimeout = "${poller.receiveTimeout}"))
public MessageSource<?> pollingSource() {
    return new JpaPollingChannelAdapter(jpaFindAllExecutor());
}

JpaExecutor带有一个类似deleteAfterPoll的选项:

https://docs.spring.io/spring-integration/docs/current/reference/html/jpa.html#jpa-inbound-channel-adapter

如果要删除执行查询后收到的行,请将此值设置为 true。 您必须确保该组件作为事务的一部分运行。

如果你不喜欢DELETE 而是UPDATE ,考虑在您的实体上使用相应的 Hibernate 注释:

@SQLDelete(sql = "Update MyEntity set status = 1 where id = ?")

根据您的@InboundChannelAdapterJpaOutboundGateway必须正常工作。 您可能会将处理从一个线程转移到另一个线程。 只要您不离开轮询线程,就不会再轮询数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM