|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.throttling; |
| 25 | + |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import com.iluwatar.throttling.timer.Throttler; |
| 30 | +import com.iluwatar.throttling.timer.ThrottleTimerImpl; |
| 31 | + |
| 32 | +import java.util.concurrent.ExecutorService; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | + |
| 36 | +/** |
| 37 | + * Throttling pattern is a design pattern to throttle or limit the use of resources or even a complete service by |
| 38 | + * users or a particular tenant. This can allow systems to continue to function and meet service level agreements, |
| 39 | + * even when an increase in demand places load on resources. |
| 40 | + * <p> |
| 41 | + * In this example we have ({@link App}) as the initiating point of the service. |
| 42 | + * This is a time based throttling, i.e. only a certain number of calls are allowed per second. |
| 43 | + * </p> |
| 44 | + * ({@link Tenant}) is the Tenant POJO class with which many tenants can be created |
| 45 | + * ({@link B2BService}) is the service which is consumed by the tenants and is throttled. |
| 46 | + */ |
| 47 | +public class App { |
| 48 | + |
| 49 | + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |
| 50 | + |
| 51 | + /** |
| 52 | + * Application entry point |
| 53 | + * @param args main arguments |
| 54 | + */ |
| 55 | + public static void main(String[] args) { |
| 56 | + |
| 57 | + Tenant adidas = new Tenant("Adidas", 5); |
| 58 | + Tenant nike = new Tenant("Nike", 6); |
| 59 | + |
| 60 | + ExecutorService executorService = Executors.newFixedThreadPool(2); |
| 61 | + |
| 62 | + executorService.execute(() -> makeServiceCalls(adidas)); |
| 63 | + executorService.execute(() -> makeServiceCalls(nike)); |
| 64 | + |
| 65 | + executorService.shutdown(); |
| 66 | + try { |
| 67 | + executorService.awaitTermination(10, TimeUnit.SECONDS); |
| 68 | + } catch (InterruptedException e) { |
| 69 | + LOGGER.error("Executor Service terminated: {}", e.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Make calls to the B2BService dummy API |
| 75 | + * @param service an instance of B2BService |
| 76 | + */ |
| 77 | + private static void makeServiceCalls(Tenant tenant) { |
| 78 | + Throttler timer = new ThrottleTimerImpl(10); |
| 79 | + B2BService service = new B2BService(timer); |
| 80 | + for (int i = 0; i < 20; i++) { |
| 81 | + service.dummyCustomerApi(tenant); |
| 82 | +// Sleep is introduced to keep the output in check and easy to view and analyze the results. |
| 83 | + try { |
| 84 | + Thread.sleep(1); |
| 85 | + } catch (InterruptedException e) { |
| 86 | + LOGGER.error("Thread interrupted: {}", e.getMessage()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments