Skip to content

Commit

Permalink
Java 11 migration: ambassador async-method-invocation balking bridge …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 11, 2019
1 parent f0f0143 commit c441831
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 176 deletions.
20 changes: 12 additions & 8 deletions ambassador/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* 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;
Expand All @@ -50,7 +50,7 @@ private RemoteService() {
}

/**
* This constuctor is used for testing purposes only.
* This constructor is used for testing purposes only.
*/
RemoteService(RandomProvider randomProvider) {
this.randomProvider = randomProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,19 @@ public long doRemoteFunction(int value) {
}

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;
}
Expand Down
4 changes: 2 additions & 2 deletions ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
/**
* Application test
*/
public class AppTest {
class AppTest {

@Test
public void test() {
void test() {
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
/**
* 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
/**
* 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,24 @@ public class App {
*/
public static void main(String[] args) throws Exception {
// construct a new executor that will run async tasks
AsyncExecutor executor = new ThreadAsyncExecutor();
var executor = new ThreadAsyncExecutor();

// start few async tasks with varying processing times, two last with callback handlers
final AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
final AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
final AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
final AsyncResult<Integer> asyncResult4 =
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
final AsyncResult<String> asyncResult5 =
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
final var asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
final var asyncResult5 =
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));

// emulate processing in the current thread while async tasks are running in their own threads
Thread.sleep(350); // Oh boy I'm working hard here
log("Some hard work done");

// wait for completion of the tasks
final Integer result1 = executor.endProcess(asyncResult1);
final String result2 = executor.endProcess(asyncResult2);
final Long result3 = executor.endProcess(asyncResult3);
final var result1 = executor.endProcess(asyncResult1);
final var result2 = executor.endProcess(asyncResult2);
final var result3 = executor.endProcess(asyncResult3);
asyncResult4.await();
asyncResult5.await();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public <T> AsyncResult<T> startProcess(Callable<T> task) {

@Override
public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) {
CompletableResult<T> result = new CompletableResult<>(callback);
var result = new CompletableResult<>(callback);
new Thread(() -> {
try {
result.setValue(task.call());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
/**
* Application test
*/
public class AppTest {

class AppTest {
@Test
public void test() throws Exception {
String[] args = {};
App.main(args);
void test() throws Exception {
App.main(new String[]{});
}
}

0 comments on commit c441831

Please sign in to comment.