spring - 使用 JobExplorer 访问时失败异常返回 Empty

标签 spring spring-batch spring-batch-admin

我有一个网络应用程序,当我上传文件时,我会立即上传文件,然后使用作业启动器和异步任务执行器启 Action 业。因此从技术上讲,该作业在其自己的线程中运行。 Spring 批处理作业输出 csv,导入数据库并对数据进行一些验证。因此,在任何步骤中出现任何类型的异常,例如由于日期无效而导致解析平面文件时出错,我使用

得到的所有异常
jobExecution.getAllFailureExceptions()

在作业结束时使用监听器以错误电子邮件形式发送出去。而上传文件的用户我让线程休眠 4 分钟

while(isJobRunning(jobId)) {
                if(count == 0)
                    break;
                Thread.sleep(sleepMilliSeconds);
                count--;
            }

然后,如果作业在我在 UI 中向用户显示步骤级别和作业级别的所有验证错误消息之前完成。所以现在我使用

            jobExecution = jobExplorer.getJobExecution(jobExecution.getId());

现在,当我这样做来获取 jobExecution.getAllFailureExceptions() 时,它会返回空异常。我在数据库作业存储库的“EXIT_MESSAGE”BATCH_JOB_EXECUTION 和 BATCH_JOB_STEP_EXECUTION 列中看到了步骤异常和作业级别异常,但当我使用作业资源管理器检索 jobexecution 时,它们未加载。有什么解决方法吗?我必须创建自定义查询来检索步骤执行异常和作业异常吗?请帮我。

下面是我的作业存储库、jobexplorer 和 taskexecutor 配置

<bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="dataSource" ref="batchDataSource" />
    <property name="transactionManager" ref="batchTransactionManager" />
    <property name="databaseType" value="Oracle" />
    <property name="isolationLevelForCreate"  value="ISOLATION_DEFAULT"  />
  </bean> 

 <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="taskExecutor" />
  </bean>

  <bean id="taskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="1" />
    <property name="maxPoolSize" value="3" />
  </bean>

  <bean id="jobExplorer"
        class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
    <property name="dataSource" ref="batchDataSource"/>
  </bean>

最佳答案

JobExplorer 仅返回 JobRepository 中的内容。由于失败异常列表未存储在 JobRepository 中,因此您将无法取回该列表。如果您确实需要维护错误列表,则必须将它们存储在独立于 JobRepository 的某个位置,或者使用 ExecutionContext。如果您使用的是 ExecutionContext,请注意您在其中放入的内容以及该列在数据库中的大小。

关于spring - 使用 JobExplorer 访问时失败异常返回 Empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20222145/

相关文章:

java - 根元素在 Spring Batch 中被多次写入

linux - Azure Kubernetes : Spring Batch running on Ubuntu 18. 04 很少被杀死

使用扩展 DefaultBatchConfiguration 的编程方式时未创建 Spring Batch 5 和 Spring Boot 3 元数据表

Spring-Batch:如何保证一个Job在运行时,不允许同时再次运行

java - 由: com. mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException引起:表 'test.raw_report'不存在

spring-batch - 如何停止和恢复 Spring 批处理作业

java - 用于 AsyncHttpClient 的 RestTemplate

java - 存储库到服务的依赖关系未得到满足

java - CXF WebSocket 配置

java - 在 JSP 中显示图像(Spring 3 java 配置)