Java 11 migration: ambassador async-method-invocation balking bridge builder (#1076)

* Moves ambassador pattern to java 11

* Moves async-method-invocation pattern  to java 11

* Moves balking pattern  to java 11

* Moves bridge pattern  to java 11

* Moves builder pattern  to java 11
This commit is contained in:
Anurag Agarwal
2019-11-12 01:17:09 +05:30
committed by Ilkka Seppälä
parent f0f0143d48
commit c4418311c6
27 changed files with 173 additions and 176 deletions

View File

@ -42,7 +42,7 @@ A remote services represented as a singleton.
```java
public class RemoteService implements RemoteServiceInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static RemoteService service = null;
static synchronized RemoteService getRemoteService() {
@ -56,14 +56,14 @@ public class RemoteService implements RemoteServiceInterface {
@Override
public long doRemoteFunction(int value) {
long waitTime = (long) Math.floor(Math.random() * 1000);
try {
sleep(waitTime);
} catch (InterruptedException e) {
LOGGER.error("Thread sleep interrupted", e)
LOGGER.error("Thread sleep interrupted", e);
}
return waitTime >= 200 ? value * 10 : -1;
}
}
@ -137,7 +137,7 @@ public class Client {
long useService(int value) {
long result = serviceAmbassador.doRemoteFunction(value);
LOGGER.info("Service result: " + result)
LOGGER.info("Service result: " + result);
return result;
}
}
@ -146,10 +146,14 @@ public class Client {
And here are two clients using the service.
```java
Client host1 = new Client();
Client host2 = new Client();
host1.useService(12);
host2.useService(73);
public class App {
public static void main(String[] args) {
Client host1 = new Client();
Client host2 = new Client();
host1.useService(12);
host2.useService(73);
}
}
```
## Applicability

View File

@ -35,7 +35,7 @@ public class Client {
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
long useService(int value) {
long result = serviceAmbassador.doRemoteFunction(value);
var result = serviceAmbassador.doRemoteFunction(value);
LOGGER.info("Service result: " + result);
return result;
}

View File

@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory;
* A remote legacy application represented by a Singleton implementation.
*/
public class RemoteService implements RemoteServiceInterface {
static final int THRESHOLD = 200;
private static final int THRESHOLD = 200;
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static RemoteService service = null;
private final RandomProvider randomProvider;
@ -50,7 +50,7 @@ public class RemoteService implements RemoteServiceInterface {
}
/**
* This constuctor is used for testing purposes only.
* This constructor is used for testing purposes only.
*/
RemoteService(RandomProvider randomProvider) {
this.randomProvider = randomProvider;

View File

@ -48,21 +48,19 @@ public class ServiceAmbassador implements RemoteServiceInterface {
}
private long checkLatency(int value) {
long startTime = System.currentTimeMillis();
long result = RemoteService.getRemoteService().doRemoteFunction(value);
long timeTaken = System.currentTimeMillis() - startTime;
var startTime = System.currentTimeMillis();
var result = RemoteService.getRemoteService().doRemoteFunction(value);
var timeTaken = System.currentTimeMillis() - startTime;
LOGGER.info("Time taken (ms): " + timeTaken);
return result;
}
private long safeCall(int value) {
int retries = 0;
long result = FAILURE;
var retries = 0;
var result = (long) FAILURE;
for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) {
return FAILURE;
}

View File

@ -28,10 +28,10 @@ import org.junit.jupiter.api.Test;
/**
* Application test
*/
public class AppTest {
class AppTest {
@Test
public void test() {
void test() {
App.main(new String[]{});
}
}

View File

@ -30,13 +30,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link Client}
*/
public class ClientTest {
class ClientTest {
@Test
public void test() {
void test() {
Client client = new Client();
long result = client.useService(10);
var result = client.useService(10);
assertTrue(result == 100 || result == RemoteService.FAILURE);
}

View File

@ -23,34 +23,31 @@
package com.iluwatar.ambassador;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.iluwatar.ambassador.util.RandomProvider;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link RemoteService}
*/
public class RemoteServiceTest {
class RemoteServiceTest {
@Test
public void testFailedCall() {
RemoteService remoteService = new RemoteService(
new StaticRandomProvider(0.21));
long result = remoteService.doRemoteFunction(10);
void testFailedCall() {
var remoteService = new RemoteService(new StaticRandomProvider(0.21));
var result = remoteService.doRemoteFunction(10);
assertEquals(RemoteServiceInterface.FAILURE, result);
}
@Test
public void testSuccessfulCall() {
RemoteService remoteService = new RemoteService(
new StaticRandomProvider(0.2));
long result = remoteService.doRemoteFunction(10);
void testSuccessfulCall() {
var remoteService = new RemoteService(new StaticRandomProvider(0.2));
var result = remoteService.doRemoteFunction(10);
assertEquals(100, result);
}
private class StaticRandomProvider implements RandomProvider {
private static class StaticRandomProvider implements RandomProvider {
private double value;
StaticRandomProvider(double value) {

View File

@ -30,10 +30,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link ServiceAmbassador}
*/
public class ServiceAmbassadorTest {
class ServiceAmbassadorTest {
@Test
public void test() {
void test() {
long result = new ServiceAmbassador().doRemoteFunction(10);
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
}