Useful Java AOP Aspects:
Useful Java AOP Aspects is a collection of useful AOP aspects and Java annotations which allow you to modify the behavior of your Java application without writing lots of duplicate code.
For example, you may want to retry HTTP resource downloading in case of a failure. You can implement a full do/while
cycle yourself or you can annotate the method with @RetryOnFailure
and let one of our AOP aspects do the work for you:
public class MyResource { @RetryOnFailure public String load(URL url) { return url.openConnection().getContent(); } }
jcabi-aspects
works together only with AspectJ, an aspect oriented programming (AOP) framework. At the moment, we offer the following aspects (we extend this list every few months):
@Async
— Executes methods asynchronously.@Cacheable
— Checks method results in memory.@Immutable
— Guarantees class true immutability in runtime.@LogExceptions
— logs exceptions viaLogger
.@Quietly
— Swallows all exceptions quietly.@Loggable
— Logs method calls viaLogger
.@Parallel
— Executes method in multiple threads.@RetryOnFailure
— Retries the method execution a few times.@ScheduleWithFixedDelay
— Schedules class execution in multiple threads.@Timeable
— Interrupts a method if it exceeds an allowed time frame.@UnitedThrow
— Encapsulates all exceptions into allowed one.
After you add these annotations to your code, you should configure your build system to "weave" your binaries after compilation. (This is mandatory! Without this step, annotations will have absolutely no effect)!
If you have any questions, please submit an issue to Github.
Also, check these blog articles, they are explaining how the library works, in details:
- Java Method Logging with AOP and Annotations
- Cache Java Method Results
- Limit Java Method Execution Time
Cutting Edge Version
If you want to use a current version of the product, you can do it with this configuration in your pom.xml
:
<repositories> <repository> <id>oss.sonatype.org</id> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-aspects</artifactId> <version>0.26.0</version> </dependency> </dependencies>