Skip to content

Commit

Permalink
Merge 7c41710 into 76956e4
Browse files Browse the repository at this point in the history
  • Loading branch information
WillemJiang committed Nov 23, 2018
2 parents 76956e4 + 7c41710 commit 8deae21
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Expand Up @@ -32,7 +32,12 @@ public void doWith(Method method) throws IllegalArgumentException {
if (!method.isAnnotationPresent(Compensable.class)) {
return;
}
String compensationMethod = method.getAnnotation(Compensable.class).compensationMethod();
Compensable compensable = method.getAnnotation(Compensable.class);
String compensationMethod = compensable.compensationMethod();
// we don't support the retries number below -1.
if (compensable.retries() < -1) {
throw new IllegalArgumentException(String.format("Compensable %s of method %s, the retries should not below -1.", compensable, method.getName()));
}
loadMethodContext(method, compensationMethod);
}
}
Expand Up @@ -18,6 +18,7 @@
package org.apache.servicecomb.saga.omega.transaction.spring;

import static com.seanyinx.github.unit.scaffolding.AssertUtils.expectFailing;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
Expand All @@ -41,6 +42,19 @@ public void blowsUpWhenCompensableMethodIsNotFound() throws Exception {
}
}

@Test
public void blowsUpWhenCompensateRetriesIsBelowNegativeOne() throws Exception {
try {
try (ConfigurableApplicationContext ignored = new SpringApplicationBuilder(TransactionTestMain.class)
.profiles("annotation-retries-checking")
.run()) {
expectFailing(BeanCreationException.class);
}
} catch (BeanCreationException e) {
assertThat(e.getCause().getMessage(), endsWith("the retries should not below -1."));
}
}

@Test
public void blowsUpWhenAnnotationOnWrongType() throws Exception {
try {
Expand Down
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.servicecomb.saga.omega.transaction.spring;

import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Profile("annotation-retries-checking")
@Component
class MisconfiguredRetriesService {

@Compensable(retries = -2)
void doSomething() {
}
}
Expand Up @@ -24,6 +24,7 @@

/**
* Indicates the annotated method will start a sub-transaction. <br>
* It is suggested to use the Spring Transactional annotation to wrap the sub-transaction method.
* A <code>@Compensable</code> method should satisfy below requirements:
* <ol>
* <li>all parameters are serialized</li>
Expand All @@ -37,6 +38,15 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Compensable {

/**
* The retires number of the compensable method.
* Default value is 0, which means never retry it
* value is -1, which means retry it until succeed
* value > 0, which means the retry number
* value < -1, an IllegalArgumentException will be thrown
*
* @return
*/
int retries() default 0;

/**
Expand Down

0 comments on commit 8deae21

Please sign in to comment.