|
| 1 | +package com.baeldung.spring.cloud.archaius.dynamosources.config; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +import org.apache.commons.configuration.AbstractConfiguration; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | + |
| 10 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 11 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; |
| 12 | +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; |
| 13 | +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; |
| 14 | +import com.amazonaws.services.dynamodbv2.util.TableUtils; |
| 15 | +import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusProperties; |
| 16 | +import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusPropertiesRepository; |
| 17 | +import com.netflix.config.DynamicConfiguration; |
| 18 | +import com.netflix.config.FixedDelayPollingScheduler; |
| 19 | +import com.netflix.config.PolledConfigurationSource; |
| 20 | +import com.netflix.config.sources.DynamoDbConfigurationSource; |
| 21 | + |
| 22 | +@Configuration |
| 23 | +public class ApplicationPropertiesConfigurations { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + AmazonDynamoDB amazonDynamoDb; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private ArchaiusPropertiesRepository repository; |
| 30 | + |
| 31 | + @Bean |
| 32 | + public AbstractConfiguration addApplicationPropertiesSource() { |
| 33 | + // Normally, the DB Table would be already created and populated. |
| 34 | + // In this case, we'll do it just before creating the archaius config source that uses it |
| 35 | + initDatabase(); |
| 36 | + PolledConfigurationSource source = new DynamoDbConfigurationSource(amazonDynamoDb); |
| 37 | + return new DynamicConfiguration(source, new FixedDelayPollingScheduler()); |
| 38 | + } |
| 39 | + |
| 40 | + private void initDatabase() { |
| 41 | + // Create the table |
| 42 | + DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDb); |
| 43 | + CreateTableRequest tableRequest = mapper.generateCreateTableRequest(ArchaiusProperties.class); |
| 44 | + tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); |
| 45 | + TableUtils.createTableIfNotExists(amazonDynamoDb, tableRequest); |
| 46 | + |
| 47 | + // Populate the table |
| 48 | + ArchaiusProperties property = new ArchaiusProperties("baeldung.archaius.properties.one", "one FROM:dynamoDB"); |
| 49 | + ArchaiusProperties property3 = new ArchaiusProperties("baeldung.archaius.properties.three", "three FROM:dynamoDB"); |
| 50 | + repository.saveAll(Arrays.asList(property, property3)); |
| 51 | + } |
| 52 | +} |
0 commit comments