Skip to content

Commit 0453bf9

Browse files
committed
General cleanup. Simplifying code. Replacing all prints with appropriate Logger.
1 parent 868e456 commit 0453bf9

File tree

6 files changed

+27
-35
lines changed

6 files changed

+27
-35
lines changed

ambassador/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tags:
1010
---
1111

1212
## Intent
13-
Provide a helper service that sends network requests on behalf of a client and offload common additional connectivity tasks.
13+
Provide a helper service instance on a client and offload common functionality away from a shared resource.
1414

1515
## Explanation
1616
Real world example
@@ -42,7 +42,8 @@ A remote services represented as a singleton.
4242
```java
4343
public class RemoteService implements RemoteServiceInterface {
4444

45-
private static RemoteService service = null;
45+
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
46+
private static RemoteService service = null;2
4647

4748
static synchronized RemoteService getRemoteService() {
4849
if (service == null) {
@@ -51,9 +52,7 @@ public class RemoteService implements RemoteServiceInterface {
5152
return service;
5253
}
5354

54-
private RemoteService() {
55-
56-
}
55+
private RemoteService() {}
5756

5857
@Override
5958
public long doRemoteFunction(int value) {
@@ -63,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface {
6362
try {
6463
sleep(waitTime);
6564
} catch (InterruptedException e) {
66-
e.printStackTrace();
65+
LOGGER.error("Thread sleep interrupted", e)
6766
}
6867
return waitTime >= 200 ? value * 10 : -1;
6968
}
@@ -79,9 +78,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
7978
private static final int RETRIES = 3;
8079
private static final int DELAY_MS = 3000;
8180

82-
ServiceAmbassador() {
83-
84-
}
81+
ServiceAmbassador() {}
8582

8683
@Override
8784
public long doRemoteFunction(int value) {
@@ -116,7 +113,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
116113
try {
117114
sleep(DELAY_MS);
118115
} catch (InterruptedException e) {
119-
e.printStackTrace();
116+
LOGGER.error("Thread sleep state interrupted", e);
120117
}
121118
} else {
122119
break;
@@ -140,7 +137,7 @@ public class Client {
140137

141138
long useService(int value) {
142139
long result = serviceAmbassador.doRemoteFunction(value);
143-
System.out.println(result);
140+
LOGGER.info("Service result: " + result)
144141
return result;
145142
}
146143
}

ambassador/src/main/java/com/iluwatar/ambassador/Client.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222
*/
2323
package com.iluwatar.ambassador;
2424

25+
import org.slf4j.LoggerFactory;
26+
27+
import org.slf4j.Logger;
28+
2529
/**
2630
* A simple Client
2731
*/
2832
public class Client {
2933

30-
private ServiceAmbassador serviceAmbassador;
31-
32-
Client() {
33-
serviceAmbassador = new ServiceAmbassador();
34-
}
34+
private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
35+
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
3536

3637
long useService(int value) {
3738
long result = serviceAmbassador.doRemoteFunction(value);
38-
System.out.println(result);
39+
LOGGER.info("Service result: " + result);
3940
return result;
4041
}
4142
}

ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222
*/
2323
package com.iluwatar.ambassador;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
import static java.lang.Thread.sleep;
2629

2730
/**
2831
* A remote legacy application represented by a Singleton implementation.
2932
*/
3033
public class RemoteService implements RemoteServiceInterface {
3134

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

3438
static synchronized RemoteService getRemoteService() {
@@ -38,13 +42,11 @@ static synchronized RemoteService getRemoteService() {
3842
return service;
3943
}
4044

41-
private RemoteService() {
42-
43-
}
45+
private RemoteService() {}
4446

4547
/**
4648
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
47-
* Will sometimes return -1. This immitates connectivity issues a client might have to account for.
49+
* Will sometimes return -1. This imitates connectivity issues a client might have to account for.
4850
* @param value integer value to be multiplied.
4951
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
5052
*/
@@ -56,7 +58,7 @@ public long doRemoteFunction(int value) {
5658
try {
5759
sleep(waitTime);
5860
} catch (InterruptedException e) {
59-
e.printStackTrace();
61+
LOGGER.error("Thread sleep state interrupted", e);
6062
}
6163
return waitTime >= 200 ? value * 10 : -1;
6264
}

ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,16 @@ public class ServiceAmbassador implements RemoteServiceInterface {
4040
private static final int RETRIES = 3;
4141
private static final int DELAY_MS = 3000;
4242

43-
ServiceAmbassador() {
44-
45-
}
43+
ServiceAmbassador() {}
4644

4745
@Override
4846
public long doRemoteFunction(int value) {
49-
5047
return safeCall(value);
5148
}
5249

5350
private long checkLatency(int value) {
54-
RemoteService service = RemoteService.getRemoteService();
5551
long startTime = System.currentTimeMillis();
56-
long result = service.doRemoteFunction(value);
52+
long result = RemoteService.getRemoteService().doRemoteFunction(value);
5753
long timeTaken = System.currentTimeMillis() - startTime;
5854

5955
LOGGER.info("Time taken (ms): " + timeTaken);
@@ -77,7 +73,7 @@ private long safeCall(int value) {
7773
try {
7874
sleep(DELAY_MS);
7975
} catch (InterruptedException e) {
80-
e.printStackTrace();
76+
LOGGER.error("Thread sleep state interrupted", e);
8177
}
8278
} else {
8379
break;

ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public class RemoteServiceTest {
3131

3232
@Test
3333
public void test() {
34-
35-
RemoteService remoteService = RemoteService.getRemoteService();
36-
long result = remoteService.doRemoteFunction(10);
37-
34+
long result = RemoteService.getRemoteService().doRemoteFunction(10);
3835
assert result == 100 || result == -1;
3936
}
4037
}

ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public class ServiceAmbassadorTest {
3131

3232
@Test
3333
public void test() {
34-
ServiceAmbassador ambassador = new ServiceAmbassador();
35-
long result = ambassador.doRemoteFunction(10);
34+
long result = new ServiceAmbassador().doRemoteFunction(10);
3635
assert result == 100 || result == -1;
3736
}
3837
}

0 commit comments

Comments
 (0)