General cleanup. Simplifying code. Replacing all prints with appropriate Logger.
This commit is contained in:
parent
868e4561b5
commit
0453bf9063
@ -10,7 +10,7 @@ tags:
|
||||
---
|
||||
|
||||
## 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
|
||||
Real world example
|
||||
@ -42,7 +42,8 @@ A remote services represented as a singleton.
|
||||
```java
|
||||
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() {
|
||||
if (service == null) {
|
||||
@ -51,9 +52,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
return service;
|
||||
}
|
||||
|
||||
private RemoteService() {
|
||||
|
||||
}
|
||||
private RemoteService() {}
|
||||
|
||||
@Override
|
||||
public long doRemoteFunction(int value) {
|
||||
@ -63,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
try {
|
||||
sleep(waitTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Thread sleep interrupted", e)
|
||||
}
|
||||
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 DELAY_MS = 3000;
|
||||
|
||||
ServiceAmbassador() {
|
||||
|
||||
}
|
||||
ServiceAmbassador() {}
|
||||
|
||||
@Override
|
||||
public long doRemoteFunction(int value) {
|
||||
@ -116,7 +113,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
try {
|
||||
sleep(DELAY_MS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
@ -140,7 +137,7 @@ public class Client {
|
||||
|
||||
long useService(int value) {
|
||||
long result = serviceAmbassador.doRemoteFunction(value);
|
||||
System.out.println(result);
|
||||
LOGGER.info("Service result: " + result)
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -22,20 +22,21 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* A simple Client
|
||||
*/
|
||||
public class Client {
|
||||
|
||||
private ServiceAmbassador serviceAmbassador;
|
||||
|
||||
Client() {
|
||||
serviceAmbassador = new ServiceAmbassador();
|
||||
}
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
|
||||
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
|
||||
|
||||
long useService(int value) {
|
||||
long result = serviceAmbassador.doRemoteFunction(value);
|
||||
System.out.println(result);
|
||||
LOGGER.info("Service result: " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,9 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
/**
|
||||
@ -29,6 +32,7 @@ import static java.lang.Thread.sleep;
|
||||
*/
|
||||
public class RemoteService implements RemoteServiceInterface {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
||||
private static RemoteService service = null;
|
||||
|
||||
static synchronized RemoteService getRemoteService() {
|
||||
@ -38,13 +42,11 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
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.
|
||||
* Will sometimes return -1. This imitates 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.
|
||||
*/
|
||||
@ -56,7 +58,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
try {
|
||||
sleep(waitTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
}
|
||||
return waitTime >= 200 ? value * 10 : -1;
|
||||
}
|
||||
|
@ -40,20 +40,16 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
private static final int RETRIES = 3;
|
||||
private static final int DELAY_MS = 3000;
|
||||
|
||||
ServiceAmbassador() {
|
||||
|
||||
}
|
||||
ServiceAmbassador() {}
|
||||
|
||||
@Override
|
||||
public long doRemoteFunction(int value) {
|
||||
|
||||
return safeCall(value);
|
||||
}
|
||||
|
||||
private long checkLatency(int value) {
|
||||
RemoteService service = RemoteService.getRemoteService();
|
||||
long startTime = System.currentTimeMillis();
|
||||
long result = service.doRemoteFunction(value);
|
||||
long result = RemoteService.getRemoteService().doRemoteFunction(value);
|
||||
long timeTaken = System.currentTimeMillis() - startTime;
|
||||
|
||||
LOGGER.info("Time taken (ms): " + timeTaken);
|
||||
@ -77,7 +73,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
try {
|
||||
sleep(DELAY_MS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
|
@ -31,10 +31,7 @@ public class RemoteServiceTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
RemoteService remoteService = RemoteService.getRemoteService();
|
||||
long result = remoteService.doRemoteFunction(10);
|
||||
|
||||
long result = RemoteService.getRemoteService().doRemoteFunction(10);
|
||||
assert result == 100 || result == -1;
|
||||
}
|
||||
}
|
||||
|
@ -31,8 +31,7 @@ public class ServiceAmbassadorTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ServiceAmbassador ambassador = new ServiceAmbassador();
|
||||
long result = ambassador.doRemoteFunction(10);
|
||||
long result = new ServiceAmbassador().doRemoteFunction(10);
|
||||
assert result == 100 || result == -1;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user