Skip to content

Commit

Permalink
General cleanup. Simplifying code. Replacing all prints with appropri…
Browse files Browse the repository at this point in the history
…ate Logger.
  • Loading branch information
okinskas committed Jun 23, 2018
1 parent 868e456 commit 0453bf9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
19 changes: 8 additions & 11 deletions ambassador/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -51,9 +52,7 @@ public class RemoteService implements RemoteServiceInterface {
return service;
}

private RemoteService() {

}
private RemoteService() {}

@Override
public long doRemoteFunction(int value) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down
13 changes: 7 additions & 6 deletions ambassador/src/main/java/com/iluwatar/ambassador/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
*/
package com.iluwatar.ambassador;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.Thread.sleep;

/**
* A remote legacy application represented by a Singleton implementation.
*/
public class RemoteService implements RemoteServiceInterface {

private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static RemoteService service = null;

static synchronized RemoteService getRemoteService() {
Expand All @@ -38,13 +42,11 @@ static synchronized RemoteService getRemoteService() {
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.
*/
Expand All @@ -56,7 +58,7 @@ public long doRemoteFunction(int value) {
try {
sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
LOGGER.error("Thread sleep state interrupted", e);
}
return waitTime >= 200 ? value * 10 : -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -77,7 +73,7 @@ private long safeCall(int value) {
try {
sleep(DELAY_MS);
} catch (InterruptedException e) {
e.printStackTrace();
LOGGER.error("Thread sleep state interrupted", e);
}
} else {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 0453bf9

Please sign in to comment.