|
| 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 | +package com.iluwatar.ambassador; |
| 24 | + |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import static java.lang.Thread.sleep; |
| 29 | + |
| 30 | +public class ServiceAmbassador implements RemoteServiceInterface { |
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); |
| 33 | + private static final int RETRIES = 3; |
| 34 | + private static final int DELAY_MS = 3000; |
| 35 | + |
| 36 | + ServiceAmbassador() { |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public long doRemoteFunction(int value) { |
| 42 | + |
| 43 | + return safeCall(value); |
| 44 | + } |
| 45 | + |
| 46 | + long doAddedFunction(int value) { |
| 47 | + return safeCall(value) * 5; |
| 48 | + } |
| 49 | + |
| 50 | + private long checkLatency(int value) { |
| 51 | + RemoteService service = RemoteService.getRemoteService(); |
| 52 | + long startTime = System.currentTimeMillis(); |
| 53 | + long result = service.doRemoteFunction(value); |
| 54 | + long timeTaken = System.currentTimeMillis() - startTime; |
| 55 | + |
| 56 | + LOGGER.info("Time taken (ms): " + timeTaken); |
| 57 | + return result; |
| 58 | + } |
| 59 | + |
| 60 | + private long safeCall(int value) { |
| 61 | + |
| 62 | + int retries = 0; |
| 63 | + long result = -1; |
| 64 | + |
| 65 | + for (int i = 0; i < RETRIES; i++) { |
| 66 | + |
| 67 | + if (retries >= RETRIES) { |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + if ((result = checkLatency(value)) == -1) { |
| 72 | + LOGGER.info("Failed to reach remote: (" + (i + 1) + ")"); |
| 73 | + retries++; |
| 74 | + try { |
| 75 | + sleep(DELAY_MS); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } else { |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + return result; |
| 84 | + } |
| 85 | +} |
0 commit comments