如何使用 Spring Data Geode 缓存 PageImpl?

How to cache PageImpl with Spring Data Geode?

当尝试使用 Spring Data Geode 缓存来自 Spring Data JpaRepository 的 PageImpl 响应时,它无法缓存结果并出现以下错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.PageImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.data.domain.PageImpl.<init>()
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:127) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64) ~[spring-data-commons-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:86) ~[spring-data-commons-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.gemfire.mapping.MappingPdxSerializer.fromData(MappingPdxSerializer.java:422) ~[spring-data-gemfire-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.apache.geode.pdx.internal.PdxReaderImpl.basicGetObject(PdxReaderImpl.java:741) ~[geode-core-9.1.1.jar:?]
at org.apache.geode.pdx.internal.PdxReaderImpl.getObject(PdxReaderImpl.java:682) ~[geode-core-9.1.1.jar:?]
at org.apache.geode.internal.InternalDataSerializer.readPdxSerializable(InternalDataSerializer.java:3054) ~[geode-core-9.1.1.jar:?]

看起来 MappingPdxSerializer 正在寻找默认构造函数,但没有为 PageImpl 找到它 class。

这是我拥有的依赖项的 maven pom:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-gemfire</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

我使用的JpaRepository是:

@RepositoryRestResource
public interface RecordRepository extends JpaRepository<Record, Long>
{
    @Override
    @CacheEvict(cacheNames = { "Records" })
    <S extends Record> S save(S s);

    @Override
    @Cacheable(value = "Records")
    Optional<Record> findById(Long id);

    @Override
    @Cacheable(value = "Records", key = "#pageable.pageNumber + '.' + #pageable.pageSize + '.records'")
    Page<Record> findAll(Pageable pageable);

    @Override
    @Cacheable(value = "Records")
    Record getOne(Long aLong);
}

用于调用存储库分页结果的代码是:

int PAGE=0,PAGE_SIZE=100;
        Page<Record> recordPage;
        do {
            recordPage = recordRepository.findAll(PageRequest.of(PAGE, PAGE_SIZE));
            log.info("Retrieved page: [{}]", recordPage);
        } while (recordPage.hasNext());

我觉得这可能是 MappingPdxSerializer 的一个错误,但我不是 100% 确定。解决此问题的任何帮助都很棒!

谢谢

为什么您认为这可能是 Spring Data Geode 的 (SDG) o.s.d.g.mapping.MappingPdxSerializer 的错误?

并非所有通过 SDG 的 MappingPdxSerializer 传递的对象都有默认(即 public,无参数)构造函数,这很常见,甚至可以预料。

当在您的应用程序中使用此类类型(例如 SD PageImpl class)并且从 Apache Geode 读取该类型的实例(例如 get(key))时,对象在(区域)数据访问操作上反序列化和重建(提供 Apache Geode 的 read-serialized 配置属性未设置为 true;这会导致您出现其他问题,因此不建议在在这种情况下),那么您需要注册一个 EntityInstantiator 来通知 SDG MappingPdxSerializer 如何使用适当的构造函数实例化对象。

"appropriate" 构造函数由持久实体的 PreferredConstructor, which is evaluated during type evaluation by the SD Mapping Infrastructure, and can be specified with the @PersistenceContructor annotation, if necessary. This is useful in cases where you are using 1 of SD's canned EntityIntantiator types, e.g. ReflectionEntityInstantiator 决定,并且您的应用程序域类型有超过 1 个非默认构造函数。

因此,您可以在 SDG 的 MappingPdxSerializer.[=33= 上使用 EntityIntantiatiors composite class, perhaps with a "mapping" between application domain object Class type (e.g. Page) and EntityInstantiator, and then register EntityInstantiators 按类型为每个应用程序域对象注册 1 个或多个 EntityInstantiator 对象]

当然,您需要确保自定义配置 MappingPdxSerializer 被 Apache Geode 使用...

@Configuration
class ApacheGeodeConfiration {

  @Bean
  MappingPdxSerializer pdxSerializer() {

    Map<Class<?>, EntityInstantiator> customInstantiators = new HashMap<>();

    customInstantiators.put(Page.class, new MyPageEntityInstantiator());
    customInstantiators.put...

    MappingPdxSerializer pdxSerializer = 
      MappingPdxSerializer.newMappingPdxSerializer();

    pdxSerializer.setGemfireInstantiators(
      new EntityInstantiators(customInstantiators));

    return pdxSerializer;
  }

  @Bean
  CacheFactoryBean gemfireCache(MappingPdxSerializer pdxSerializer) {

    CacheFactoryBean gemfireCache = new CacheFactoryBean();

    gemfireCache.setPdxSerializer(pdxSerializer);
    gemfireCache.set...

    return gemfireCache;
  }

  ...
}

希望对您有所帮助!

-j