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 { public class App {
/** /**
* Entry point * Entry point
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Client host1 = new Client();
Client host1 = new Client(); Client host2 = new Client();
Client host2 = new Client(); host1.useService(12);
host1.useService(12); host2.useService(73);
host2.useService(73); }
}
} }

View File

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

View File

@ -29,35 +29,35 @@ import static java.lang.Thread.sleep;
*/ */
public class RemoteService implements RemoteServiceInterface { public class RemoteService implements RemoteServiceInterface {
private static RemoteService service = null; private static RemoteService service = null;
static synchronized RemoteService getRemoteService() { static synchronized RemoteService getRemoteService() {
if (service == null) { if (service == null) {
service = new RemoteService(); service = new RemoteService();
}
return service;
} }
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();
} }
return waitTime >= 200 ? value * 10 : -1;
/** }
* 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;
}
} }

View File

@ -26,6 +26,6 @@ package com.iluwatar.ambassador;
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}). * Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
*/ */
interface RemoteServiceInterface { 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 { public class ServiceAmbassador implements RemoteServiceInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
private static final int RETRIES = 3; private static final int RETRIES = 3;
private static final int DELAY_MS = 3000; private static final int DELAY_MS = 3000;
ServiceAmbassador() { ServiceAmbassador() {
} }
@Override @Override
public long doRemoteFunction(int value) { public long doRemoteFunction(int value) {
return safeCall(value); return safeCall(value);
} }
private long checkLatency(int value) { private long checkLatency(int value) {
RemoteService service = RemoteService.getRemoteService(); RemoteService service = RemoteService.getRemoteService();
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
long result = service.doRemoteFunction(value); long result = service.doRemoteFunction(value);
long timeTaken = System.currentTimeMillis() - startTime; long timeTaken = System.currentTimeMillis() - startTime;
LOGGER.info("Time taken (ms): " + timeTaken); LOGGER.info("Time taken (ms): " + timeTaken);
return result; return result;
} }
private long safeCall(int value) { private long safeCall(int value) {
int retries = 0; int retries = 0;
long result = -1; long result = -1;
for (int i = 0; i < RETRIES; i++) { for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) { if (retries >= RETRIES) {
return -1; return -1;
} }
if ((result = checkLatency(value)) == -1) { if ((result = checkLatency(value)) == -1) {
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")"); LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++; retries++;
try { try {
sleep(DELAY_MS); sleep(DELAY_MS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
}
} else {
break;
}
} }
return result; } else {
break;
}
} }
return result;
}
} }