在Graal中运行时反序列化器失败

时间:2019-09-18 15:19:44

标签: java jackson micronaut graalvm graalvm-native-image

我有一个内置在GraalVM本机映像中的Micronaut应用程序。从正常角度(即没有Graal)运行应用程序时,它运行得很好。

但是,当我尝试从Graal运行时,在尝试将String反序列化为枚举时会遇到以下运行时异常:

org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition queue.mes.tm_email_processing_result-0 at offset 57. If needed, please seek past the record to continue consumption.
Caused by: io.micronaut.core.serialize.exceptions.SerializationException: Error deserializing object from JSON: Class fts.marketing.utils.deserializers.CampaignEmailStatusDeserializer has no default (no arg) constructor
 at [Source: (byte[])"{"merchant":"Lab6","customerId":729441,"campaignRunId":51,"status":"Sent","messageKey":"70bb5454-8e22-4b37-bba7-83c0dd0cb66f"}"; line: 1, column: 1]
    at io.micronaut.jackson.serialize.JacksonObjectSerializer.deserialize(JacksonObjectSerializer.java:70)
    at io.micronaut.configuration.kafka.serde.JsonSerde.deserialize(JsonSerde.java:79)
    at org.apache.kafka.common.serialization.Deserializer.deserialize(Deserializer.java:60)
    at org.apache.kafka.clients.consumer.internals.Fetcher.parseRecord(Fetcher.java:1264)
    at org.apache.kafka.clients.consumer.internals.Fetcher.access$3600(Fetcher.java:124)
    at org.apache.kafka.clients.consumer.internals.Fetcher$PartitionRecords.fetchRecords(Fetcher.java:1488)
    at org.apache.kafka.clients.consumer.internals.Fetcher$PartitionRecords.access$1600(Fetcher.java:1328)
    at org.apache.kafka.clients.consumer.internals.Fetcher.fetchRecords(Fetcher.java:641)
    at org.apache.kafka.clients.consumer.internals.Fetcher.fetchedRecords(Fetcher.java:602)
    at org.apache.kafka.clients.consumer.KafkaConsumer.pollForFetches(KafkaConsumer.java:1294)
    at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1225)
    at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1201)
    at io.micronaut.configuration.kafka.processor.KafkaConsumerProcessor.lambda$process$7(KafkaConsumerProcessor.java:393)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:460)
    at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:193)

我的反序列化器如下:

public class CampaignEmailStatusDeserializer extends JsonDeserializer<CampaignEmailStatus> {

    @Override
    public CampaignEmailStatus deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String readValue = p.getText();
        if (readValue == null) {
            return CampaignEmailStatus.UNKNOWN;
    }

        return Arrays.stream(CampaignEmailStatus.values())
        .filter(status -> status.getDisplayName().equals(readValue))
        .findAny()
        .orElse(CampaignEmailStatus.UNKNOWN);
    }

}

我认为我可能需要为Graal添加一些自定义反射配置,但是我不确定。谁能在这里阐明一些观点?

1 个答案:

答案 0 :(得分:3)

经过一番挖掘后,问题似乎是由SubtrateVM不允许的反射调用引起的。

我为反射调用添加了一个配置文件,其中包含以下条目:

{
    "name": "fts.marketing.utils.deserializers.CampaignEmailStatusDeserializer",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true,
    "allDeclaredClasses" : true,
    "allPublicClasses" : true
  }

,并配置要使用的本机映像。这样就解决了所有问题。