java.lang.NullPointerException:使用Gemfire cachewriter保存jpa数据时

时间:2019-09-11 14:52:31

标签: spring-boot spring-data-gemfire geode

Jpa存储库保存适用于所有类。但是,当尝试保存在CacheWriter中时,它将引发NullPointerException(personRepository.save(entryEvent.getNewValue()))。有什么想法吗?在应用程序属性中配置了mysql数据库。

java.lang.NullPointerException
    at com.javasampleapproach.gemfirerestapi.GemfireWriter.beforeCreate(GemfireWriter.java:28)
    at com.gemstone.gemfire.internal.cache.LocalRegion.cacheWriteBeforePut(LocalRegion.java:3131)
    at com.gemstone.gemfire.internal.cache.AbstractRegionMap.invokeCacheWriter(AbstractRegionMap.java:3145)
    at com.gemstone.gemfire.internal.cache.AbstractRegionMap.basicPut(AbstractRegionMap.java:2909)
    at com.gemstone.gemfire.internal.cache.LocalRegion.virtualPut(LocalRegion.java:5821)
    at com.gemstone.gemfire.internal.cache.LocalRegionDataView.putEntry(LocalRegionDataView.java:118)
    at com.gemstone.gemfire.internal.cache.LocalRegion.basicPut(LocalRegion.java:5211)
    at com.gemstone.gemfire.internal.cache.LocalRegion.validatedPut(LocalRegion.java:1597)
    at com.gemstone.gemfire.internal.cache.LocalRegion.put(LocalRegion.java:1580)
    at com.gemstone.gemfire.internal.cache.AbstractRegion.put(AbstractRegion.java:327)

控制器:

@GetMapping(value = "/getPerson")
    public Iterable<Person> getPerson(@RequestParam("id") long personId,@RequestParam("age") int age, @RequestParam("name") String name) {
        try{
            Person bob = new Person();
            bob.setPersonId(personId);
            bob.setAge(age);
            bob.setName(name);
            Region<Long,Person> region=gemfireCache.getRegion("person");
            region.put(personId, bob);

        }catch(Exception e){
            e.printStackTrace();
        }
        return personRepository.findAll();
    }

Cachewriter:

public class GemfireCacheWriter implements CacheWriter<Long, Person>{
    @Autowired
    PersonRepository personRepository;

   @Override
    public void beforeCreate(EntryEvent<Long, Person> entryEvent) throws CacheWriterException {
        // TODO Auto-generated method stub
        personRepository.save(entryEvent.getNewValue());
    }

    }

CacheWriter配置:

@Bean
    LocalRegionFactoryBean<Long, Person> personRegion(final GemFireCache cache) {
        LocalRegionFactoryBean<Long, Person> personRegion = new LocalRegionFactoryBean<>();
        personRegion.setCache(cache);
        personRegion.setName("person");
        personRegion.setPersistent(false);
        personRegion.setCacheWriter(new GemfireWriter());
        personRegion.setCacheLoader(new GemfireLoader());
        return personRegion;
    }

1 个答案:

答案 0 :(得分:0)

LocalRegion的源代码,我认为您的entryEvent收到的CacheWriter可能不是null,所以实际的null参考可能是personRepository。您是否已正确配置spring-data-gemfire以自动连接PersonRepositoryCacheWriter是否已配置为Spring bean(以@Component为例)?

您可以使用Write Through Example作为实现此用例的良好起点。

希望这会有所帮助。干杯。