如何将ApacheCamel与分页API一起使用



我确实有一个使用ApacheCamel在Quakrkus中开发的项目,它可以与外部API交互并将详细信息保存到数据库中。目前,它与外部API交互以获取详细信息,来自该外部API的样本响应如下所示

{
"totalSize": 3,
"limit": 1,
"offset": 2,
"records": [
{
...
}
]
}

ApacheCamel代码如下所示,使用上述外部API,要求支持基于limit、offset和totalSize的分页。我发现了apachecamel的一些循环功能,但不确定如何使用它们

@RegisterForReflection
@ApplicationScoped
public class SalesForceAccountRouter extends BaseRouter {
@Inject
AccountRepository accountRepository;
@Override
public void configure() throws Exception {
super.configure();
from("direct:salesforce-account")
.removeHeaders("*")
.log("Fetching records from SF Account with request URL::: "
+ "{{sales.s.url}}/accounts?${exchangeProperty.query}")
.toD("{{sales.s.url}}/accounts?${exchangeProperty.query}")
.unmarshal()
.json(JsonLibrary.Gson, PaginationResponse.class)
.process(new SalesForceAccountProcessor(accountRepository))
.marshal()
.json(JsonLibrary.Gson)
.end()
.log("End route");
}
}

Salesforce分页通过查询结果中名为nextRecordsUrl的字段进行。有很多方法可以解决这个问题。这里有一个:

// do initial salesforce query and unmarshal 
...
.setProperty("nextRecordsUrl", simple("${body.nextRecordsUrl}"))
// process query results
...
.loopDoWhile(simple("${exchangeProperty.nextRecordsUrl} != null"))
// query salesforce using nextRecordsUrl
.toD("${exchangeProperty.nextRecordsUrl}")
// unmarshal
.setProperty("nextRecordsUrl", simple("${body.nextRecordsUrl}"))
// process query results
.end()

最新更新