#539 - Add spring-data-geode-examples module.
This commit is contained in:
committed by
Mark Paluch
parent
08dce4f0f3
commit
fa0021cffb
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.geode.client.transactions;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.gemfire.mapping.annotation.Region;
|
||||
|
||||
/**
|
||||
* A customer used for Lucene examples.
|
||||
*
|
||||
* @author Udo Kohlmeyer
|
||||
* @author Patrick Johnson
|
||||
*/
|
||||
@Data
|
||||
@Region(name = "Customers")
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private EmailAddress emailAddress;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Customer(Long id, EmailAddress emailAddress, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.emailAddress = emailAddress;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.geode.client.transactions;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Value object to represent email addresses.
|
||||
*
|
||||
* @author Udo Kohlmeyer
|
||||
* @author Patrick Johnson
|
||||
*/
|
||||
@Data
|
||||
public class EmailAddress {
|
||||
private String value;
|
||||
|
||||
public EmailAddress(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* 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;
|
||||
|
||||
@ClientRegion("Customers")
|
||||
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
||||
|
||||
List<Customer> findAll();
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* 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;
|
||||
|
||||
@Service
|
||||
public class CustomerService {
|
||||
private final CustomerRepository customerRepository;
|
||||
|
||||
@Resource(name = "Customers")
|
||||
private Region<Long, Customer> customerRegion;
|
||||
|
||||
public CustomerService(CustomerRepository customerRepository, @Qualifier("Customers") Region<Long, Customer> customerRegion) {
|
||||
this.customerRepository = customerRepository;
|
||||
this.customerRegion = customerRegion;
|
||||
}
|
||||
|
||||
private CustomerRepository getCustomerRepository() {
|
||||
return customerRepository;
|
||||
}
|
||||
|
||||
public Optional<Customer> findById(long id) {
|
||||
return getCustomerRepository().findById(id);
|
||||
}
|
||||
|
||||
public int numberEntriesStoredOnServer() {
|
||||
return customerRegion.keySetOnServer().size();
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateCustomersSuccess() {
|
||||
customerRepository.save(new Customer(2L, new EmailAddress("2@2.com"), "Humpty", "Hamilton"));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateCustomersWithDelay(int millisDelay, Customer customer) throws InterruptedException {
|
||||
customerRepository.save(customer);
|
||||
Thread.sleep(millisDelay);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateCustomersFailure() {
|
||||
customerRepository.save(new Customer(2L, new EmailAddress("2@2.com"), "Numpty", "Hamilton"));
|
||||
throw new IllegalArgumentException("This should fail the transactions");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* 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;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
|
||||
import org.springframework.data.gemfire.config.annotation.EnableClusterDefinedRegions;
|
||||
import org.springframework.data.gemfire.config.annotation.EnablePdx;
|
||||
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
|
||||
import org.springframework.data.gemfire.transaction.config.EnableGemfireCacheTransactions;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@EnableClusterDefinedRegions(clientRegionShortcut = ClientRegionShortcut.PROXY)
|
||||
@EnableTransactionManagement
|
||||
@EnableGemfireCacheTransactions
|
||||
@Configuration
|
||||
@EnablePdx
|
||||
@ComponentScan(basePackageClasses = CustomerService.class)
|
||||
@EnableGemfireRepositories(basePackageClasses = CustomerRepository.class)
|
||||
@ClientCacheApplication(name = "TransactionalClient", logLevel = "error", pingInterval = 5000L, readTimeout = 15000, retryAttempts = 1)
|
||||
public class TransactionalClientConfig {
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.geode.client.transactions.server;
|
||||
|
||||
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;
|
||||
|
||||
@Component
|
||||
public class CustomerTransactionListener extends TransactionListenerAdapter {
|
||||
private Log log = LogFactory.getLog(CustomerTransactionListener.class);
|
||||
|
||||
@Override
|
||||
public void afterFailedCommit(TransactionEvent event) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterRollback(TransactionEvent event) {
|
||||
log.info("In afterRollback for entry(s) [" + event.getEvents().stream().map(this::getEventInfo).collect(Collectors.toList()) + "]");
|
||||
}
|
||||
|
||||
private String getEventInfo(CacheEvent cacheEvent) {
|
||||
if (cacheEvent instanceof TXEntryState.TxEntryEventImpl) {
|
||||
return ((TXEntryState.TxEntryEventImpl) cacheEvent).getNewValue().toString();
|
||||
} else {
|
||||
return cacheEvent.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.geode.client.transactions.server;
|
||||
|
||||
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;
|
||||
|
||||
@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)) {
|
||||
six_found.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
if (six_found.get()) {
|
||||
throw new TransactionWriterException("Customer for Key: 6 is being changed. Failing transaction");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* 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)
|
||||
public class TransactionalServer {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(TransactionalServer.class)
|
||||
.web(WebApplicationType.NONE)
|
||||
.build()
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* 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;
|
||||
import org.springframework.data.gemfire.config.annotation.EnableLocator;
|
||||
import org.springframework.data.gemfire.config.annotation.EnableManager;
|
||||
import org.springframework.data.gemfire.transaction.config.EnableGemfireCacheTransactions;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@EnableLocator
|
||||
@EnableTransactionManagement
|
||||
@EnableGemfireCacheTransactions
|
||||
@EnableManager(start = true)
|
||||
@CacheServerApplication(logLevel = "error")
|
||||
public class TransactionalServerConfig {
|
||||
|
||||
@Bean("Customers")
|
||||
ReplicatedRegionFactoryBean<Long, Customer> createCustomerRegion(GemFireCache gemfireCache) {
|
||||
ReplicatedRegionFactoryBean<Long, Customer> replicatedRegionFactoryBean = new ReplicatedRegionFactoryBean<>();
|
||||
replicatedRegionFactoryBean.setCache(gemfireCache);
|
||||
replicatedRegionFactoryBean.setRegionName("Customers");
|
||||
replicatedRegionFactoryBean.setDataPolicy(DataPolicy.REPLICATE);
|
||||
replicatedRegionFactoryBean.setScope(Scope.DISTRIBUTED_ACK);
|
||||
return replicatedRegionFactoryBean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* 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 example.springdata.geode.client.transactions.server.TransactionalServer;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = TransactionalClientConfig.class)
|
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
|
||||
public class TransactionalClientTests extends ForkingClientServerIntegrationTestsSupport {
|
||||
|
||||
@Autowired
|
||||
private CustomerService customerService;
|
||||
|
||||
@Resource(name = "Customers")
|
||||
private Region<Long, Customer> customers;
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws IOException {
|
||||
startGemFireServer(TransactionalServer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transactionsConfiguredCorrectly() throws InterruptedException {
|
||||
|
||||
logger.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());
|
||||
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);
|
||||
|
||||
try {
|
||||
customerService.updateCustomersFailure();
|
||||
} catch (IllegalArgumentException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
customer = customerService.findById(2L).get();
|
||||
assertThat(customer.getFirstName()).isEqualTo("Humpty");
|
||||
logger.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");
|
||||
customerService.updateCustomersWithDelay(1000, numpty);
|
||||
customerService.updateCustomersWithDelay(10, frumpy);
|
||||
customer = customerService.findById(2L).get();
|
||||
assertThat(customer).isEqualTo(frumpy);
|
||||
logger.info("Customer for ID after 2 updates with delay = " + customer);
|
||||
}
|
||||
}
|
||||
11
geode/transactions/src/test/resources/logback.xml
Normal file
11
geode/transactions/src/test/resources/logback.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="error">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user