Resolves checkstyle errors for template-method thread-pool throttling tls tolerant-reader (#1073)

* Reduces checkstyle errors in template-method

* Reduces checkstyle errors in thread-pool

* Reduces checkstyle errors in throttling

* Reduces checkstyle errors in tls

* Reduces checkstyle errors in tolerant-reader
This commit is contained in:
Anurag Agarwal
2019-11-10 23:15:17 +05:30
committed by Ilkka Seppälä
parent 9c8ad4485b
commit b92eb5229d
23 changed files with 176 additions and 223 deletions

View File

@ -23,33 +23,30 @@
package com.iluwatar.throttling;
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.iluwatar.throttling.timer.Throttler;
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* 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.
* 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 we have ({@link App}) as the initiating point of the service.
* This is a time based throttling, i.e. only a certain number of calls are allowed per second.
* In this example we have ({@link App}) as the initiating point of the service. This is a time
* based throttling, i.e. only a certain number of calls are allowed per second.
* </p>
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created
* ({@link B2BService}) is the service which is consumed by the tenants and is throttled.
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created ({@link
* B2BService}) is the service which is consumed by the tenants and is throttled.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Application entry point
* Application entry point.
*
* @param args main arguments
*/
public static void main(String[] args) {
@ -58,10 +55,10 @@ public class App {
var nike = new Tenant("Nike", 6, callsCount);
var executorService = Executors.newFixedThreadPool(2);
executorService.execute(() -> makeServiceCalls(adidas, callsCount));
executorService.execute(() -> makeServiceCalls(nike, callsCount));
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
@ -71,14 +68,14 @@ public class App {
}
/**
* Make calls to the B2BService dummy API
* Make calls to the B2BService dummy API.
*/
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
var timer = new ThrottleTimerImpl(10, callsCount);
var service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) {
service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
try {
Thread.sleep(1);
} catch (InterruptedException e) {

View File

@ -23,15 +23,14 @@
package com.iluwatar.throttling;
import com.iluwatar.throttling.timer.Throttler;
import java.util.concurrent.ThreadLocalRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.iluwatar.throttling.timer.Throttler;
import java.util.concurrent.ThreadLocalRandom;
/**
* A service which accepts a tenant and throttles the resource based on the time given to the tenant.
* A service which accepts a tenant and throttles the resource based on the time given to the
* tenant.
*/
class B2BService {
@ -44,6 +43,7 @@ class B2BService {
}
/**
* Calls dummy customer api.
*
* @return customer id which is randomly generated
*/

View File

@ -23,18 +23,17 @@
package com.iluwatar.throttling;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A class to keep track of the counter of different Tenants
* @author drastogi
* A class to keep track of the counter of different Tenants.
*
* @author drastogi
*/
public final class CallsCount {
@ -43,29 +42,32 @@ public final class CallsCount {
/**
* Add a new tenant to the map.
*
* @param tenantName name of the tenant.
*/
public void addTenant(String tenantName) {
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
}
/**
* Increment the count of the specified tenant.
*
* @param tenantName name of the tenant.
*/
public void incrementCount(String tenantName) {
tenantCallsCount.get(tenantName).incrementAndGet();
}
/**
*
* Get count of tenant based on tenant name.
*
* @param tenantName name of the tenant.
* @return the count of the tenant.
*/
public long getCount(String tenantName) {
return tenantCallsCount.get(tenantName).get();
}
/**
* Resets the count of all the tenants in the map.
*/

View File

@ -34,8 +34,9 @@ public class Tenant {
private int allowedCallsPerSecond;
/**
* Constructor.
*
* @param name Name of the tenant
* @param name Name of the tenant
* @param allowedCallsPerSecond The number of calls allowed for a particular tenant.
* @throws InvalidParameterException If number of calls is less than 0, throws exception.
*/

View File

@ -21,16 +21,12 @@
* THE SOFTWARE.
*/
/**
*
*/
package com.iluwatar.throttling.timer;
import com.iluwatar.throttling.CallsCount;
import java.util.Timer;
import java.util.TimerTask;
import com.iluwatar.throttling.CallsCount;
/**
* Implementation of throttler interface. This class resets the counter every second.
* @author drastogi
@ -45,7 +41,7 @@ public class ThrottleTimerImpl implements Throttler {
this.throttlePeriod = throttlePeriod;
this.callsCount = callsCount;
}
/**
* A timer is initiated with this method. The timer runs every second and resets the
* counter.

View File

@ -21,9 +21,6 @@
* THE SOFTWARE.
*/
/**
*
*/
package com.iluwatar.throttling.timer;
/**
@ -32,6 +29,6 @@ package com.iluwatar.throttling.timer;
*
*/
public interface Throttler {
void start();
}