mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-03 05:20:09 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
1 parent
371439aeaa
commit
0ca162a55c
1863 files changed
+14403
-17632
No files matched your search
@@ -34,6 +34,14 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>throttling</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
||||
@@ -34,12 +34,11 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* Throttling pattern is a design pattern to throttle or limit the use of resources or even a
|
||||
* complete service by users or a particular tenant. This can allow systems to continue to function
|
||||
* and meet service level agreements, even when an increase in demand places load on resources.
|
||||
* <p>
|
||||
* In this example there is a {@link Bartender} serving beer to {@link BarCustomer}s. This is a time
|
||||
* based throttling, i.e. only a certain number of calls are allowed per second.
|
||||
* </p>
|
||||
* ({@link BarCustomer}) is the service tenant class having a name and the number of calls allowed.
|
||||
* ({@link Bartender}) is the service which is consumed by the tenants and is throttled.
|
||||
*
|
||||
* <p>In this example there is a {@link Bartender} serving beer to {@link BarCustomer}s. This is a
|
||||
* time based throttling, i.e. only a certain number of calls are allowed per second. ({@link
|
||||
* BarCustomer}) is the service tenant class having a name and the number of calls allowed. ({@link
|
||||
* Bartender}) is the service which is consumed by the tenants and is throttled.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -69,20 +68,20 @@ public class App {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make calls to the bartender.
|
||||
*/
|
||||
/** Make calls to the bartender. */
|
||||
private static void makeServiceCalls(BarCustomer barCustomer, CallsCount callsCount) {
|
||||
var timer = new ThrottleTimerImpl(1000, callsCount);
|
||||
var service = new Bartender(timer, callsCount);
|
||||
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
|
||||
IntStream.range(0, 50).forEach(i -> {
|
||||
service.orderDrink(barCustomer);
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread interrupted: {}", e.getMessage());
|
||||
}
|
||||
});
|
||||
IntStream.range(0, 50)
|
||||
.forEach(
|
||||
i -> {
|
||||
service.orderDrink(barCustomer);
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread interrupted: {}", e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.throttling;
|
||||
import java.security.InvalidParameterException;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* BarCustomer is a tenant with a name and a number of allowed calls per second.
|
||||
*/
|
||||
/** BarCustomer is a tenant with a name and a number of allowed calls per second. */
|
||||
@Getter
|
||||
public class BarCustomer {
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Bartender is a service which accepts a BarCustomer (tenant) and throttles
|
||||
* the resource based on the time given to the tenant.
|
||||
* Bartender is a service which accepts a BarCustomer (tenant) and throttles the resource based on
|
||||
* the time given to the tenant.
|
||||
*/
|
||||
class Bartender {
|
||||
|
||||
@@ -45,6 +45,7 @@ class Bartender {
|
||||
|
||||
/**
|
||||
* Orders a drink from the bartender.
|
||||
*
|
||||
* @return customer id which is randomly generated
|
||||
*/
|
||||
public int orderDrink(BarCustomer barCustomer) {
|
||||
|
||||
@@ -29,10 +29,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A class to keep track of the counter of different Tenants.
|
||||
*
|
||||
*/
|
||||
/** A class to keep track of the counter of different Tenants. */
|
||||
@Slf4j
|
||||
public final class CallsCount {
|
||||
private final Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
|
||||
@@ -65,9 +62,7 @@ public final class CallsCount {
|
||||
return tenantCallsCount.get(tenantName).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the count of all the tenants in the map.
|
||||
*/
|
||||
/** Resets the count of all the tenants in the map. */
|
||||
public void reset() {
|
||||
tenantCallsCount.replaceAll((k, v) -> new AtomicLong(0));
|
||||
LOGGER.info("reset counters");
|
||||
|
||||
@@ -28,10 +28,7 @@ import com.iluwatar.throttling.CallsCount;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* Implementation of throttler interface. This class resets the counter every second.
|
||||
*
|
||||
*/
|
||||
/** Implementation of throttler interface. This class resets the counter every second. */
|
||||
public class ThrottleTimerImpl implements Throttler {
|
||||
|
||||
private final int throttlePeriod;
|
||||
@@ -42,17 +39,18 @@ public class ThrottleTimerImpl implements Throttler {
|
||||
this.callsCount = callsCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* A timer is initiated with this method. The timer runs every second and resets the
|
||||
* counter.
|
||||
*/
|
||||
/** A timer is initiated with this method. The timer runs every second and resets the counter. */
|
||||
@Override
|
||||
public void start() {
|
||||
new Timer(true).schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
callsCount.reset();
|
||||
}
|
||||
}, 0, throttlePeriod);
|
||||
new Timer(true)
|
||||
.schedule(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
callsCount.reset();
|
||||
}
|
||||
},
|
||||
0,
|
||||
throttlePeriod);
|
||||
}
|
||||
}
|
||||
@@ -24,10 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.throttling.timer;
|
||||
|
||||
/**
|
||||
* An interface for defining the structure of different types of throttling ways.
|
||||
*
|
||||
*/
|
||||
/** An interface for defining the structure of different types of throttling ways. */
|
||||
public interface Throttler {
|
||||
|
||||
void start();
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.throttling;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
@@ -24,18 +24,17 @@
|
||||
*/
|
||||
package com.iluwatar.throttling;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* TenantTest to test the creation of Tenant with valid parameters.
|
||||
*/
|
||||
import java.security.InvalidParameterException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** TenantTest to test the creation of Tenant with valid parameters. */
|
||||
class BarCustomerTest {
|
||||
|
||||
@Test
|
||||
void constructorTest() {
|
||||
assertThrows(InvalidParameterException.class, () -> new BarCustomer("sirBrave", -1, new CallsCount()));
|
||||
assertThrows(
|
||||
InvalidParameterException.class, () -> new BarCustomer("sirBrave", -1, new CallsCount()));
|
||||
}
|
||||
}
|
||||
@@ -30,9 +30,7 @@ import com.iluwatar.throttling.timer.Throttler;
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* B2BServiceTest class to test the B2BService
|
||||
*/
|
||||
/** B2BServiceTest class to test the B2BService */
|
||||
class BartenderTest {
|
||||
|
||||
private final CallsCount callsCount = new CallsCount();
|
||||
@@ -40,7 +38,8 @@ class BartenderTest {
|
||||
@Test
|
||||
void dummyCustomerApiTest() {
|
||||
var tenant = new BarCustomer("pirate", 2, callsCount);
|
||||
// In order to assure that throttling limits will not be reset, we use an empty throttling implementation
|
||||
// In order to assure that throttling limits will not be reset, we use an empty throttling
|
||||
// implementation
|
||||
var timer = (Throttler) () -> {};
|
||||
var service = new Bartender(timer, callsCount);
|
||||
|
||||
|
||||
Reference in new issue
Block a user