Created
January 19, 2021 20:05
-
-
Save judepereira/3d4c7c0189bc84ca922b703a7e5feff1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyStubbornAPIInterface actualInstance = ... // Create it however you'd create your original instance. | |
MyStubbornAPIInterface proxiedInstance = (MyStubbornAPIInterface) Proxy.newProxyInstance(actualInstance.getClass().getClassLoader(), | |
new Class[]{MyStubbornAPIInterface.class}, (proxy, method, args) -> { | |
while (true) { | |
try { | |
return method.invoke(actualInstance, args); | |
} catch (MyThrottlingException e) { | |
try { | |
Thread.sleep(ThreadLocalRandom.current().nextInt(1, 5) * 1000L); | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment