如何使用Spring Data Geode缓存PageImpl?

时间:2018-05-13 20:26:33

标签: spring spring-data spring-data-jpa spring-data-gemfire 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类。

这是我拥有的依赖项的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的一个可能的错误,但我不是百分百肯定。解决这个问题的任何帮助都会很棒!

由于

1 个答案:

答案 0 :(得分:0)

为什么您认为这可能是 Spring Data Geode&#39> {SDG} o.s.d.g.mapping.MappingPdxSerializer的错误?

通过SDG&#39; MappingPdxSerializer传递的所有对象都不会有默认(即public,no-arg)构造函数,这是很常见的,甚至是预期的。

在您的应用程序中使用此类型(例如SD PageImpl类)并从Apache Geode(例如get(key))读取该类型的实例时,对象将被反序列化并重建在(区域)数据访问操作(提供Apache Geode&#39; read-serialized配置属性未设置为 true ;这会导致其他问题,在这种情况下不推荐),然后你需要注册一个EntityInstantiator,告诉SDG&#39; MappingPdxSerializer如何使用适当的构造函数来实例化对象。

&#34;适当&#34;构造函数由持久性实体PreferredConstructor确定,该实体在SD Mapping Infrastructure的类型评估期间进行评估,并且如果需要,可以使用@PersistenceContructor注释进行指定。这在您使用SD的固定EntityIntantiator类型之一的情况下非常有用,例如ReflectionEntityInstantiator,您的应用程序域类型具有多个非默认构造函数。

因此,您可以使用EntityIntantiatiors复合类按类型为每个应用程序域对象注册一个或多个EntityInstantiator个对象,可能在应用程序域对象Class类型之间with a "mapping"(例如Page)和EntityInstantiator,然后register SDG&{39} EntityInstantiators上的MappingPdxSerializer

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

@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