Fixing CheckStyle issues.

This commit is contained in:
Ovidijus Okinskas 2018-06-04 22:50:59 +01:00 committed by O.Okinskas
parent 5393c96247
commit ff579fabcf
5 changed files with 82 additions and 83 deletions

View File

@ -41,14 +41,13 @@ package com.iluwatar.ambassador;
*/
public class App {
/**
* Entry point
*/
public static void main(String[] args) {
Client host1 = new Client();
Client host2 = new Client();
host1.useService(12);
host2.useService(73);
}
/**
* Entry point
*/
public static void main(String[] args) {
Client host1 = new Client();
Client host2 = new Client();
host1.useService(12);
host2.useService(73);
}
}

View File

@ -27,15 +27,15 @@ package com.iluwatar.ambassador;
*/
public class Client {
private ServiceAmbassador serviceAmbassador;
private ServiceAmbassador serviceAmbassador;
Client() {
serviceAmbassador = new ServiceAmbassador();
}
Client() {
serviceAmbassador = new ServiceAmbassador();
}
long useService(int value) {
long result = serviceAmbassador.doRemoteFunction(value);
System.out.println(result);
return result;
}
long useService(int value) {
long result = serviceAmbassador.doRemoteFunction(value);
System.out.println(result);
return result;
}
}

View File

@ -29,35 +29,35 @@ import static java.lang.Thread.sleep;
*/
public class RemoteService implements RemoteServiceInterface {
private static RemoteService service = null;
private static RemoteService service = null;
static synchronized RemoteService getRemoteService() {
if (service == null) {
service = new RemoteService();
}
return service;
static synchronized RemoteService getRemoteService() {
if (service == null) {
service = new RemoteService();
}
return service;
}
private RemoteService() {
private RemoteService() {
}
/**
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
* Will sometimes return -1. This immitates connectivity issues a client might have to account for.
* @param value integer value to be multiplied.
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
*/
@Override
public long doRemoteFunction(int value) {
long waitTime = (long) Math.floor(Math.random() * 1000);
try {
sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
* Will sometimes return -1. This immitates connectivity issues a client might have to account for.
* @param value integer value to be multiplied.
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
*/
@Override
public long doRemoteFunction(int value) {
long waitTime = (long) Math.floor(Math.random() * 1000);
try {
sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
return waitTime >= 200 ? value * 10 : -1;
}
return waitTime >= 200 ? value * 10 : -1;
}
}

View File

@ -26,6 +26,6 @@ package com.iluwatar.ambassador;
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
*/
interface RemoteServiceInterface {
long doRemoteFunction(int value) throws Exception;
long doRemoteFunction(int value) throws Exception;
}

View File

@ -36,53 +36,53 @@ import static java.lang.Thread.sleep;
*/
public class ServiceAmbassador implements RemoteServiceInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
private static final int RETRIES = 3;
private static final int DELAY_MS = 3000;
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
private static final int RETRIES = 3;
private static final int DELAY_MS = 3000;
ServiceAmbassador() {
ServiceAmbassador() {
}
}
@Override
public long doRemoteFunction(int value) {
@Override
public long doRemoteFunction(int value) {
return safeCall(value);
}
return safeCall(value);
}
private long checkLatency(int value) {
RemoteService service = RemoteService.getRemoteService();
long startTime = System.currentTimeMillis();
long result = service.doRemoteFunction(value);
long timeTaken = System.currentTimeMillis() - startTime;
private long checkLatency(int value) {
RemoteService service = RemoteService.getRemoteService();
long startTime = System.currentTimeMillis();
long result = service.doRemoteFunction(value);
long timeTaken = System.currentTimeMillis() - startTime;
LOGGER.info("Time taken (ms): " + timeTaken);
return result;
}
LOGGER.info("Time taken (ms): " + timeTaken);
return result;
}
private long safeCall(int value) {
private long safeCall(int value) {
int retries = 0;
long result = -1;
int retries = 0;
long result = -1;
for (int i = 0; i < RETRIES; i++) {
for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) {
return -1;
}
if (retries >= RETRIES) {
return -1;
}
if ((result = checkLatency(value)) == -1) {
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++;
try {
sleep(DELAY_MS);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
if ((result = checkLatency(value)) == -1) {
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++;
try {
sleep(DELAY_MS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
} else {
break;
}
}
return result;
}
}