Skip to content

Instantly share code, notes, and snippets.

@judepereira
Created January 19, 2021 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judepereira/3d4c7c0189bc84ca922b703a7e5feff1 to your computer and use it in GitHub Desktop.
Save judepereira/3d4c7c0189bc84ca922b703a7e5feff1 to your computer and use it in GitHub Desktop.
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