#539 - Polishing.

Add author tags. Simplify dependency setup. Replace logger declarations with Lombok's CommonsLog. Simplify test annotations.

Disable WAN module as the WAN server does not stop after running tests. Reformat code.
This commit is contained in:
Mark Paluch
2020-02-27 10:21:46 +01:00
parent fa0021cffb
commit 54f7146e0f
121 changed files with 1160 additions and 1092 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event;
import lombok.Data;
@@ -44,4 +43,4 @@ public class Customer implements Serializable {
this.firstName = firstName;
this.lastName = lastName;
}
}
}

View File

@@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}
/**
* @author Patrick Johnson
*/
public interface CustomerRepository extends CrudRepository<Customer, Long> {}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event;
import lombok.Data;
@@ -28,9 +27,10 @@ import java.io.Serializable;
*/
@Data
public class EmailAddress implements Serializable {
private String value;
public EmailAddress(String value) {
this.value = value;
}
}
}

View File

@@ -20,8 +20,12 @@ import org.apache.geode.cache.wan.GatewayEventFilter;
import org.apache.geode.cache.wan.GatewayQueueEvent;
import org.springframework.stereotype.Component;
/**
* @author Patrick Johnson
*/
@Component
public class EvenNumberedKeyWanEventFilter implements GatewayEventFilter {
@Override
public boolean beforeEnqueue(GatewayQueueEvent event) {
return (Long) event.getKey() % 2 == 0;
@@ -33,7 +37,5 @@ public class EvenNumberedKeyWanEventFilter implements GatewayEventFilter {
}
@Override
public void afterAcknowledgement(GatewayQueueEvent event) {
}
public void afterAcknowledgement(GatewayQueueEvent event) {}
}

View File

@@ -13,20 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.server;
import example.springdata.geode.server.wan.event.Customer;
import org.apache.geode.cache.EntryEvent;
import org.apache.geode.cache.wan.GatewayEventSubstitutionFilter;
import org.springframework.stereotype.Component;
/**
* @author Patrick Johnson
*/
@Component
public class WanEventSubstitutionFilter implements GatewayEventSubstitutionFilter<Long, Customer> {
@Override
public Object getSubstituteValue(EntryEvent<Long, Customer> event) {
Customer customer = event.getNewValue();
return new Customer(customer.getId(), customer.getEmailAddress(), customer.getFirstName(), customer.getLastName().substring(0, 1));
return new Customer(customer.getId(), customer.getEmailAddress(), customer.getFirstName(),
customer.getLastName().substring(0, 1));
}
}

View File

@@ -13,17 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.server;
import com.github.javafaker.Faker;
import com.github.javafaker.Internet;
import com.github.javafaker.Name;
import example.springdata.geode.server.wan.event.Customer;
import example.springdata.geode.server.wan.event.CustomerRepository;
import example.springdata.geode.server.wan.event.EmailAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.apachecommons.CommonsLog;
import java.util.Scanner;
import java.util.stream.LongStream;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -31,23 +30,23 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import java.util.Scanner;
import java.util.stream.LongStream;
import com.github.javafaker.Faker;
import com.github.javafaker.Internet;
import com.github.javafaker.Name;
/**
* @author Patrick Johnson
*/
@SpringBootApplication(scanBasePackageClasses = WanServerConfig.class)
@CommonsLog
public class WanServer {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
new SpringApplicationBuilder(WanServer.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
new SpringApplicationBuilder(WanServer.class).web(WebApplicationType.NONE).build().run(args);
}
@Bean
@Profile({"default", "SiteA"})
@Profile({ "default", "SiteA" })
public ApplicationRunner siteARunner() {
return args -> new Scanner(System.in).nextLine();
}
@@ -56,7 +55,7 @@ public class WanServer {
@Profile("SiteB")
public ApplicationRunner siteBRunner(CustomerRepository customerRepository) {
return args -> {
logger.info("Inserting 300 customers");
log.info("Inserting 300 customers");
createCustomers(customerRepository);
};
}
@@ -65,8 +64,7 @@ public class WanServer {
Faker faker = new Faker();
Name fakerName = faker.name();
Internet fakerInternet = faker.internet();
LongStream.range(0, 300).forEach(index ->
repository.save(new Customer(index,
new EmailAddress(fakerInternet.emailAddress()), fakerName.firstName(), fakerName.lastName())));
LongStream.range(0, 300).forEach(index -> repository.save(new Customer(index,
new EmailAddress(fakerInternet.emailAddress()), fakerName.firstName(), fakerName.lastName())));
}
}

View File

@@ -13,19 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.server;
import com.github.javafaker.Faker;
import example.springdata.geode.server.wan.event.Customer;
import example.springdata.geode.server.wan.event.CustomerRepository;
import example.springdata.geode.server.wan.event.server.siteA.SiteAWanEnabledServerConfig;
import example.springdata.geode.server.wan.event.server.siteB.SiteBWanServerConfig;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.PartitionAttributes;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.wan.GatewaySender;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@@ -35,51 +39,59 @@ import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import java.io.File;
import java.util.Arrays;
import com.github.javafaker.Faker;
/**
* @author Patrick Johnson
*/
@Configuration
@EnableGemfireRepositories(basePackageClasses = CustomerRepository.class)
@Import({SiteAWanEnabledServerConfig.class, SiteBWanServerConfig.class})
@Import({ SiteAWanEnabledServerConfig.class, SiteBWanServerConfig.class })
public class WanServerConfig {
@Bean
Faker faker() {
return new Faker();
}
@Bean(name = "DiskStore")
DiskStoreFactoryBean diskStore(GemFireCache gemFireCache, Faker faker) {
final DiskStoreFactoryBean diskStoreFactoryBean = new DiskStoreFactoryBean();
final boolean completed = new File("/tmp/" + faker.name().firstName()).mkdirs();
final DiskStoreFactoryBean.DiskDir[] diskDirs = {new DiskStoreFactoryBean.DiskDir("/tmp/" + faker.name().firstName())};
DiskStoreFactoryBean diskStore(GemFireCache gemFireCache, Faker faker) throws IOException {
DiskStoreFactoryBean diskStoreFactoryBean = new DiskStoreFactoryBean();
File tempDirectory = File.createTempFile(faker.name().firstName(), faker.name().firstName());
tempDirectory.delete();
tempDirectory.mkdirs();
tempDirectory.deleteOnExit();
DiskStoreFactoryBean.DiskDir[] diskDirs = { new DiskStoreFactoryBean.DiskDir(tempDirectory.getAbsolutePath()) };
diskStoreFactoryBean.setDiskDirs(Arrays.asList(diskDirs));
diskStoreFactoryBean.setCache(gemFireCache);
return diskStoreFactoryBean;
}
@Bean
RegionAttributesFactoryBean<Long, Customer> regionAttributes(PartitionAttributes<Long, Customer> partitionAttributes) {
final RegionAttributesFactoryBean<Long, Customer> regionAttributesFactoryBean = new RegionAttributesFactoryBean<>();
RegionAttributesFactoryBean<Long, Customer> regionAttributes(
PartitionAttributes<Long, Customer> partitionAttributes) {
RegionAttributesFactoryBean<Long, Customer> regionAttributesFactoryBean = new RegionAttributesFactoryBean<>();
regionAttributesFactoryBean.setPartitionAttributes(partitionAttributes);
return regionAttributesFactoryBean;
}
@Bean
PartitionAttributesFactoryBean<Long, Customer> partitionAttributes() {
final PartitionAttributesFactoryBean<Long, Customer> partitionAttributesFactoryBean = new PartitionAttributesFactoryBean<>();
PartitionAttributesFactoryBean<Long, Customer> partitionAttributesFactoryBean = new PartitionAttributesFactoryBean<>();
partitionAttributesFactoryBean.setTotalNumBuckets(13);
partitionAttributesFactoryBean.setRedundantCopies(0);
return partitionAttributesFactoryBean;
}
@Bean("Customers")
PartitionedRegionFactoryBean<Long, Customer> createCustomerRegion(GemFireCache gemFireCache, RegionAttributes<Long, Customer> regionAttributes, GatewaySender gatewaySender) {
final PartitionedRegionFactoryBean<Long, Customer> partitionedRegionFactoryBean = new PartitionedRegionFactoryBean<>();
PartitionedRegionFactoryBean<Long, Customer> createCustomerRegion(GemFireCache gemFireCache,
RegionAttributes<Long, Customer> regionAttributes, GatewaySender gatewaySender) {
PartitionedRegionFactoryBean<Long, Customer> partitionedRegionFactoryBean = new PartitionedRegionFactoryBean<>();
partitionedRegionFactoryBean.setCache(gemFireCache);
partitionedRegionFactoryBean.setRegionName("Customers");
partitionedRegionFactoryBean.setDataPolicy(DataPolicy.PARTITION);
partitionedRegionFactoryBean.setAttributes(regionAttributes);
partitionedRegionFactoryBean.setGatewaySenders(new GatewaySender[]{gatewaySender});
partitionedRegionFactoryBean.setGatewaySenders(new GatewaySender[] { gatewaySender });
return partitionedRegionFactoryBean;
}
}

View File

@@ -13,13 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.server;
import org.apache.geode.cache.wan.GatewayTransportFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import lombok.extern.apachecommons.CommonsLog;
import java.io.InputStream;
import java.io.OutputStream;
@@ -27,22 +23,28 @@ import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import org.apache.geode.cache.wan.GatewayTransportFilter;
import org.springframework.stereotype.Component;
/**
* @author Patrick Johnson
*/
@Component
@CommonsLog
public class WanTransportEncryptionListener implements GatewayTransportFilter {
private final Adler32 CHECKER = new Adler32();
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public InputStream getInputStream(InputStream stream) {
logger.info("CheckedTransportFilter: Getting input stream");
log.info("CheckedTransportFilter: Getting input stream");
return new CheckedInputStream(stream, CHECKER);
}
@Override
public OutputStream getOutputStream(OutputStream stream) {
logger.info("CheckedTransportFilter: Getting output stream");
log.info("CheckedTransportFilter: Getting output stream");
return new CheckedOutputStream(stream, CHECKER);
}
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.server.siteA;
import org.apache.geode.cache.Cache;
@@ -28,15 +27,19 @@ import org.springframework.data.gemfire.config.annotation.EnableLocator;
import org.springframework.data.gemfire.wan.GatewayReceiverFactoryBean;
import org.springframework.data.gemfire.wan.GatewaySenderFactoryBean;
/**
* @author Patrick Johnson
*/
@Configuration
@CacheServerApplication(port = 0, locators = "localhost[10334]", name = "SiteA_Server", logLevel = "error")
@Profile({"default", "SiteA"})
@Profile({ "default", "SiteA" })
@EnableLocator
@EnableGemFireProperties(distributedSystemId = 1, remoteLocators = "localhost[20334]")
public class SiteAWanEnabledServerConfig {
@Bean
GatewayReceiverFactoryBean createGatewayReceiver(GemFireCache gemFireCache) {
final GatewayReceiverFactoryBean gatewayReceiverFactoryBean = new GatewayReceiverFactoryBean((Cache) gemFireCache);
GatewayReceiverFactoryBean gatewayReceiverFactoryBean = new GatewayReceiverFactoryBean((Cache) gemFireCache);
gatewayReceiverFactoryBean.setStartPort(15000);
gatewayReceiverFactoryBean.setEndPort(15010);
gatewayReceiverFactoryBean.setManualStart(false);
@@ -46,7 +49,7 @@ public class SiteAWanEnabledServerConfig {
@Bean
@DependsOn("DiskStore")
GatewaySenderFactoryBean createGatewaySender(GemFireCache gemFireCache) {
final GatewaySenderFactoryBean gatewaySenderFactoryBean = new GatewaySenderFactoryBean(gemFireCache);
GatewaySenderFactoryBean gatewaySenderFactoryBean = new GatewaySenderFactoryBean(gemFireCache);
gatewaySenderFactoryBean.setBatchSize(15);
gatewaySenderFactoryBean.setBatchTimeInterval(1000);
gatewaySenderFactoryBean.setRemoteDistributedSystemId(2);
@@ -54,4 +57,4 @@ public class SiteAWanEnabledServerConfig {
gatewaySenderFactoryBean.setDiskStoreRef("DiskStore");
return gatewaySenderFactoryBean;
}
}
}

View File

@@ -13,18 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.server.siteB;
import example.springdata.geode.server.wan.event.Customer;
import example.springdata.geode.server.wan.event.server.EvenNumberedKeyWanEventFilter;
import example.springdata.geode.server.wan.event.server.WanEventSubstitutionFilter;
import example.springdata.geode.server.wan.event.server.WanTransportEncryptionListener;
import java.util.Collections;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.wan.GatewayEventFilter;
import org.apache.geode.cache.wan.GatewayEventSubstitutionFilter;
import org.apache.geode.cache.wan.GatewayTransportFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@@ -36,18 +39,20 @@ import org.springframework.data.gemfire.config.annotation.EnableLocator;
import org.springframework.data.gemfire.wan.GatewayReceiverFactoryBean;
import org.springframework.data.gemfire.wan.GatewaySenderFactoryBean;
import java.util.Collections;
/**
* @author Patrick Johnson
*/
@Configuration
@CacheServerApplication(port = 0, locators = "localhost[20334]", name = "SiteB_Server", logLevel = "error")
@Profile("SiteB")
@EnableLocator(port = 20334)
@EnableGemFireProperties(distributedSystemId = 2, remoteLocators = "localhost[10334]")
@Import({EvenNumberedKeyWanEventFilter.class, WanEventSubstitutionFilter.class, WanTransportEncryptionListener.class})
@Import({ EvenNumberedKeyWanEventFilter.class, WanEventSubstitutionFilter.class, WanTransportEncryptionListener.class })
public class SiteBWanServerConfig {
@Bean
GatewayReceiverFactoryBean createGatewayReceiver(GemFireCache gemFireCache) {
final GatewayReceiverFactoryBean gatewayReceiverFactoryBean = new GatewayReceiverFactoryBean((Cache) gemFireCache);
GatewayReceiverFactoryBean gatewayReceiverFactoryBean = new GatewayReceiverFactoryBean((Cache) gemFireCache);
gatewayReceiverFactoryBean.setStartPort(25000);
gatewayReceiverFactoryBean.setEndPort(25010);
return gatewayReceiverFactoryBean;
@@ -56,8 +61,9 @@ public class SiteBWanServerConfig {
@Bean
@DependsOn("DiskStore")
GatewaySenderFactoryBean createGatewaySender(GemFireCache gemFireCache, GatewayEventFilter gatewayEventFilter,
GatewayTransportFilter gatewayTransportFilter, GatewayEventSubstitutionFilter<Long, Customer> gatewayEventSubstitutionFilter) {
final GatewaySenderFactoryBean gatewaySenderFactoryBean = new GatewaySenderFactoryBean(gemFireCache);
GatewayTransportFilter gatewayTransportFilter,
GatewayEventSubstitutionFilter<Long, Customer> gatewayEventSubstitutionFilter) {
GatewaySenderFactoryBean gatewaySenderFactoryBean = new GatewaySenderFactoryBean(gemFireCache);
gatewaySenderFactoryBean.setBatchSize(15);
gatewaySenderFactoryBean.setBatchTimeInterval(1000);
gatewaySenderFactoryBean.setRemoteDistributedSystemId(1);
@@ -68,4 +74,4 @@ public class SiteBWanServerConfig {
gatewaySenderFactoryBean.setPersistent(false);
return gatewaySenderFactoryBean;
}
}
}

View File

@@ -16,36 +16,36 @@
package example.springdata.geode.server.wan.event;
import static org.assertj.core.api.Assertions.*;
import example.springdata.geode.server.wan.event.client.WanClientConfig;
import example.springdata.geode.server.wan.event.server.WanServer;
import org.apache.geode.cache.Region;
import org.awaitility.Awaitility;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import lombok.extern.apachecommons.CommonsLog;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import javax.annotation.Resource;
import org.apache.geode.cache.Region;
import org.awaitility.Awaitility;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Patrick Johnson
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = WanClientConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@SpringBootTest(classes = WanClientConfig.class)
@CommonsLog
public class WanServerTests extends ForkingClientServerIntegrationTestsSupport {
@Resource(name = "Customers")
private Region<Long, Customer> customers;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource(name = "Customers") private Region<Long, Customer> customers;
@BeforeClass
public static void setup() throws IOException {
@@ -55,13 +55,16 @@ public class WanServerTests extends ForkingClientServerIntegrationTestsSupport {
}
@Test
@Ignore
public void wanReplicationOccursCorrectly() {
Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> customers.keySetOnServer().size() == 150);
assertThat(customers.keySetOnServer().size()).isEqualTo(150);
logger.info(customers.keySetOnServer().size() + " entries replicated to siteA");
customers.getAll(customers.keySetOnServer()).forEach((key, value) -> assertThat(value.getLastName().length()).isEqualTo(1));
logger.info("All customers' last names changed to last initial on siteA");
Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> customers.keySetOnServer().size() == 150);
assertThat(customers.keySetOnServer()).hasSize(150);
log.info(customers.keySetOnServer().size() + " entries replicated to siteA");
customers.getAll(customers.keySetOnServer())
.forEach((key, value) -> assertThat(value.getLastName().length()).isEqualTo(1));
log.info("All customers' last names changed to last initial on siteA");
}
}

View File

@@ -13,13 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.wan.event.client;
import example.springdata.geode.server.wan.event.Customer;
import example.springdata.geode.server.wan.event.CustomerRepository;
import java.util.Collections;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -29,8 +32,6 @@ import org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import org.springframework.data.gemfire.support.ConnectionEndpoint;
import java.util.Collections;
/**
* Spring JavaConfig configuration class to setup a Spring container and infrastructure components.
*
@@ -39,8 +40,10 @@ import java.util.Collections;
*/
@Configuration
@EnableGemfireRepositories(basePackageClasses = CustomerRepository.class)
@ClientCacheApplication(name = "WanClient", logLevel = "error", pingInterval = 5000L, readTimeout = 15000, retryAttempts = 1)
@ClientCacheApplication(name = "WanClient", logLevel = "error", pingInterval = 5000L, readTimeout = 15000,
retryAttempts = 1)
public class WanClientConfig {
@Bean("Customers")
protected ClientRegionFactoryBean<Long, Customer> configureProxyClientCustomerRegion(GemFireCache gemFireCache) {
ClientRegionFactoryBean<Long, Customer> clientRegionFactoryBean = new ClientRegionFactoryBean<>();
@@ -55,7 +58,7 @@ public class WanClientConfig {
@Value("${spring.data.geode.locator.host:localhost}") String hostname,
@Value("${spring.data.geode.locator.port:10334}") int port) {
return (beanName, clientCacheFactoryBean) -> clientCacheFactoryBean.setLocators(Collections.singletonList(
new ConnectionEndpoint(hostname, port)));
return (beanName, clientCacheFactoryBean) -> clientCacheFactoryBean
.setLocators(Collections.singletonList(new ConnectionEndpoint(hostname, port)));
}
}