Install Gemfire entry-ttl in Java Beans

I would like to create a Gemfire scope in a Spring Boot application. Following this sample , it works great with adding database support. If I add a database, an error like "Failed to create bean named" dataSource "" appears. However, the default gemfire bean works well with datasource integration.

@EnableAutoConfiguration
// Sprint Boot Auto Configuration
@ComponentScan(basePackages = "napo.demo")
@EnableCaching
@SuppressWarnings("unused")
public class Application extends SpringBootServletInitializer {

private static final Class<Application> applicationClass = Application.class;
private static final Logger log = LoggerFactory.getLogger(applicationClass);

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

/* **The commented code works well with database.**
@Bean
CacheFactoryBean cacheFactoryBean() {
    return new CacheFactoryBean();
}

@Bean
ReplicatedRegionFactoryBean<Integer, Integer> replicatedRegionFactoryBean(final Cache cache) {
     ReplicatedRegionFactoryBean<Integer, Integer> region= new ReplicatedRegionFactoryBean<Integer, Integer>() {{
        setCache(cache);
        setName("demo");

    }};

    return region;
}  */
// This configuration will cause issue as beow 
// 

      

org.springframework.beans.factory.BeanCreationException: error while creating bean with name 'dataSource' defined in key path resource [org / springframework / boot / autoconfigure / jdbc / DataSourceAutoConfiguration $ NonEmbeddedConfiguration.class]: bean instance using factory method failed; Nested exception - org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: factory method 'dataSource' threw an exception; The nested exception is java.lang.NullPointerException

@Bean
GemfireCacheManager cacheManager(final Cache gemfireCache) {
    return new GemfireCacheManager() {
        {
            setCache(gemfireCache);
        }
    };
}


// NOTE ideally, "placeholder" properties used by Spring PropertyPlaceholderConfigurer would be externalized
    // in order to avoid re-compilation on property value changes (so... this is just an example)!
    @Bean
    public Properties placeholderProperties() {
        Properties placeholders = new Properties();

        placeholders.setProperty("app.gemfire.region.eviction.action", "LOCAL_DESTROY");
        placeholders.setProperty("app.gemfire.region.eviction.policy-type", "MEMORY_SIZE");
        placeholders.setProperty("app.gemfire.region.eviction.threshold", "4096");
        placeholders.setProperty("app.gemfire.region.expiration.entry.tti.action", "INVALIDATE");
        placeholders.setProperty("app.gemfire.region.expiration.entry.tti.timeout", "300");
        placeholders.setProperty("app.gemfire.region.expiration.entry.ttl.action", "DESTROY");
        placeholders.setProperty("app.gemfire.region.expiration.entry.ttl.timeout", "60");
        placeholders.setProperty("app.gemfire.region.partition.local-max-memory", "16384");
        placeholders.setProperty("app.gemfire.region.partition.redundant-copies", "1");
        placeholders.setProperty("app.gemfire.region.partition.total-max-memory", "32768");

        return placeholders;
    }

    @Bean
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(
            @Qualifier("placeholderProperties") Properties placeholders) {
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setProperties(placeholders);
        return propertyPlaceholderConfigurer;
    }

    @Bean
    public Properties gemfireProperties() {
        Properties gemfireProperties = new Properties();

        gemfireProperties.setProperty("name", "SpringGemFireJavaConfigTest");
        gemfireProperties.setProperty("mcast-port", "0");
        gemfireProperties.setProperty("log-level", "config");

        return gemfireProperties;
    }

    @Bean
    @Autowired
    public CacheFactoryBean gemfireCache(@Qualifier("gemfireProperties") Properties gemfireProperties) throws Exception {
        CacheFactoryBean cacheFactory = new CacheFactoryBean();
        cacheFactory.setProperties(gemfireProperties);
        return cacheFactory;
    }


    @Bean(name = "ExamplePartition")
    @Autowired
    public ReplicatedRegionFactoryBean<Object, Object> examplePartitionRegion(Cache gemfireCache,
            @Qualifier("partitionRegionAttributes") RegionAttributes<Object, Object> regionAttributes) throws Exception {

        ReplicatedRegionFactoryBean<Object, Object> examplePartitionRegion =
            new ReplicatedRegionFactoryBean<Object, Object>();

        examplePartitionRegion.setAttributes(regionAttributes);
        examplePartitionRegion.setCache(gemfireCache);
        examplePartitionRegion.setName("demo");


        return examplePartitionRegion;
    }

    @Bean
    @Autowired
    public RegionAttributesFactoryBean partitionRegionAttributes(
            EvictionAttributes evictionAttributes,
            @Qualifier("entryTtiExpirationAttributes") ExpirationAttributes entryTti,
            @Qualifier("entryTtlExpirationAttributes") ExpirationAttributes entryTtl) {

        RegionAttributesFactoryBean regionAttributes = new RegionAttributesFactoryBean();

        regionAttributes.setEvictionAttributes(evictionAttributes);
        regionAttributes.setEntryIdleTimeout(entryTti);
        regionAttributes.setEntryTimeToLive(entryTtl);


        return regionAttributes;
    }

    @Bean
    public EvictionAttributesFactoryBean defaultEvictionAttributes(
            @Value("${app.gemfire.region.eviction.action}") String action,
            @Value("${app.gemfire.region.eviction.policy-type}") String policyType,
            @Value("${app.gemfire.region.eviction.threshold}") int threshold) {

        EvictionAttributesFactoryBean evictionAttributes = new EvictionAttributesFactoryBean();

        evictionAttributes.setAction(EvictionActionType.valueOfIgnoreCase(action).getEvictionAction());
        evictionAttributes.setThreshold(threshold);
        evictionAttributes.setType(EvictionPolicyType.valueOfIgnoreCase(policyType));

        return evictionAttributes;
    }

    @Bean
    public ExpirationAttributesFactoryBean entryTtiExpirationAttributes(
            @Value("${app.gemfire.region.expiration.entry.tti.action}") String action,
            @Value("${app.gemfire.region.expiration.entry.tti.timeout}") int timeout) {

        ExpirationAttributesFactoryBean expirationAttributes = new ExpirationAttributesFactoryBean();

        expirationAttributes.setAction(ExpirationActionType.valueOfIgnoreCase(action).getExpirationAction());
        expirationAttributes.setTimeout(timeout);

        return expirationAttributes;
    }

    @Bean
    public ExpirationAttributesFactoryBean entryTtlExpirationAttributes(
            @Value("${app.gemfire.region.expiration.entry.ttl.action}") String action,
            @Value("${app.gemfire.region.expiration.entry.ttl.timeout}") int timeout) {

        ExpirationAttributesFactoryBean expirationAttributes = new ExpirationAttributesFactoryBean();

        expirationAttributes.setAction(ExpirationActionType.valueOfIgnoreCase(action).getExpirationAction());
        expirationAttributes.setTimeout(timeout);

        return expirationAttributes;
    }

    @Bean
    public PartitionAttributesFactoryBean defaultPartitionAttributes(
            @Value("${app.gemfire.region.partition.local-max-memory}") int localMaxMemory,
            @Value("${app.gemfire.region.partition.redundant-copies}") int redundantCopies,
            @Value("${app.gemfire.region.partition.total-max-memory}") int totalMaxMemory) {

        PartitionAttributesFactoryBean partitionAttributes = new PartitionAttributesFactoryBean();

        partitionAttributes.setLocalMaxMemory(localMaxMemory);
        partitionAttributes.setRedundantCopies(redundantCopies);
        partitionAttributes.setTotalMaxMemory(totalMaxMemory);

        return partitionAttributes;
    }


@Override
protected SpringApplicationBuilder configure(
        SpringApplicationBuilder application) {
    return application.sources(applicationClass);
}}

      

java code demoService:

@Service
public class demoService {

  @Autowired    
private demoMapper demoMapper;


@Cacheable("demo")
public Fund getDemo(String code) {
    Demo demo= demoMapper.getDemo(Code);
    return demo;

}

      

+3


source to share


1 answer


+1


source







All Articles