Reputation: 3952
I want to manually call for a retry on a method by using JCabi. Aspect oriented programming should make this easy but I can't figure it out.
import com.jcabi.aspects.RetryOnFailure;
public class Example
{
public int j;
@RetryOnFailure(attempts = 4, delay = 100, verbose = true)
public void retryFun() throws Exception
{
j++;
if(j<3)
throw new Exception();
else
return;
}
public static void main(String[] args) throws Exception
{
Example example = new Example();
System.out.println(example.j);
example.retryFun();
System.out.println(example.j);
}
}
The only example available from jcabi is this one below which doesn't show how to throw an exception to force retry call:
Annotate your methods with @RetryOnFailure annotation and in case of exception in the method its execution will be repeated a few times:
public class Resource { @RetryOnFailure(attempts = 2, delay = 10, verbose = false) public String load(URL url) { return url.openConnection().getContent(); } }
In an exception occurs the method will retry two times, with a 10 msec delay between attempts.
Upvotes: 8
Views: 4085
Reputation: 5135
Alternatives, you can add these configuration to pom.xml to weave at compilation time.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<showWeaveInfo>true</showWeaveInfo>
<source>1.7</source>
<target>1.7</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>
Reference: http://aspects.jcabi.com/example-aspectj.html
Upvotes: 0
Reputation: 2676
For those of you out there still looking for an answer, Yegor's answer is now outdated. The jcabi-maven-plugin version 0.8
he posted wasn't working for me.
After some hours of digging I have found this which states that we should use the last version As of this time, July 2014 is 0.9.2
.
That was my answer for why when running mvn jcabi:ajc I was getting errors from the link and also the weaving not working.
Upvotes: 8
Reputation: 105153
Indeed, just using jcabi annotations is not enough. You should "weave" your source code or binaries. I would recommend to weave binaries, as explained here: http://aspects.jcabi.com/example-weaving.html. Add this plugin to your pom.xml
and you're done:
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.8</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
Upvotes: 3
Reputation: 3952
Ok unfortunately I found out these jcabi aspects need a custom pom.xml task to compile the required aspects in the project.
So dropping the jcabi jar inside NetBeans and compiling the above code is not enough.
Upvotes: 0