Set Gemfire entry-ttl in Java Beans -
i create gemfire region in spring boot application. following sample, works wihout adding database support. if add database, shows error " error creating bean name 'datasource'". however, default gemfire cache bean works 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 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; } */ // configuration cause issue beow //
org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/datasourceautoconfiguration$nonembeddedconfiguration.class]: bean instantiation via factory method failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [javax.sql.datasource]: factory method 'datasource' threw exception; nested exception java.lang.nullpointerexception
@bean gemfirecachemanager cachemanager(final cache gemfirecache) { return new gemfirecachemanager() { { setcache(gemfirecache); } }; } // note ideally, "placeholder" properties used spring's propertyplaceholderconfigurer externalized // in order avoid re-compilation on property value changes (so... 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 code:
@service public class demoservice { @autowired private demomapper demomapper; @cacheable("demo") public fund getdemo(string code) { demo demo= demomapper.getdemo(code); return demo; }
here 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
Comments
Post a Comment