Merge pull request #8683 from SmartyAnsh/BAEL-3758_AOP_Annotations_jcabi-aspects

BAEL-3758 - AOP Annotations of jcabi-aspects
This commit is contained in:
Eric Martin 2020-02-20 20:00:58 -06:00 committed by GitHub
commit 409ebc1a90
2 changed files with 160 additions and 0 deletions

View File

@ -73,6 +73,18 @@
<version>${cache2k.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
<version>${jcabi-aspects.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectjrt.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<repositories>
@ -82,6 +94,36 @@
</repository>
</repositories>
<build>
<finalName>libraries-3</finalName>
<plugins>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>${jcabi-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectjtools.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<jcommander.version>1.78</jcommander.version>
<lombok.version>1.18.6</lombok.version>
@ -93,5 +135,11 @@
<cactoos.version>0.43</cactoos.version>
<airline.version>2.7.2</airline.version>
<cache2k.version>1.2.3.Final</cache2k.version>
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
<aspectjrt.version>1.9.2</aspectjrt.version>
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
<aspectjtools.version>1.9.2</aspectjtools.version>
<aspectjweaver.version>1.9.2</aspectjweaver.version>
</properties>
</project>

View File

@ -0,0 +1,112 @@
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;
import com.jcabi.aspects.Async;
import com.jcabi.aspects.Cacheable;
import com.jcabi.aspects.LogExceptions;
import com.jcabi.aspects.Loggable;
import com.jcabi.aspects.Quietly;
import com.jcabi.aspects.RetryOnFailure;
import com.jcabi.aspects.UnitedThrow;
public class JcabiAspectJ {
public static void main(String[] args) {
try {
displayFactorial(10);
getFactorial(10).get();
String result = cacheExchangeRates();
if (result != cacheExchangeRates()) {
System.out.println(result);
}
divideByZero();
} catch(Exception e) {
e.printStackTrace();
}
divideByZeroQuietly();
try {
processFile();
} catch(Exception e) {
e.printStackTrace();
}
}
@Loggable
@Async
public static void displayFactorial(int number) {
long result = factorial(number);
System.out.println(result);
}
@Loggable
@Async
public static Future<Long> getFactorial(int number) {
Future<Long> 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;
}
@Loggable
@Cacheable(lifetime = 2, unit = TimeUnit.SECONDS)
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;
}
@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;
}
@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);
}
}