Spring Integration JPA Gateway:对空结果采取行动

问题描述

我的流程中有以下内容

| id  | content_name                                | content_path                                                | created_time  |
| --- | ------------------------------------------- | ----------------------------------------------------------- | ------------- |
| 14  | gayathri-sri-ptbKY_b1ROc-unsplash.jpg       | /assets/content/gayathri-sri-ptbKY_b1ROc-unsplash.jpg       | 1597082730561 |
| 15  | nathan-anderson-UhagOo7IOyc-unsplash.jpg    | /assets/content/nathan-anderson-UhagOo7IOyc-unsplash.jpg    | 1597131503584 |
| 16  | melnychuk-nataliya-8J6uuvsdj-4-unsplash.jpg | /assets/content/melnychuk-nataliya-8J6uuvsdj-4-unsplash.jpg | 1597141241146 |

如果jpa查询未返回任何结果,我想做些事情。我需要为此提供建议吗?如果是这样,我如何获取查询返回空结果的信息?

更新

根据Artem的建议,我提出了以下建议:

.handle(Jpa.retrievingGateway(this.sourceEntityManagerFactory)
                    .entityClass(DocumentHeader.class)
                    .jpaQuery("from DocumentHeader d where d.modifiedDate > :modified")
                    .parameterExpression("modified","payload")
                    .maxResults(maxResults),e -> e.id("retrieveDocumentHeader"))
            .channel(Channels.DOCUMENT_HEADER.name())

还有网关:

    @Bean
public Advice returnEmptyResultForAdvice() {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setonFailureExpressionString("new java.util.ArrayList(0)");
    advice.setReturnFailureExpressionResult(true);
    return advice;
}

但这给了我以下错误

.handle(Jpa.retrievingGateway(this.sourceEntityManagerFactory)
                    .entityClass(DocumentHeader.class)
                    .jpaQuery("from DocumentHeader d where d.modifiedDate > :modified")
                    .parameterExpression("modified",e -> e.id("retrieveDocumentHeader")
                    .advice(this.advices.returnEmptyResultForAdvice()).requiresReply(true))

UPDATE2

我对 ExpressionEvaluatingRequestHandlerAdvice

进行了更多调试
2020-08-13 19:13:57.474 ERROR 5552 --- [ask-scheduler-1] o.s.integration.handler.LoggingHandler   : org.springframework.integration.handler.ReplyrequiredException: No reply produced by handler 'retrieveDocumentHeader',and its 'requiresReply' property is set to true....]

它永远不会引发异常,所以我想这就是为什么不调用失败表达式的原因?

解决方法

此功能尚未实现:https://jira.spring.io/browse/INT-3333

但是,当将此建议添加到ExpressionEvaluatingRequestHandlerAdvice(端点配置器)及其ReplyRequiredException选项中时,您可以抓住erequiresReply(true)

然后ExpressionEvaluatingRequestHandlerAdvice可以有一个onFailureExpressionreturnFailureExpressionResult以返回例如一个空列表。

更新

我收回我的ExpressionEvaluatingRequestHandlerAdvice的建议。 我们在那里有这样的代码:

Object result = callback.execute();
if (this.onSuccessExpression != null) {
    evaluateSuccessExpression(message);
}
return result;

callback.execute()为我们返回了null,我们对此并不关心,只照原样返回它。 ReplyRequiredException稍后被抛出。因此,这确实符合我们的null reply要求。

我建议您通过检查AbstractRequestHandlerAdvice istead的结果来实现自己的callback.execute()

,

示例建议:

@Bean
public Advice verifyEmptyResult() {
    return new AbstractRequestHandlerAdvice() {
        @Override
        protected Object doInvoke(ExecutionCallback callback,Object target,Message<?> message) {
            Object executed = callback.execute();
            if (executed == null)
                return new ArrayList<>();
            else
                return executed;
        }
    };
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...