XmlDecoder 和 Akka Stream 的问题仅在 Docker 容器内

rka*_*zyk 5 java docker akka-stream

我只在 Docker 容器中运行的应用程序中遇到了在 AkkaStream 中使用 XmlDecoder 的问题。


错误描述

java.lang.ClassNotFoundException: com/example/xmldecoder/FileDto
Continuing ...
java.lang.ClassNotFoundException: com/example/xmldecoder/FileDto
Continuing ...
java.lang.NoSuchMethodException: <unbound>=XMLDecoder.new();
Continuing ...
java.lang.NoSuchMethodException: <unbound>=XMLDecoder.new();
Continuing ...
java.lang.IllegalStateException: The outer element does not return value
Continuing ...
java.lang.IllegalStateException: The outer element does not return value
Continuing ...
java.lang.IllegalStateException: The outer element does not return value
Continuing ...
java.lang.IllegalStateException: The outer element does not return value
Continuing ...
2019-05-22 09:42:29.145 ERROR 1 --- [onPool-worker-5] com.example.xmldecoder.FileReader        : Unexpected exception in load file, {} 
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at java.desktop/java.beans.XMLDecoder.readObject(XMLDecoder.java:251) ~[na:na]
    at com.example.xmldecoder.FileReader.lambda$loadFile$0(XmlDecoderApplication.java:66) ~[classes!/:0.0.1-SNAPSHOT]
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1700) ~[na:na]
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1692) ~[na:na]
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) ~[na:na]
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020) ~[na:na]
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656) ~[na:na]
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594) ~[na:na]
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177) ~[na:na]
Run Code Online (Sandbox Code Playgroud)

有几个条件需要满足:

  1. 该错误仅发生在 Docker 中,当您在非容器化主机上运行代码时,一切正常
  2. 问题仅当您使用 XmlDecoder 时,使用 BufferedReader 逐行读取文件工作正常
  3. 当您限制 docker CPU (--cpus=1) 时,不会发生错误
  4. 当您使用 ExecutorService 而不是 Akka Streams 时,不会发生错误
  5. 我尝试使用一些有助于解决 JDK 问题的 docker 标志 ( UseContainerSupport, ActiveProcessorCount) 但它没有帮助

代码

可运行示例在此处可用

有问题的代码如下:

@Slf4j
@RequiredArgsConstructor
class FileReader {

private final ActorSystem system;
private final ReadJob readJob;

public NotUsed loadFiles() {
    List<String> paths = listFiles(readJob);
    return Source.from(paths)
            .via(Flow.of(String.class).mapAsync(5, p -> loadFile(p)))
            .to(Sink.foreach(System.out::println)).run(ActorMaterializer.create(system));
}

private CompletionStage<String> loadFile(String filePath) {
    return CompletableFuture.supplyAsync(() -> {
        try {
            FileInputStream fis2 = new FileInputStream(filePath);
            BufferedInputStream bis2 = new BufferedInputStream(fis2);
            XMLDecoder xmlDecoder = new XMLDecoder(bis2);
            FileDto mb = (FileDto) xmlDecoder.readObject();
            log.info("Decoder: {}", mb);
            return mb.toString();
        } catch (Exception e) {
            log.error("Unexpected exception in load file, {}", e);
            throw new RuntimeException("Unexpected exception in load file", e);
        }
    });
}

private List<String> listFiles(ReadJob readJob) {
    File folder = new File(readJob.getHolderDirPath().toString());
    File[] listOfFiles = folder.listFiles();
    log.info(listOfFiles.toString());
    return Stream.of(listOfFiles).map(File::getAbsolutePath).collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)

}

可以运行,例如,这样:

@SpringBootApplication
@EnableScheduling
@Slf4j
public class XmlDecoderApplication {

private Path holderPath = Paths.get("opt", "files_to_load");

public static void main(String[] args) {
    SpringApplication.run(XmlDecoderApplication.class, args);
}

@Scheduled(fixedDelay = 30000, initialDelay = 1000)
public void readFiles() {
    FileReader reader = new FileReader(ActorSystem.create(), new ReadJob(holderPath));
    reader.loadFiles();
}
}
Run Code Online (Sandbox Code Playgroud)

我想根本原因在主机 <-> docker <-> java 之间

在此先感谢您对此的任何帮助

Mic*_*l M 4

示例代码对我进行了以下修改:替换行

\n\n
XMLDecoder xmlDecoder = new XMLDecoder(bis2);\n
Run Code Online (Sandbox Code Playgroud)\n\n

和

\n\n
XMLDecoder xmlDecoder = new XMLDecoder(bis2, null, null, FileDto.class.getClassLoader());\n
Run Code Online (Sandbox Code Playgroud)\n\n

即有效地强制XMLDecoder使用用于加载相关类的精确类加载器。但至于为什么会出现这样的情况

\n\n
    \n
  1. 在 Docker 中
  2. \n
  3. if--cpus设置为大于 1 的某物
  4. \n
  5. 与 Akka Streams
  6. \n
  7. 具有特定的 JDK 版本
  8. \n
\n\n

\xe2\x80\x93 我只有一些(大部分)未经教育的猜测。

\n