Review changes

This commit is contained in:
Anshul BANSAL 2020-02-11 18:02:53 +02:00
parent 8f01547516
commit b907d2cd6e

View File

@ -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;
@ -21,9 +28,9 @@ public class JcabiAspectJ {
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();
@ -32,7 +39,12 @@ public class JcabiAspectJ {
}
divideByZeroQuietly();
parallelExecution();
try {
processFile();
} catch(Exception e) {
e.printStackTrace();
}
}
@Loggable
@ -64,11 +76,19 @@ 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;
@ -80,9 +100,13 @@ public class JcabiAspectJ {
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);
}
}