【问题标题】:Spring Batch Restart having Eureka Client in dependencySpring Batch Restart 具有依赖的 Eureka Client
【发布时间】:2017-07-19 00:28:23
【问题描述】:

当我在启用发现客户端的情况下尝试重新启动失败的批处理作业时遇到问题。如果我没有@EnableDiscoveryClient,每件事都可以正常工作,但是当我启用它时,我得到了

com.thoughtworks.xstream.converters.ConversionException: Cannot construct type
---- Debugging information ----
message             : Cannot construct type
cause-exception     : java.lang.InstantiationException
cause-message       : java.util.Map$Entry
construction-type   : java.util.Map$Entry
class               : java.util.Map$Entry
required-type       : java.util.Map$Entry
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /map/map/entry
line number         : -1
class[1]            : java.util.HashMap
converter-type[1]   : com.thoughtworks.xstream.converters.collections.MapConverter
version             : 1.4.9
-------------------------------

我在尝试反序列化(in)时跟踪错误,发现它在JdbcExecutionContextDao中

private class ExecutionContextRowMapper implements RowMapper<ExecutionContext> {

        @Override
        public ExecutionContext mapRow(ResultSet rs, int i) throws SQLException {
            ExecutionContext executionContext = new ExecutionContext();
            String serializedContext = rs.getString("SERIALIZED_CONTEXT");
            if (serializedContext == null) {
                serializedContext = rs.getString("SHORT_CONTEXT");
            }

            Map<String, Object> map;
            try {
                ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes("ISO-8859-1"));
                map = serializer.deserialize(in);
            }
            catch (IOException ioe) {
                throw new IllegalArgumentException("Unable to deserialize the execution context", ioe);
            }
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                executionContext.put(entry.getKey(), entry.getValue());
            }
            return executionContext;
        }
    }

检查serializer 的版本和类型并没有让我发现任何事情,因为两者都是相同的,我正在尝试反序列化相同的字符串

{"map":[{"entry":[{"string":"activeSheetId","int":-1},{"string":"acceptPendingDelete","int":-1},{"string":"expiryDateValue","long":7},{"string":"decreaseGracePeriod","long":0},{"string":"countryList","set":[""]},{"string":"acceptRemovedPrefix","int":-1},{"string":"increaseGracePeriod","long":7},{"string":"highPeriod","long":14},{"string":["carrierName","XXXX"]},{"string":"acceptRemovedCountry","int":-1},{"string":"acceptFutureIgnore","int":-1},{"string":"carrierId","long":1168},{"string":"highIncreaseValue","long":2}]}]}

【问题讨论】:

  • 你有针对上下文存储的序列化器定制吗?
  • 不,我没有。当@EnableDiscoveryClient 被禁用时,上面相同的序列化字符串将起作用
  • 您是否也只有在启动时才有此异常?您是否在应用程序启动时立即运行批处理作业?
  • 目前在测试中我正在做的工作是在开始时自动运行,但是当我试图通过休息请求重新启动工作时问题就开始了,所以我认为它不相关,我会仔细检查虽然
  • 您有示例说明这一点吗?

标签: spring-boot spring-batch spring-cloud spring-cloud-netflix


【解决方案1】:

所以在我们在 cmets 的讨论之后,我认为 Eureka 与这个问题没有太大关系。由于 bean 初始化顺序,它可能会影响行为。

我认为问题是 - 当您尝试通过休息请求调用作业时,您很可能正在使用 JobOperator 和 JobExplorer。

尝试像这样初始化JobExplorer:

@Bean
public JobExplorer jobExplorer(DataSource dataSource, ExecutionContextSerializer serializer) throws Exception {
    JobExplorerFactoryBean fac = new JobExplorerFactoryBean();
    fac.setDataSource(dataSource);
    fac.setSerializer(serializer);
    fac.afterPropertiesSet();
    return fac.getObject();
}

重要的部分是您使用的 serializer 与 Spring Batch 完全相同。

【讨论】:

  • 那不起作用,我正在尝试创建一个示例项目来重现该问题
  • 这真的很奇怪,在我打算分享的示例项目中,我几乎创建了所有依赖项的项目,并且运行良好。现在我正在比较两者,看看有什么区别
  • 是的,通常会有所帮助。我有一个类似的问题,当我做一个示例项目时,我发现我的嫌疑人是无辜的;)如果你能找到什么,请写在这里。
  • 由于 Eureka 而发生
【解决方案2】:

好的,经过大量调查,我发现了如何重现问题以及如何解决它

https://github.com/shahbour/batch-task-serializer 的示例项目将展示如何重现错误以及如何修复它。

基本上我所做的就是将pom中的批处理依赖移到依赖部分的顶部,问题就解决了。

我发现如果我在底部包含 spring-batch(我们将面临错误),我最终会得到两个 stax-api stax-api-1.0.1.jarstax-api-1.0-2.jar

[INFO] |  +- com.netflix.eureka:eureka-client:jar:1.4.12:compile
[INFO] |  |  +- org.codehaus.jettison:jettison:jar:1.3.7:compile
[INFO] |  |  |  \- stax:stax-api:jar:1.0.1:compile
...
[INFO] |  +- com.netflix.eureka:eureka-core:jar:1.4.12:compile
[INFO] |  |  +- com.netflix.governator:governator:jar:1.12.10:runtime
[INFO] |  |  |  +- com.netflix.governator:governator-core:jar:1.12.10:runtime
[INFO] |  |  |  |  +- com.google.inject.extensions:guice-multibindings:jar:4.0:runtime
[INFO] |  |  |  |  \- com.google.inject.extensions:guice-grapher:jar:4.0:runtime
[INFO] |  |  |  |     \- com.google.inject.extensions:guice-assistedinject:jar:4.0:runtime
[INFO] |  |  |  \- org.ow2.asm:asm:jar:5.0.4:runtime
[INFO] |  |  \- org.codehaus.woodstox:woodstox-core-asl:jar:4.4.1:runtime
[INFO] |  |     +- javax.xml.stream:stax-api:jar:1.0-2:runtime

如果我把它放在顶部(项目将起作用),我将以一个 stax-api stax-api-1.0-2.jar 结束

[INFO] |  +- com.netflix.eureka:eureka-core:jar:1.4.12:compile
[INFO] |  |  +- com.netflix.governator:governator:jar:1.12.10:runtime
[INFO] |  |  |  +- com.netflix.governator:governator-core:jar:1.12.10:runtime
[INFO] |  |  |  |  +- com.google.inject.extensions:guice-multibindings:jar:4.0:runtime
[INFO] |  |  |  |  \- com.google.inject.extensions:guice-grapher:jar:4.0:runtime
[INFO] |  |  |  |     \- com.google.inject.extensions:guice-assistedinject:jar:4.0:runtime
[INFO] |  |  |  \- org.ow2.asm:asm:jar:5.0.4:runtime
[INFO] |  |  \- org.codehaus.woodstox:woodstox-core-asl:jar:4.4.1:runtime
[INFO] |  |     +- javax.xml.stream:stax-api:jar:1.0-2:runtime
[INFO] |  |     \- org.codehaus.woodstox:stax2-api:jar:3.1.4:runtime

【讨论】:

  • @MichaelMinella 我应该为此创建一个错误,如果是,是什么项目
【解决方案3】:

遇到类似问题,通过在 POM 中添加以下依赖项解决。

  1. 添加 Jettison 1.2/1.3 依赖项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-20
    • 2015-02-04
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2022-01-26
    • 2016-12-27
    相关资源
    最近更新 更多