Merge pull request #8683 from SmartyAnsh/BAEL-3758_AOP_Annotations_jcabi-aspects
BAEL-3758 - AOP Annotations of jcabi-aspects
This commit is contained in:
commit
409ebc1a90
@ -73,6 +73,18 @@
|
|||||||
<version>${cache2k.version}</version>
|
<version>${cache2k.version}</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
@ -81,6 +93,36 @@
|
|||||||
<url>https://jitpack.io</url>
|
<url>https://jitpack.io</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</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>
|
<properties>
|
||||||
<jcommander.version>1.78</jcommander.version>
|
<jcommander.version>1.78</jcommander.version>
|
||||||
@ -93,5 +135,11 @@
|
|||||||
<cactoos.version>0.43</cactoos.version>
|
<cactoos.version>0.43</cactoos.version>
|
||||||
<airline.version>2.7.2</airline.version>
|
<airline.version>2.7.2</airline.version>
|
||||||
<cache2k.version>1.2.3.Final</cache2k.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>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
112
libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java
Normal file
112
libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user