簡體   English   中英

帶有 Apache Pulsar 和 Spring Boot 的自定義對象

[英]Custom Objects with Apache Pulsar and Spring Boot

我第一次使用 Apache Pulsar,但發現很難發布和收聽自定義對象。 我已經按照參考頁的指示定義了架構,但由於某種原因它不起作用。

@SpringBootApplication
public class QuickbooksApplication {

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

  @Data
  @Builder
  @ToString
  @NoArgsConstructor
  @AllArgsConstructor
  public static class SomeClass {
    private String someVariable;
  }

  @Bean
  ApplicationRunner runner(PulsarTemplate<SomeClass> pulsarTemplate) {
    pulsarTemplate.setSchema(JSONSchema.of(SomeClass.class));
    SomeClass someClass = SomeClass.builder().someVariable("Hello World!!!").build();
    return (args) -> pulsarTemplate.send("hello-pulsar", someClass);
  }

  @PulsarListener(
      subscriptionName = "hello-pulsar-subscription",
      topics = "hello-pulsar",
      schemaType = SchemaType.AUTO_CONSUME)
  void listen(SomeClass message) {
    System.out.println("Message Received: " + message);
  }
}

當我運行它時,我只得到兩個錯誤,

java.lang.IllegalAccessException: class org.apache.pulsar.common.util.netty.DnsResolverUtil cannot access class sun.net..netAddressCachePolicy (in module java.base) because module java.base does not export sun.net to unnamed module @544ff9ef 在 java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392) ~[na:an]

java.lang.NullPointerException:無法調用“org.apache.pulsar.client.api.Schema.getSchemaInfo()”,因為“org.springframework.pulsar.listener.PulsarContainerProperties.getSchema()”的返回值為886348182539

當我將它作為字符串使用時,一切正常。

任何幫助將不勝感激。 提前致謝。

第一個錯誤是由於 Java17 默認對內部進行了強封裝。 為了解決這個問題,你最終不得不在你啟動的所有地方add-opens 以下是使用 Spring Boot Gradle 插件啟動應用程序時將其設置為bootRun的示例。 這是一個煩惱,但不是最終 NPE 的原因。

實際原因是@PulsarListener不支持SchemaType.AUTO_CONSUME 將其設置為SchemaType.JSON 有關更多詳細信息,請參見此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM