#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.client.transactions;
import lombok.Data;
@@ -42,4 +41,4 @@ public class Customer {
this.firstName = firstName;
this.lastName = lastName;
}
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions;
import lombok.Data;
@@ -26,9 +25,10 @@ import lombok.Data;
*/
@Data
public class EmailAddress {
private String value;
public EmailAddress(String value) {
this.value = value;
}
}
}

View File

@@ -13,17 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.client;
import example.springdata.geode.client.transactions.Customer;
import org.springframework.data.gemfire.mapping.annotation.ClientRegion;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
/**
* @author Patrick Johnson
*/
@ClientRegion("Customers")
public interface CustomerRepository extends CrudRepository<Customer, Long> {
List<Customer> findAll();
}
public interface CustomerRepository extends CrudRepository<Customer, Long> {}

View File

@@ -13,30 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.client;
import example.springdata.geode.client.transactions.Customer;
import example.springdata.geode.client.transactions.EmailAddress;
import org.apache.geode.cache.Region;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.geode.cache.Region;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Patrick Johnson
*/
@Service
public class CustomerService {
private final CustomerRepository customerRepository;
@Resource(name = "Customers")
private Region<Long, Customer> customerRegion;
@Resource(name = "Customers") private Region<Long, Customer> customerRegion;
public CustomerService(CustomerRepository customerRepository, @Qualifier("Customers") Region<Long, Customer> customerRegion) {
public CustomerService(CustomerRepository customerRepository,
@Qualifier("Customers") Region<Long, Customer> customerRegion) {
this.customerRepository = customerRepository;
this.customerRegion = customerRegion;
}
@@ -55,13 +60,13 @@ public class CustomerService {
@Transactional
public List<Customer> createFiveCustomers() {
return Arrays.stream(new Customer[]{new Customer(1L, new EmailAddress("1@1.com"), "John", "Melloncamp"),
new Customer(2L, new EmailAddress("2@2.com"), "Franky", "Hamilton"),
new Customer(3L, new EmailAddress("3@3.com"), "Sebastian", "Horner"),
new Customer(4L, new EmailAddress("4@4.com"), "Chris", "Vettel"),
new Customer(5L, new EmailAddress("5@5.com"), "Kimi", "Rosberg")})
.map(customerRepository::save)
.collect(Collectors.toList());
return Arrays
.stream(new Customer[] { new Customer(1L, new EmailAddress("1@1.com"), "John", "Melloncamp"),
new Customer(2L, new EmailAddress("2@2.com"), "Franky", "Hamilton"),
new Customer(3L, new EmailAddress("3@3.com"), "Sebastian", "Horner"),
new Customer(4L, new EmailAddress("4@4.com"), "Chris", "Vettel"),
new Customer(5L, new EmailAddress("5@5.com"), "Kimi", "Rosberg") })
.map(customerRepository::save).collect(Collectors.toList());
}
@Transactional
@@ -78,6 +83,6 @@ public class CustomerService {
@Transactional
public void updateCustomersFailure() {
customerRepository.save(new Customer(2L, new EmailAddress("2@2.com"), "Numpty", "Hamilton"));
throw new IllegalArgumentException("This should fail the transactions");
throw new IllegalArgumentException("This is an expected exception that should fail the transactions");
}
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.client;
import org.apache.geode.cache.client.ClientRegionShortcut;
@@ -26,6 +25,9 @@ import org.springframework.data.gemfire.repository.config.EnableGemfireRepositor
import org.springframework.data.gemfire.transaction.config.EnableGemfireCacheTransactions;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Patrick Johnson
*/
@EnableClusterDefinedRegions(clientRegionShortcut = ClientRegionShortcut.PROXY)
@EnableTransactionManagement
@EnableGemfireCacheTransactions
@@ -33,6 +35,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnablePdx
@ComponentScan(basePackageClasses = CustomerService.class)
@EnableGemfireRepositories(basePackageClasses = CustomerRepository.class)
@ClientCacheApplication(name = "TransactionalClient", logLevel = "error", pingInterval = 5000L, readTimeout = 15000, retryAttempts = 1)
public class TransactionalClientConfig {
}
@ClientCacheApplication(name = "TransactionalClient", logLevel = "error", pingInterval = 5000L, readTimeout = 15000,
retryAttempts = 1)
public class TransactionalClientConfig {}

View File

@@ -13,22 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.server;
import java.util.stream.Collectors;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geode.cache.CacheEvent;
import org.apache.geode.cache.TransactionEvent;
import org.apache.geode.cache.util.TransactionListenerAdapter;
import org.apache.geode.internal.cache.TXEntryState;
import org.springframework.stereotype.Component;
import java.util.stream.Collectors;
/**
* @author Patrick Johnson
*/
@Component
@CommonsLog
public class CustomerTransactionListener extends TransactionListenerAdapter {
private Log log = LogFactory.getLog(CustomerTransactionListener.class);
@Override
public void afterFailedCommit(TransactionEvent event) {
@@ -37,7 +41,8 @@ public class CustomerTransactionListener extends TransactionListenerAdapter {
@Override
public void afterRollback(TransactionEvent event) {
log.info("In afterRollback for entry(s) [" + event.getEvents().stream().map(this::getEventInfo).collect(Collectors.toList()) + "]");
log.info("In afterRollback for entry(s) ["
+ event.getEvents().stream().map(this::getEventInfo).collect(Collectors.toList()) + "]");
}
private String getEventInfo(CacheEvent cacheEvent) {
@@ -47,4 +52,4 @@ public class CustomerTransactionListener extends TransactionListenerAdapter {
return cacheEvent.toString();
}
}
}
}

View File

@@ -13,26 +13,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.server;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.geode.cache.TransactionEvent;
import org.apache.geode.cache.TransactionWriter;
import org.apache.geode.cache.TransactionWriterException;
import org.apache.geode.internal.cache.TXEntryState;
import org.apache.geode.internal.cache.TXEvent;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author Patrick Johnson
*/
@Component
public class CustomerTransactionWriter implements TransactionWriter {
@Override
public void beforeCommit(TransactionEvent transactionEvent) throws TransactionWriterException {
AtomicBoolean six_found = new AtomicBoolean(false);
((TXEvent) transactionEvent).getEvents().forEach(event -> {
if (event instanceof TXEntryState.TxEntryEventImpl && ((TXEntryState.TxEntryEventImpl) event).getKey().equals(6L)) {
if (event instanceof TXEntryState.TxEntryEventImpl
&& ((TXEntryState.TxEntryEventImpl) event).getKey().equals(6L)) {
six_found.set(true);
}
});
@@ -41,4 +47,4 @@ public class CustomerTransactionWriter implements TransactionWriter {
throw new TransactionWriterException("Customer for Key: 6 is being changed. Failing transaction");
}
}
}
}

View File

@@ -13,19 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.server;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication(scanBasePackageClasses = TransactionalServerConfig.class)
/**
* @author Patrick Johnson
*/
@SpringBootApplication
public class TransactionalServer {
public static void main(String[] args) {
new SpringApplicationBuilder(TransactionalServer.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
new SpringApplicationBuilder(TransactionalServer.class).web(WebApplicationType.NONE).build().run(args);
}
}
}

View File

@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.server;
import example.springdata.geode.client.transactions.Customer;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Scope;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
@@ -28,6 +29,9 @@ import org.springframework.data.gemfire.config.annotation.EnableManager;
import org.springframework.data.gemfire.transaction.config.EnableGemfireCacheTransactions;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Patrick Johnson
*/
@EnableLocator
@EnableTransactionManagement
@EnableGemfireCacheTransactions

View File

@@ -13,41 +13,40 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.client.transactions.client;
import static org.assertj.core.api.Assertions.*;
import example.springdata.geode.client.transactions.Customer;
import example.springdata.geode.client.transactions.EmailAddress;
import example.springdata.geode.client.transactions.server.TransactionalServer;
import lombok.extern.apachecommons.CommonsLog;
import java.io.IOException;
import javax.annotation.Resource;
import org.apache.geode.cache.Region;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.annotation.Resource;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Patrick Johnson
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = TransactionalClientConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@SpringBootTest(classes = TransactionalClientConfig.class)
@CommonsLog
public class TransactionalClientTests extends ForkingClientServerIntegrationTestsSupport {
@Autowired
private CustomerService customerService;
@Autowired private CustomerService customerService;
@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 {
@@ -57,26 +56,26 @@ public class TransactionalClientTests extends ForkingClientServerIntegrationTest
@Test
public void transactionsConfiguredCorrectly() throws InterruptedException {
logger.info("Number of Entries stored before = " + customerService.numberEntriesStoredOnServer());
log.info("Number of Entries stored before = " + customerService.numberEntriesStoredOnServer());
customerService.createFiveCustomers();
assertThat(customerService.numberEntriesStoredOnServer()).isEqualTo(5);
logger.info("Number of Entries stored after = " + customerService.numberEntriesStoredOnServer());
logger.info("Customer for ID before (transaction commit success) = " + customerService.findById(2L).get());
log.info("Number of Entries stored after = " + customerService.numberEntriesStoredOnServer());
log.info("Customer for ID before (transaction commit success) = " + customerService.findById(2L).get());
customerService.updateCustomersSuccess();
assertThat(customerService.numberEntriesStoredOnServer()).isEqualTo(5);
Customer customer = customerService.findById(2L).get();
assertThat(customer.getFirstName()).isEqualTo("Humpty");
logger.info("Customer for ID after (transaction commit success) = " + customer);
log.info("Customer for ID after (transaction commit success) = " + customer);
try {
customerService.updateCustomersFailure();
} catch (IllegalArgumentException exception) {
exception.printStackTrace();
// do not print the exception to not spam the log
}
customer = customerService.findById(2L).get();
assertThat(customer.getFirstName()).isEqualTo("Humpty");
logger.info("Customer for ID after (transaction commit failure) = " + customerService.findById(2L).get());
log.info("Customer for ID after (transaction commit failure) = " + customerService.findById(2L).get());
Customer numpty = new Customer(2L, new EmailAddress("2@2.com"), "Numpty", "Hamilton");
Customer frumpy = new Customer(2L, new EmailAddress("2@2.com"), "Frumpy", "Hamilton");
@@ -84,6 +83,6 @@ public class TransactionalClientTests extends ForkingClientServerIntegrationTest
customerService.updateCustomersWithDelay(10, frumpy);
customer = customerService.findById(2L).get();
assertThat(customer).isEqualTo(frumpy);
logger.info("Customer for ID after 2 updates with delay = " + customer);
log.info("Customer for ID after 2 updates with delay = " + customer);
}
}
}