首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么jcabi-aspects注解不起作用

为什么jcabi-aspects注解不起作用
EN

Stack Overflow用户
提问于 2020-10-26 20:24:49
回答 1查看 55关注 0票数 0

我有这样的代码:

代码语言:javascript
复制
public static void main(String[] args)
{
    testAnnotation();
}

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

但它只运行函数一次。这是输出:

代码语言:javascript
复制
enter here
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.testAnnotation(Main.java:16)
at Main.main(Main.java:10)

这是我的pom:

代码语言:javascript
复制
<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>

再加上这个插件:

代码语言:javascript
复制
       <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>

除了重试之外,我的程序不能识别任何注释。我怎样才能让它识别注释呢?谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-10-19 20:50:10

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

我的方法

代码语言:javascript
复制
@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);
    }
}

我的测试

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64537064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档