|
| 1 | +/* |
| 2 | + * Copyright 2010-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package sample.data.geode; |
| 18 | + |
| 19 | +import java.util.Properties; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 22 | +import org.springframework.beans.factory.annotation.Value; |
| 23 | +import org.springframework.boot.SpringApplication; |
| 24 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 25 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 26 | +import org.springframework.context.ApplicationContext; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.data.gemfire.CacheFactoryBean; |
| 29 | +import org.springframework.data.gemfire.GemfireTemplate; |
| 30 | +import org.springframework.data.gemfire.PartitionedRegionFactoryBean; |
| 31 | +import org.springframework.data.gemfire.RegionAttributesFactoryBean; |
| 32 | + |
| 33 | +import com.gemstone.gemfire.cache.Cache; |
| 34 | +import com.gemstone.gemfire.cache.CacheLoader; |
| 35 | +import com.gemstone.gemfire.cache.CacheLoaderException; |
| 36 | +import com.gemstone.gemfire.cache.LoaderHelper; |
| 37 | +import com.gemstone.gemfire.cache.RegionAttributes; |
| 38 | + |
| 39 | +import sample.data.geode.service.Calculator; |
| 40 | +import sample.data.geode.service.factory.CalculatorFactory; |
| 41 | + |
| 42 | +/** |
| 43 | + * SampleDataGeodeApplication class is a Spring Boot, Apache Geode peer cache application using Geode |
| 44 | + * to perform and store mathematical calculations. |
| 45 | + * |
| 46 | + * @author John Blum |
| 47 | + * @see org.springframework.boot.SpringApplication |
| 48 | + * @see org.springframework.boot.autoconfigure.SpringBootApplication |
| 49 | + * @see org.springframework.boot.context.properties.ConfigurationProperties |
| 50 | + * @see org.springframework.context.ApplicationContext |
| 51 | + * @see org.springframework.context.annotation.Bean |
| 52 | + * @see org.springframework.data.gemfire.CacheFactoryBean |
| 53 | + * @see org.springframework.data.gemfire.GemfireTemplate |
| 54 | + * @see org.springframework.data.gemfire.PartitionedRegionFactoryBean |
| 55 | + * @see com.gemstone.gemfire.cache.Cache |
| 56 | + * @see com.gemstone.gemfire.cache.CacheLoader |
| 57 | + * @see sample.data.geode.service.Calculator |
| 58 | + * @see sample.data.geode.service.factory.CalculatorFactory |
| 59 | + * @since 1.4.0 |
| 60 | + */ |
| 61 | +@SpringBootApplication |
| 62 | +@ConfigurationProperties |
| 63 | +@SuppressWarnings("unused") |
| 64 | +public class SampleDataGeodeApplication { |
| 65 | + |
| 66 | + protected static final String CALCULATIONS_REGION_NAME = "Calculations"; |
| 67 | + |
| 68 | + public static void main(final String[] args) { |
| 69 | + SpringApplication.run(SampleDataGeodeApplication.class, args); |
| 70 | + } |
| 71 | + |
| 72 | + @Bean |
| 73 | + Properties gemfireProperties(@Value("${sample.data.geode.log.level:config}") String logLevel) { |
| 74 | + Properties gemfireProperties = new Properties(); |
| 75 | + |
| 76 | + gemfireProperties.setProperty("name", SampleDataGeodeApplication.class.getSimpleName()); |
| 77 | + gemfireProperties.setProperty("mcast-port", "0"); |
| 78 | + gemfireProperties.setProperty("log-level", logLevel); |
| 79 | + |
| 80 | + return gemfireProperties; |
| 81 | + } |
| 82 | + |
| 83 | + @Bean |
| 84 | + CacheFactoryBean gemfireCache(@Qualifier("gemfireProperties") Properties gemfireProperties) { |
| 85 | + CacheFactoryBean gemfireCache = new CacheFactoryBean(); |
| 86 | + |
| 87 | + gemfireCache.setClose(true); |
| 88 | + gemfireCache.setProperties(gemfireProperties); |
| 89 | + |
| 90 | + return gemfireCache; |
| 91 | + } |
| 92 | + |
| 93 | + @Bean(name = CALCULATIONS_REGION_NAME) |
| 94 | + PartitionedRegionFactoryBean<Long, Long> calculationsRegion(Cache gemfireCache, |
| 95 | + RegionAttributes<Long, Long> calculationsRegionAttributes, |
| 96 | + @Qualifier("calculationsRegionLoader") CacheLoader<Long, Long> calculationsLoader) { |
| 97 | + |
| 98 | + PartitionedRegionFactoryBean<Long, Long> calculationsRegion = new PartitionedRegionFactoryBean<Long, Long>(); |
| 99 | + |
| 100 | + calculationsRegion.setAttributes(calculationsRegionAttributes); |
| 101 | + calculationsRegion.setCache(gemfireCache); |
| 102 | + calculationsRegion.setCacheLoader(calculationsLoader); |
| 103 | + calculationsRegion.setClose(false); |
| 104 | + calculationsRegion.setName(CALCULATIONS_REGION_NAME); |
| 105 | + calculationsRegion.setPersistent(false); |
| 106 | + |
| 107 | + return calculationsRegion; |
| 108 | + } |
| 109 | + |
| 110 | + @Bean |
| 111 | + @SuppressWarnings("unchecked") |
| 112 | + RegionAttributesFactoryBean calculationsRegionAttributes() { |
| 113 | + RegionAttributesFactoryBean calculationsRegionAttributes = new RegionAttributesFactoryBean(); |
| 114 | + |
| 115 | + calculationsRegionAttributes.setKeyConstraint(Long.class); |
| 116 | + calculationsRegionAttributes.setKeyConstraint(Long.class); |
| 117 | + |
| 118 | + return calculationsRegionAttributes; |
| 119 | + } |
| 120 | + |
| 121 | + @Bean |
| 122 | + CacheLoader calculationsRegionLoader(@Qualifier("calculatorResolver") final Calculator<Long, Long> calculator) { |
| 123 | + return new CacheLoader<Long, Long>() { |
| 124 | + @Override public Long load(LoaderHelper<Long, Long> loaderHelper) throws CacheLoaderException { |
| 125 | + Long operand = loaderHelper.getKey(); |
| 126 | + return calculator.calculate(operand); |
| 127 | + } |
| 128 | + |
| 129 | + @Override public void close() { |
| 130 | + } |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + @Bean |
| 135 | + GemfireTemplate calculationsRegionTemplate(Cache gemfireCache) { |
| 136 | + return new GemfireTemplate(gemfireCache.getRegion(CALCULATIONS_REGION_NAME)); |
| 137 | + } |
| 138 | + |
| 139 | + @Bean |
| 140 | + @SuppressWarnings("unchecked") |
| 141 | + Calculator<Long, Long> calculatorResolver(ApplicationContext context, |
| 142 | + @Value("${sample.data.geode.calculator:factorial}") String beanName) { |
| 143 | + return context.getBean(beanName, Calculator.class); |
| 144 | + } |
| 145 | + |
| 146 | + @Bean |
| 147 | + Calculator<Long, Long> addition() { |
| 148 | + return CalculatorFactory.addition(); |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + Calculator<Long, Long> factorial() { |
| 153 | + return CalculatorFactory.factorial(); |
| 154 | + } |
| 155 | + |
| 156 | + @Bean |
| 157 | + Calculator<Long, Long> multiplication() { |
| 158 | + return CalculatorFactory.multiplication(); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments