General cleanup. Simplifying code. Replacing all prints with appropriate Logger.

This commit is contained in:
Ovidijus Okinskas 2018-06-23 13:24:07 +01:00 committed by O.Okinskas
parent 868e4561b5
commit 0453bf9063
6 changed files with 27 additions and 35 deletions

View File

@ -10,7 +10,7 @@ tags:
--- ---
## Intent ## Intent
Provide a helper service that sends network requests on behalf of a client and offload common additional connectivity tasks. Provide a helper service instance on a client and offload common functionality away from a shared resource.
## Explanation ## Explanation
Real world example Real world example
@ -42,7 +42,8 @@ A remote services represented as a singleton.
```java ```java
public class RemoteService implements RemoteServiceInterface { public class RemoteService implements RemoteServiceInterface {
private static RemoteService service = null; private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static RemoteService service = null;2
static synchronized RemoteService getRemoteService() { static synchronized RemoteService getRemoteService() {
if (service == null) { if (service == null) {
@ -51,9 +52,7 @@ public class RemoteService implements RemoteServiceInterface {
return service; return service;
} }
private RemoteService() { private RemoteService() {}
}
@Override @Override
public long doRemoteFunction(int value) { public long doRemoteFunction(int value) {
@ -63,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface {
try { try {
sleep(waitTime); sleep(waitTime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("Thread sleep interrupted", e)
} }
return waitTime >= 200 ? value * 10 : -1; return waitTime >= 200 ? value * 10 : -1;
} }
@ -79,9 +78,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
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) {
@ -116,7 +113,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
try { try {
sleep(DELAY_MS); sleep(DELAY_MS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("Thread sleep state interrupted", e);
} }
} else { } else {
break; break;
@ -140,7 +137,7 @@ public class Client {
long useService(int value) { long useService(int value) {
long result = serviceAmbassador.doRemoteFunction(value); long result = serviceAmbassador.doRemoteFunction(value);
System.out.println(result); LOGGER.info("Service result: " + result)
return result; return result;
} }
} }

View File

@ -22,20 +22,21 @@
*/ */
package com.iluwatar.ambassador; package com.iluwatar.ambassador;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
/** /**
* A simple Client * A simple Client
*/ */
public class Client { public class Client {
private ServiceAmbassador serviceAmbassador; private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
Client() {
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); LOGGER.info("Service result: " + result);
return result; return result;
} }
} }

View File

@ -22,6 +22,9 @@
*/ */
package com.iluwatar.ambassador; package com.iluwatar.ambassador;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
/** /**
@ -29,6 +32,7 @@ import static java.lang.Thread.sleep;
*/ */
public class RemoteService implements RemoteServiceInterface { public class RemoteService implements RemoteServiceInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static RemoteService service = null; private static RemoteService service = null;
static synchronized RemoteService getRemoteService() { static synchronized RemoteService getRemoteService() {
@ -38,13 +42,11 @@ public class RemoteService implements RemoteServiceInterface {
return service; return service;
} }
private RemoteService() { private RemoteService() {}
}
/** /**
* Remote function takes a value and multiplies it by 10 taking a random amount of time. * 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. * Will sometimes return -1. This imitates connectivity issues a client might have to account for.
* @param value integer value to be multiplied. * @param value integer value to be multiplied.
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1. * @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
*/ */
@ -56,7 +58,7 @@ public class RemoteService implements RemoteServiceInterface {
try { try {
sleep(waitTime); sleep(waitTime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("Thread sleep state interrupted", e);
} }
return waitTime >= 200 ? value * 10 : -1; return waitTime >= 200 ? value * 10 : -1;
} }

View File

@ -40,20 +40,16 @@ public class ServiceAmbassador implements RemoteServiceInterface {
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();
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
long result = service.doRemoteFunction(value); long result = RemoteService.getRemoteService().doRemoteFunction(value);
long timeTaken = System.currentTimeMillis() - startTime; long timeTaken = System.currentTimeMillis() - startTime;
LOGGER.info("Time taken (ms): " + timeTaken); LOGGER.info("Time taken (ms): " + timeTaken);
@ -77,7 +73,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
try { try {
sleep(DELAY_MS); sleep(DELAY_MS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("Thread sleep state interrupted", e);
} }
} else { } else {
break; break;

View File

@ -31,10 +31,7 @@ public class RemoteServiceTest {
@Test @Test
public void test() { public void test() {
long result = RemoteService.getRemoteService().doRemoteFunction(10);
RemoteService remoteService = RemoteService.getRemoteService();
long result = remoteService.doRemoteFunction(10);
assert result == 100 || result == -1; assert result == 100 || result == -1;
} }
} }

View File

@ -31,8 +31,7 @@ public class ServiceAmbassadorTest {
@Test @Test
public void test() { public void test() {
ServiceAmbassador ambassador = new ServiceAmbassador(); long result = new ServiceAmbassador().doRemoteFunction(10);
long result = ambassador.doRemoteFunction(10);
assert result == 100 || result == -1; assert result == 100 || result == -1;
} }
} }