📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -48,9 +48,8 @@ interface RemoteServiceInterface {
|
||||
A remote services represented as a singleton.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public class RemoteService implements RemoteServiceInterface {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
||||
private static RemoteService service = null;
|
||||
|
||||
static synchronized RemoteService getRemoteService() {
|
||||
@ -80,9 +79,8 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
A service ambassador adding additional features such as logging, latency checks
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
|
||||
private static final int RETRIES = 3;
|
||||
private static final int DELAY_MS = 3000;
|
||||
|
||||
@ -132,9 +130,8 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
A client has a local service ambassador used to interact with the remote service:
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public class Client {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
|
||||
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
|
||||
|
||||
long useService(int value) {
|
||||
|
@ -23,20 +23,19 @@
|
||||
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A simple Client.
|
||||
*/
|
||||
@Slf4j
|
||||
public class Client {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
|
||||
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
|
||||
|
||||
long useService(int value) {
|
||||
var result = serviceAmbassador.doRemoteFunction(value);
|
||||
LOGGER.info("Service result: " + result);
|
||||
LOGGER.info("Service result: {}", result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -26,15 +26,14 @@ package com.iluwatar.ambassador;
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
import com.iluwatar.ambassador.util.RandomProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A remote legacy application represented by a Singleton implementation.
|
||||
*/
|
||||
@Slf4j
|
||||
public class RemoteService implements RemoteServiceInterface {
|
||||
private static final int THRESHOLD = 200;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
||||
private static RemoteService service = null;
|
||||
private final RandomProvider randomProvider;
|
||||
|
||||
@ -73,8 +72,9 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
sleep(waitTime);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return waitTime <= THRESHOLD ? value * 10
|
||||
: RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue();
|
||||
: RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue();
|
||||
}
|
||||
}
|
||||
|
@ -33,8 +33,7 @@ package com.iluwatar.ambassador;
|
||||
*/
|
||||
|
||||
public enum RemoteServiceStatus {
|
||||
FAILURE(-1)
|
||||
;
|
||||
FAILURE(-1);
|
||||
|
||||
private final long remoteServiceStatusValue;
|
||||
|
||||
|
@ -26,17 +26,16 @@ package com.iluwatar.ambassador;
|
||||
import static com.iluwatar.ambassador.RemoteServiceStatus.FAILURE;
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* ServiceAmbassador provides an interface for a ({@link Client}) to access ({@link RemoteService}).
|
||||
* The interface adds logging, latency testing and usage of the service in a safe way that will not
|
||||
* add stress to the remote service when connectivity issues occur.
|
||||
*/
|
||||
@Slf4j
|
||||
public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
|
||||
private static final int RETRIES = 3;
|
||||
private static final int DELAY_MS = 3000;
|
||||
|
||||
@ -53,7 +52,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
var result = RemoteService.getRemoteService().doRemoteFunction(value);
|
||||
var timeTaken = System.currentTimeMillis() - startTime;
|
||||
|
||||
LOGGER.info("Time taken (ms): " + timeTaken);
|
||||
LOGGER.info("Time taken (ms): {}", timeTaken);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -67,12 +66,13 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
}
|
||||
|
||||
if ((result = checkLatency(value)) == FAILURE.getRemoteServiceStatusValue()) {
|
||||
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
|
||||
LOGGER.info("Failed to reach remote: ({})", i + 1);
|
||||
retries++;
|
||||
try {
|
||||
sleep(DELAY_MS);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
|
Reference in New Issue
Block a user