BAEL-3758 - AOP annotations of jcabi-aspects

This commit is contained in:
Anshul BANSAL 2020-02-08 18:40:16 +02:00
parent d40a5c1194
commit 9ab42e6118
3 changed files with 37 additions and 61 deletions

View File

@ -17,12 +17,6 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.jcabi</groupId> <groupId>com.jcabi</groupId>
@ -70,7 +64,6 @@
</build> </build>
<properties> <properties>
<assertj.version>3.14.0</assertj.version>
<jcabi-aspects.version>0.22.6</jcabi-aspects.version> <jcabi-aspects.version>0.22.6</jcabi-aspects.version>
<aspectjrt.version>1.9.5</aspectjrt.version> <aspectjrt.version>1.9.5</aspectjrt.version>
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version> <jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>

View File

@ -1,4 +1,5 @@
package com.baeldung.jcabi_aspectj; package com.baeldung.jcabi;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -11,40 +12,42 @@ import com.jcabi.aspects.Loggable;
import com.jcabi.aspects.Parallel; import com.jcabi.aspects.Parallel;
import com.jcabi.aspects.Quietly; import com.jcabi.aspects.Quietly;
import com.jcabi.aspects.RetryOnFailure; import com.jcabi.aspects.RetryOnFailure;
import com.jcabi.aspects.Timeable;
import com.jcabi.aspects.UnitedThrow; import com.jcabi.aspects.UnitedThrow;
//@Loggable public class JcabiAspectJ {
public class Example {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
tryAsync(); displayFactorial(10);
asyncFactorial(10).get(); getFactorial(10).get();
System.out.println("calling 2...");
System.out.println(called2());
//called3();
called4();
System.out.println(called2());
System.out.println(called2());
called6();
//called7();
Double number = cacheRandomNumber();
if (number != cacheRandomNumber()) {
System.out.println(number);
}
divideByZero();
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace();
}
called5();
called8();
} }
divideByZeroQuietly();
parallelExecution();
}
@Loggable
@Async @Async
public static void tryAsync() { public static void displayFactorial(int number) {
long result = factorial(10); long result = factorial(number);
System.out.println(result); System.out.println(result);
} }
@Loggable
@Async @Async
public static Future<Long> asyncFactorial(int number) { public static Future<Long> getFactorial(int number) {
Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number)); Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number));
return factorialFuture; return factorialFuture;
} }
@ -62,42 +65,27 @@ public class Example {
return result; return result;
} }
@Cacheable(lifetime = 5, unit = TimeUnit.SECONDS) @Loggable
public static Double called2() { @Cacheable(lifetime = 2, unit = TimeUnit.SECONDS)
public static Double cacheRandomNumber() {
return Math.random(); return Math.random();
} }
@UnitedThrow(IllegalStateException.class)
@LogExceptions @LogExceptions
public static void called3() { public static void divideByZero() {
System.out.println(1/0); int x = 1/0;
}
@Loggable
public static void called4() {
System.out.println("checking loggable");
} }
@RetryOnFailure(attempts = 2, types = {java.lang.NumberFormatException.class})
@Quietly @Quietly
public static void called5() { public static void divideByZeroQuietly() {
int x = 1/0; int x = 1/0;
} }
@Parallel(threads = 4) @Parallel(threads = 4)
public static void called6() { public static void parallelExecution() {
System.out.println("called6"); System.out.println("Calling Parallel...");
}
@RetryOnFailure
//@Quietly
public static void called7() {
System.out.println("called7...");
int y = 1/0;
}
@UnitedThrow //(IllegalStateException.class)
public static void called8() {
System.out.println("called8...");
int y = 1/0;
} }
} }

View File

@ -1,5 +0,0 @@
package com.baeldung.jcabi_aspectj;
public class User {
}