英文:
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("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;
}
}
,and this is the namedquery
@NamedQuery(
name = "ChangeRecord.findAllAfterLastPolled",
query="select r from ChangeRecord r where r.changeDt > :lastPolled")
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()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论