0

我有这个代码:

public static void main(String[] args)
{
    testAnnotation();
}

@RetryOnFailure(attempts = 2)
public static void testAnnotation() {
    System.out.println("enter here");
    int x = 1/0;
}

但它只运行一次该功能。这是输出:

enter here
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.testAnnotation(Main.java:16)
at Main.main(Main.java:10)

这是我的pom:

<dependency>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-aspects</artifactId>
        <version>0.22.6</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.2</version>
        <scope>runtime</scope>
    </dependency>

加上这个插件:

       <plugin>
            <groupId>com.jcabi</groupId>
            <artifactId>jcabi-maven-plugin</artifactId>
            <version>0.14.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>ajc</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.9.2</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>1.9.2</version>
                </dependency>
            </dependencies>
        </plugin>

我的程序不识别任何注释,而不仅仅是重试。我怎样才能让它识别注释?谢谢你

4

1 回答 1

0

有类似的问题。将注释添加到项目的方法中并尝试在 junit 测试中调用它,但在发生模拟异常时不会重试。它应该尝试执行两次。

我的方法

@RetryOnFailure(attempts = 2, //
    delay = 5, //
    unit = TimeUnit.SECONDS, //
    types = {DataRequestException.class, HttpHostConnectException.class})
protected String post(final String postContent, final String url, final CloseableHttpClient httpClient,
    final Map<String, String> headers) throws DataRequestException {

    final HttpPost httpPost = new HttpPost(url);
    headers.forEach(httpPost::addHeader);
    httpPost.setEntity(new StringEntity(postContent, StandardCharsets.UTF_8));

    try (final CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
        return handleResponse(httpResponse);
    } catch (final DataRequestException e) {
        e.setRequest(postContent);
        throw e;
    } catch (final Exception e) {
        throw new DataRequestException(e, postContent);
    }
}

我的测试

CloseableHttpClient httpClient;

@Before
public void setup() {
    httpClient = Mockito.mock(CloseableHttpClient.class);
}


@Test
public void test() throws ClientProtocolException, IOException {
    assertThrows(DataRequestException.class, () -> {
        Mockito.when(httpClient.execute(Mockito.any(HttpPost.class))).thenThrow(DataRequestException.class);
        new RestRequest().post("", "http://teste.com", httpClient, new HashMap<>());
    });

    Mockito.verify(httpClient, Mockito.times(2)).execute(Mockito.any(HttpPost.class));
}

pom.xml

<dependencies>
    [...]
    <dependency>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-aspects</artifactId>
        <version>0.22.6</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jcabi</groupId>
                <artifactId>jcabi-maven-plugin</artifactId>
                <version>0.14.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>ajc</goal>
                        </goals>
                        <configuration>
                            <aspectsDirectories>
                                <directory>src/main/java</directory>
                            </aspectsDirectories>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.jcabi</groupId>
                        <artifactId>jcabi-aspects</artifactId>
                        <version>0.22.6</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>1.9.1</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>1.8</source>
                    <target>1.8</target>
                    <verbose>true</verbose>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>com.jcabi</groupId>
                            <artifactId>jcabi-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <id>weave-classes</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.3</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
于 2021-10-19T20:50:09.823 回答