Я использую оптимизацию c блокировки с версией. Поддерживать запись версий. Для обработки ObjectOptimisticLockingFailureException Я использую Spring Retry, но я хочу изменить версию записи после проверки обновленной версии из БД. Для которого я планирую использовать операцию Retry для добавления бизнес-логики c. Но не знаю, как это сделать.
Файл1: AppConfig. java
@Configuration
@EnableRetry
public class AppConfig {
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(2000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.registerListener(new DefaultListenerSupport());
return retryTemplate;
}
}
Файл2: DefaultListenerSupport. java
public class DefaultListenerSupport extends RetryListenerSupport {
private static final Logger logger = LoggerFactory.getLogger(DefaultListenerSupport.class);
@Override
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
logger.info("onClose");
super.close(context, callback, throwable);
}
@Override
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
logger.info("onError");
super.onError(context, callback, throwable);
}
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
logger.info("onOpen");
return super.open(context, callback);
}
}
Файл3: ProductService. java
public interface ProductService {
Product get(Long id);
@Retryable(value = { ObjectOptimisticLockingFailureException.class }, maxAttempts = 5, backoff = @Backoff(delay = 5000))
Product save(Product product) throws ObjectOptimisticLockingFailureException;
@Recover
Product recover(ObjectOptimisticLockingFailureException e,Product product);
}
File4: ProductServiceImpl. java
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductsRepository repo;
// private final ReentrantReadWriteLock rw = new ReentrantReadWriteLock();
@Override
public Product get(Long id) {
return repo.findOne(id);
}
@Override
@Transactional
public Product save(Product product) throws ObjectOptimisticLockingFailureException {
Product x = null;
try {
x = repo.save(product);
} catch (ObjectOptimisticLockingFailureException e) {
System.out.println("****************&&&&&&&&&&&&&&&&&&&&&&&&&&&&&************************");
throw e;
}
return x;
}
@Override
public Product recover(ObjectOptimisticLockingFailureException e, Product product) {
System.out.println("asdasdasdasdasdasdasdasd");
return null;
}
}
File5: Product. java
@Entity
public class Product {
@Id
@GeneratedValue
private Long id;
@Version
private Long version;
private String name;
private Long upc;
protected Product() {}
public Product(String name, Long upc) {
this.name = name;
this.upc = upc;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getUpc() {
return upc;
}
public void setUpc(Long upc) {
this.upc = upc;
}
}
File6: ProductsRepository. java
public interface ProductsRepository extends CrudRepository<Product, Long> { }