XMLDecoderのとアッカストリームのみドッカー容器内での問題

rkarczmarczyk:

私はドッカーコンテナ内で実行されているアプリで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]

満たすべき条件がいくつかあります:

  1. エラーは、非コンテナホスト上でコードを実行すると、すべてがOKである、唯一のドッカー内で発生します
  2. あなたはBufferedReaderのは、罰金に動作します使用して行毎にファイルを読み込む、XMLDecoderのを使用するときに問題があるだけで
  3. あなたがドッキングウィンドウのCPUを制限する場合(--cpus = 1)エラーは発生しません。
  4. あなたの代わりにアッカストリームのExecutorServiceのを使用する場合、エラーは発生しません。
  5. 私はいくつかのドッキングウィンドウフラグJDKの問題(とそのヘルプを使用しようとしたUseContainerSupportActiveProcessorCount)それは助けにはなりませんでした

コード

利用可能Runnableを例ここ

以下の問題のコード:

@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());
}

}

例えば、この方法を実行することができます:

@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();
}
}

< - >ドッキングウィンドウ< - > javaの私は、根本的な原因は、ホスト間のどこかにあると仮定します

これで任意の助けを事前に感謝

ミハル・M:

サンプルコードは、次のように変更して、私のために働いた:行を置き換えます

XMLDecoder xmlDecoder = new XMLDecoder(bis2);

とともに

XMLDecoder xmlDecoder = new XMLDecoder(bis2, null, null, FileDto.class.getClassLoader());

つまり、効果的に強制的にXMLDecoder問題のクラスをロードするために使用される正確なクラスローダを使用します。しかし、それだけで発生する理由のためとして、

  1. ドッカーで
  2. 場合は--cpus1よりもかなった以上に設定されています
  3. アッカストリームと
  4. 特定のJDKのバージョンで

- 私はいくつかの(主に)無学推測を持っています。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=188802&siteId=1