为什么 jcabi-aspects 注释不起作用 [英] Why jcabi-aspects annotations doesn't work

查看:18
本文介绍了为什么 jcabi-aspects 注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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

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

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

But it runs the function just one time. This is the output:

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>

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

My program doesn't recognize any annotation not just the retry. How can I make it recognize the annotations? thank you

推荐答案

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

Having a similar issue. Added the annotation to a method on a project and tried to call it on a junit test, but it doesn't retry when occurs a mocked exception. It should try to execute twice.

我的方法

@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>

这篇关于为什么 jcabi-aspects 注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆