Skip to content

Commit 73925ce

Browse files
committed
Ambassador adds logging, imitates trying to connect to remote service and provides new client-side functionality. Need to clean up code, add tests and add descriptive comments.
1 parent c713dbc commit 73925ce

File tree

7 files changed

+244
-0
lines changed

7 files changed

+244
-0
lines changed

ambassador/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>com.iluwatar</groupId>
8+
<version>1.20.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>ambassador</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar.ambassador;
2+
3+
public class App {
4+
5+
/**
6+
* Entry point
7+
*/
8+
public static void main(String[] args) {
9+
10+
Client host1 = new Client();
11+
Client host2 = new Client();
12+
13+
host1.useService(12);
14+
host2.useService(73);
15+
16+
host1.useNewService(12);
17+
host2.useNewService(73);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.ambassador;
24+
25+
public class Client {
26+
27+
private ServiceAmbassador serviceAmbassador;
28+
29+
Client() {
30+
serviceAmbassador = new ServiceAmbassador();
31+
}
32+
33+
void useService(int value) {
34+
long result = serviceAmbassador.doRemoteFunction(value);
35+
System.out.println(result);
36+
}
37+
38+
void useNewService(int value) {
39+
long result = serviceAmbassador.doAddedFunction(value);
40+
System.out.println(result);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.ambassador;
24+
25+
import static java.lang.Thread.sleep;
26+
27+
public class RemoteService implements RemoteServiceInterface {
28+
29+
private static RemoteService service = null;
30+
31+
static synchronized RemoteService getRemoteService() {
32+
if (service == null) {
33+
service = new RemoteService();
34+
}
35+
return service;
36+
}
37+
38+
private RemoteService() {
39+
40+
}
41+
42+
@Override
43+
public long doRemoteFunction(int value) {
44+
45+
long waitTime = (long) Math.floor(Math.random() * 1000);
46+
47+
try {
48+
sleep(waitTime);
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
return waitTime >= 200 ? value * 10 : -1;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.ambassador;
24+
25+
interface RemoteServiceInterface {
26+
27+
long doRemoteFunction(int value) throws Exception;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.ambassador;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import static java.lang.Thread.sleep;
29+
30+
public class ServiceAmbassador implements RemoteServiceInterface {
31+
32+
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
33+
private static final int RETRIES = 3;
34+
private static final int DELAY_MS = 3000;
35+
36+
ServiceAmbassador() {
37+
38+
}
39+
40+
@Override
41+
public long doRemoteFunction(int value) {
42+
43+
return safeCall(value);
44+
}
45+
46+
long doAddedFunction(int value) {
47+
return safeCall(value) * 5;
48+
}
49+
50+
private long checkLatency(int value) {
51+
RemoteService service = RemoteService.getRemoteService();
52+
long startTime = System.currentTimeMillis();
53+
long result = service.doRemoteFunction(value);
54+
long timeTaken = System.currentTimeMillis() - startTime;
55+
56+
LOGGER.info("Time taken (ms): " + timeTaken);
57+
return result;
58+
}
59+
60+
private long safeCall(int value) {
61+
62+
int retries = 0;
63+
long result = -1;
64+
65+
for (int i = 0; i < RETRIES; i++) {
66+
67+
if (retries >= RETRIES) {
68+
return -1;
69+
}
70+
71+
if ((result = checkLatency(value)) == -1) {
72+
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
73+
retries++;
74+
try {
75+
sleep(DELAY_MS);
76+
} catch (InterruptedException e) {
77+
e.printStackTrace();
78+
}
79+
} else {
80+
break;
81+
}
82+
}
83+
return result;
84+
}
85+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<module>dirty-flag</module>
162162
<module>trampoline</module>
163163
<module>serverless</module>
164+
<module>ambassador</module>
164165
</modules>
165166

166167
<repositories>

0 commit comments

Comments
 (0)