在Java Bean中设置Gemfire entry-ttl [英] Set Gemfire entry-ttl in Java Beans

查看:94
本文介绍了在Java Bean中设置Gemfire entry-ttl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spring Boot应用程序中创建一个Gemfire区域.遵循此

I would like to create a Gemfire region in a Spring Boot application. Following this sample, it works well wihout adding database support. If I add database, it will shows error like " Error creating bean with name 'dataSource'". However, default gemfire cache 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:在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration $ NonEmbeddedConfiguration.class]中创建名称为"dataSource"的bean时出错:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.sql.DataSource]:工厂方法'dataSource'引发了异常;嵌套的异常是java.lang.NullPointerException

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


// NOTE ideally, "placeholder" properties used by Spring's 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);
}}

demoService Java代码:

@Service
public class demoService {

  @Autowired    
private demoMapper demoMapper;


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

}

推荐答案

以下是在其他属性中设置entry-ttl的示例:

Here is an example of setting entry-ttl among other attributes: https://github.com/spring-projects/spring-gemfire-examples/blob/master/basic/java-config/src/main/java/org/springframework/data/gemfire/example/SpringJavaBasedContainerGemFireConfiguration.java

这篇关于在Java Bean中设置Gemfire entry-ttl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆