From 877f479ed75d7a820786f53291353a0a3e530692 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Tue, 28 Jan 2020 11:34:42 +0200 Subject: [PATCH 1/7] Initial commit --- jcabi/pom.xml | 81 ++++++++++++++ .../com/baeldung/jcabi_aspectj/Example.java | 103 ++++++++++++++++++ .../java/com/baeldung/jcabi_aspectj/User.java | 5 + 3 files changed, 189 insertions(+) create mode 100644 jcabi/pom.xml create mode 100644 jcabi/src/main/java/com/baeldung/jcabi_aspectj/Example.java create mode 100644 jcabi/src/main/java/com/baeldung/jcabi_aspectj/User.java diff --git a/jcabi/pom.xml b/jcabi/pom.xml new file mode 100644 index 0000000000..7fb9c313c5 --- /dev/null +++ b/jcabi/pom.xml @@ -0,0 +1,81 @@ + + + + 4.0.0 + jcabi + 0.1.0-SNAPSHOT + jcabi + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + com.jcabi + jcabi-aspects + ${jcabi-aspects.version} + + + + org.aspectj + aspectjrt + ${aspectjrt.version} + runtime + + + + + + jcabi + + + com.jcabi + jcabi-maven-plugin + ${jcabi-maven-plugin.version} + + + + ajc + + + + + + org.aspectj + aspectjtools + ${aspectjtools.version} + + + org.aspectj + aspectjweaver + ${aspectjweaver.version} + + + + + + + + 3.14.0 + 0.22.6 + 1.9.5 + 0.14.1 + 1.9.1 + 1.9.1 + + + diff --git a/jcabi/src/main/java/com/baeldung/jcabi_aspectj/Example.java b/jcabi/src/main/java/com/baeldung/jcabi_aspectj/Example.java new file mode 100644 index 0000000000..0c688ceb22 --- /dev/null +++ b/jcabi/src/main/java/com/baeldung/jcabi_aspectj/Example.java @@ -0,0 +1,103 @@ +package com.baeldung.jcabi_aspectj; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import com.jcabi.aspects.Async; +import com.jcabi.aspects.Cacheable; +import com.jcabi.aspects.LogExceptions; +import com.jcabi.aspects.Loggable; +import com.jcabi.aspects.Parallel; +import com.jcabi.aspects.Quietly; +import com.jcabi.aspects.RetryOnFailure; +import com.jcabi.aspects.UnitedThrow; + +//@Loggable +public class Example { + + public static void main(String[] args) { + try { + tryAsync(); + asyncFactorial(10).get(); + System.out.println("calling 2..."); + System.out.println(called2()); + //called3(); + called4(); + System.out.println(called2()); + System.out.println(called2()); + called6(); + //called7(); + + + } catch(Exception e) { + + } + called5(); + called8(); + } + + @Async + public static void tryAsync() { + long result = factorial(10); + System.out.println(result); + } + + @Async + public static Future asyncFactorial(int number) { + Future factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number)); + return factorialFuture; + } + + /** + * Finds factorial of a number + * @param number + * @return + */ + public static long factorial(int number) { + long result = 1; + for(int i=number;i>0;i--) { + result *= i; + } + return result; + } + + @Cacheable(lifetime = 5, unit = TimeUnit.SECONDS) + public static Double called2() { + return Math.random(); + } + + @LogExceptions + public static void called3() { + System.out.println(1/0); + } + + @Loggable + public static void called4() { + System.out.println("checking loggable"); + } + + @Quietly + public static void called5() { + int x = 1/0; + } + + @Parallel(threads = 4) + public static void called6() { + System.out.println("called6"); + } + + @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; + } + +} diff --git a/jcabi/src/main/java/com/baeldung/jcabi_aspectj/User.java b/jcabi/src/main/java/com/baeldung/jcabi_aspectj/User.java new file mode 100644 index 0000000000..c918f8471c --- /dev/null +++ b/jcabi/src/main/java/com/baeldung/jcabi_aspectj/User.java @@ -0,0 +1,5 @@ +package com.baeldung.jcabi_aspectj; + +public class User { + +} From 992fe24fe06907f264afaf63697e576572f68bd3 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 8 Feb 2020 18:39:39 +0200 Subject: [PATCH 2/7] BAEL-3758 - AOP annotations of jcabi-aspects --- .../{jcabi_aspectj/Example.java => jcabi/JcabiAspectJ.java} | 0 .../src/main/java/com/baeldung/{jcabi_aspectj => jcabi}/User.java | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename jcabi/src/main/java/com/baeldung/{jcabi_aspectj/Example.java => jcabi/JcabiAspectJ.java} (100%) rename jcabi/src/main/java/com/baeldung/{jcabi_aspectj => jcabi}/User.java (100%) diff --git a/jcabi/src/main/java/com/baeldung/jcabi_aspectj/Example.java b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java similarity index 100% rename from jcabi/src/main/java/com/baeldung/jcabi_aspectj/Example.java rename to jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java diff --git a/jcabi/src/main/java/com/baeldung/jcabi_aspectj/User.java b/jcabi/src/main/java/com/baeldung/jcabi/User.java similarity index 100% rename from jcabi/src/main/java/com/baeldung/jcabi_aspectj/User.java rename to jcabi/src/main/java/com/baeldung/jcabi/User.java From dbdbc24b61fcd7ccfc2ca0573cccccbd36acfa1c Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 8 Feb 2020 18:40:16 +0200 Subject: [PATCH 3/7] BAEL-3758 - AOP annotations of jcabi-aspects --- jcabi/pom.xml | 7 -- .../java/com/baeldung/jcabi/JcabiAspectJ.java | 86 ++++++++----------- .../main/java/com/baeldung/jcabi/User.java | 5 -- 3 files changed, 37 insertions(+), 61 deletions(-) delete mode 100644 jcabi/src/main/java/com/baeldung/jcabi/User.java diff --git a/jcabi/pom.xml b/jcabi/pom.xml index 7fb9c313c5..bf53f74bcd 100644 --- a/jcabi/pom.xml +++ b/jcabi/pom.xml @@ -17,12 +17,6 @@ - - org.assertj - assertj-core - ${assertj.version} - test - com.jcabi @@ -70,7 +64,6 @@ - 3.14.0 0.22.6 1.9.5 0.14.1 diff --git a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java index 0c688ceb22..1bfecd6c52 100644 --- a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java +++ b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java @@ -1,4 +1,5 @@ -package com.baeldung.jcabi_aspectj; +package com.baeldung.jcabi; + import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; @@ -11,40 +12,42 @@ import com.jcabi.aspects.Loggable; import com.jcabi.aspects.Parallel; import com.jcabi.aspects.Quietly; import com.jcabi.aspects.RetryOnFailure; +import com.jcabi.aspects.Timeable; import com.jcabi.aspects.UnitedThrow; -//@Loggable -public class Example { +public class JcabiAspectJ { public static void main(String[] args) { try { - tryAsync(); - asyncFactorial(10).get(); - System.out.println("calling 2..."); - System.out.println(called2()); - //called3(); - called4(); - System.out.println(called2()); - System.out.println(called2()); - called6(); - //called7(); + displayFactorial(10); + getFactorial(10).get(); + Double number = cacheRandomNumber(); + if (number != cacheRandomNumber()) { + System.out.println(number); + } + + divideByZero(); } catch(Exception e) { - + e.printStackTrace(); } - called5(); - called8(); + + divideByZeroQuietly(); + parallelExecution(); + } + @Loggable @Async - public static void tryAsync() { - long result = factorial(10); + public static void displayFactorial(int number) { + long result = factorial(number); System.out.println(result); } + @Loggable @Async - public static Future asyncFactorial(int number) { + public static Future getFactorial(int number) { Future factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number)); return factorialFuture; } @@ -62,42 +65,27 @@ public class Example { return result; } - @Cacheable(lifetime = 5, unit = TimeUnit.SECONDS) - public static Double called2() { + @Loggable + @Cacheable(lifetime = 2, unit = TimeUnit.SECONDS) + public static Double cacheRandomNumber() { return Math.random(); } - + + @UnitedThrow(IllegalStateException.class) @LogExceptions - public static void called3() { - System.out.println(1/0); - } - - @Loggable - public static void called4() { - System.out.println("checking loggable"); - } - - @Quietly - public static void called5() { + public static void divideByZero() { int x = 1/0; } - + + @RetryOnFailure(attempts = 2, types = {java.lang.NumberFormatException.class}) + @Quietly + public static void divideByZeroQuietly() { + int x = 1/0; + } + @Parallel(threads = 4) - public static void called6() { - System.out.println("called6"); - } - - @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; + public static void parallelExecution() { + System.out.println("Calling Parallel..."); } } diff --git a/jcabi/src/main/java/com/baeldung/jcabi/User.java b/jcabi/src/main/java/com/baeldung/jcabi/User.java deleted file mode 100644 index c918f8471c..0000000000 --- a/jcabi/src/main/java/com/baeldung/jcabi/User.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.jcabi_aspectj; - -public class User { - -} From 598edf2847495173ea269490e140c36ccf7422de Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 8 Feb 2020 19:10:10 +0200 Subject: [PATCH 4/7] BAEL-3758 - AOP Annotations of jcabi-aspects --- jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java | 5 +---- pom.xml | 2 ++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java index 1bfecd6c52..c88ad5a74d 100644 --- a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java +++ b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java @@ -12,7 +12,6 @@ import com.jcabi.aspects.Loggable; import com.jcabi.aspects.Parallel; import com.jcabi.aspects.Quietly; import com.jcabi.aspects.RetryOnFailure; -import com.jcabi.aspects.Timeable; import com.jcabi.aspects.UnitedThrow; public class JcabiAspectJ { @@ -28,14 +27,12 @@ public class JcabiAspectJ { } divideByZero(); - } catch(Exception e) { e.printStackTrace(); } divideByZeroQuietly(); - parallelExecution(); - + parallelExecution(); } @Loggable diff --git a/pom.xml b/pom.xml index aeaef5f676..177792f3b8 100644 --- a/pom.xml +++ b/pom.xml @@ -470,6 +470,7 @@ javax-servlets javaxval jaxb + jcabi jee-7 jee-7-security jee-kotlin @@ -1019,6 +1020,7 @@ javax-servlets javaxval jaxb + jcabi jee-7 jee-7-security jee-kotlin From 646498787d6b190e3f117831ecd442199dbe28d5 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Tue, 11 Feb 2020 18:02:53 +0200 Subject: [PATCH 5/7] Review changes --- .../java/com/baeldung/jcabi/JcabiAspectJ.java | 62 +++++++++++++------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java index c88ad5a74d..6bd345c59c 100644 --- a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java +++ b/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java @@ -1,6 +1,14 @@ package com.baeldung.jcabi; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -9,7 +17,6 @@ import com.jcabi.aspects.Async; import com.jcabi.aspects.Cacheable; import com.jcabi.aspects.LogExceptions; import com.jcabi.aspects.Loggable; -import com.jcabi.aspects.Parallel; import com.jcabi.aspects.Quietly; import com.jcabi.aspects.RetryOnFailure; import com.jcabi.aspects.UnitedThrow; @@ -20,19 +27,24 @@ public class JcabiAspectJ { try { displayFactorial(10); getFactorial(10).get(); - - Double number = cacheRandomNumber(); - if (number != cacheRandomNumber()) { - System.out.println(number); + + String result = cacheExchangeRates(); + if (result != cacheExchangeRates()) { + System.out.println(result); } - + divideByZero(); } catch(Exception e) { e.printStackTrace(); } - + divideByZeroQuietly(); - parallelExecution(); + try { + processFile(); + } catch(Exception e) { + e.printStackTrace(); + } + } @Loggable @@ -41,14 +53,14 @@ public class JcabiAspectJ { long result = factorial(number); System.out.println(result); } - + @Loggable @Async public static Future getFactorial(int number) { Future factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number)); return factorialFuture; } - + /** * Finds factorial of a number * @param number @@ -64,25 +76,37 @@ public class JcabiAspectJ { @Loggable @Cacheable(lifetime = 2, unit = TimeUnit.SECONDS) - public static Double cacheRandomNumber() { - return Math.random(); + public static String cacheExchangeRates() { + String result = null; + try { + URL exchangeRateUrl = new URL("https://api.exchangeratesapi.io/latest"); + URLConnection con = exchangeRateUrl.openConnection(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + result = in.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; } - - @UnitedThrow(IllegalStateException.class) + @LogExceptions public static void divideByZero() { int x = 1/0; } - + @RetryOnFailure(attempts = 2, types = {java.lang.NumberFormatException.class}) @Quietly public static void divideByZeroQuietly() { int x = 1/0; } - - @Parallel(threads = 4) - public static void parallelExecution() { - System.out.println("Calling Parallel..."); + + @UnitedThrow(IllegalStateException.class) + public static void processFile() throws IOException, InterruptedException { + BufferedReader reader = new BufferedReader(new FileReader("baeldung.txt")); + reader.readLine(); + + Thread thread = new Thread(); + thread.wait(2000); } } From 3a5fef318b87735c14600a5976bddec61fe7906e Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Tue, 18 Feb 2020 11:13:39 +0200 Subject: [PATCH 6/7] aspectJ dependencies set to 1.9.2 --- jcabi/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jcabi/pom.xml b/jcabi/pom.xml index bf53f74bcd..eb5d2dc4cf 100644 --- a/jcabi/pom.xml +++ b/jcabi/pom.xml @@ -65,10 +65,10 @@ 0.22.6 - 1.9.5 + 1.9.2 0.14.1 - 1.9.1 - 1.9.1 + 1.9.2 + 1.9.2 From 73c60e88acb016a04b5cc039e024433e3a61b509 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Tue, 18 Feb 2020 15:32:00 +0200 Subject: [PATCH 7/7] Code moved to libraries-3 module --- jcabi/pom.xml | 74 ------------------- libraries-3/pom.xml | 48 ++++++++++++ .../java/com/baeldung/jcabi/JcabiAspectJ.java | 0 pom.xml | 2 - 4 files changed, 48 insertions(+), 76 deletions(-) delete mode 100644 jcabi/pom.xml rename {jcabi => libraries-3}/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java (100%) diff --git a/jcabi/pom.xml b/jcabi/pom.xml deleted file mode 100644 index eb5d2dc4cf..0000000000 --- a/jcabi/pom.xml +++ /dev/null @@ -1,74 +0,0 @@ - - - - 4.0.0 - jcabi - 0.1.0-SNAPSHOT - jcabi - jar - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../parent-java - - - - - - com.jcabi - jcabi-aspects - ${jcabi-aspects.version} - - - - org.aspectj - aspectjrt - ${aspectjrt.version} - runtime - - - - - - jcabi - - - com.jcabi - jcabi-maven-plugin - ${jcabi-maven-plugin.version} - - - - ajc - - - - - - org.aspectj - aspectjtools - ${aspectjtools.version} - - - org.aspectj - aspectjweaver - ${aspectjweaver.version} - - - - - - - - 0.22.6 - 1.9.2 - 0.14.1 - 1.9.2 - 1.9.2 - - - diff --git a/libraries-3/pom.xml b/libraries-3/pom.xml index 1dbe6b2435..b1795ab7e0 100644 --- a/libraries-3/pom.xml +++ b/libraries-3/pom.xml @@ -74,6 +74,18 @@ ${cache2k.version} pom + + + com.jcabi + jcabi-aspects + ${jcabi-aspects.version} + + + org.aspectj + aspectjrt + ${aspectjrt.version} + runtime + @@ -82,6 +94,36 @@ https://jitpack.io + + + libraries-3 + + + com.jcabi + jcabi-maven-plugin + ${jcabi-maven-plugin.version} + + + + ajc + + + + + + org.aspectj + aspectjtools + ${aspectjtools.version} + + + org.aspectj + aspectjweaver + ${aspectjweaver.version} + + + + + 1.78 @@ -94,5 +136,11 @@ 0.43 2.7.2 1.2.3.Final + + 0.22.6 + 1.9.2 + 0.14.1 + 1.9.2 + 1.9.2 diff --git a/jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java b/libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java similarity index 100% rename from jcabi/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java rename to libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java diff --git a/pom.xml b/pom.xml index 19f435f1b8..6e6316edd6 100644 --- a/pom.xml +++ b/pom.xml @@ -472,7 +472,6 @@ javax-servlets javaxval jaxb - jcabi jee-7 jee-7-security jee-kotlin @@ -983,7 +982,6 @@ javax-servlets javaxval jaxb - jcabi jee-7 jee-7-security jee-kotlin