Using OkHttpClient, I can do the following, which would allow me to send the request and process the response when possible.
okhttpClient
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onResponse(Call arg0, Response arg1) throws IOException {
log.info("Call success", arg1);
}
@Override
public void onFailure(Call arg0, IOException arg1) {
log.info("Call fail", arg1);
}
});
Is there a way to do this with Feign? I have enabled the okhttpclient for Feign, if that helps?
CodePudding user response:
See https://github.com/OpenFeign/feign#async-execution-via-completablefuture
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
CompletableFuture<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
public class MyApp {
public static void main(String... args) {
GitHub github = AsyncFeign.asyncBuilder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
Because their OkHttp client isn't async you would either need to use
AsyncClient.AsyncClient(client, executorService) or more optimally implement an OkHttpAsyncClient.
or RX https://github.com/OpenFeign/feign/tree/master/reactive
public interface GitHubReactiveX {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
Flowable<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
class Contributor {
String login;
public Contributor(String login) {
this.login = login;
}
}
}
public class ExampleRxJava2 {
public static void main(String args[]) {
GitHubReactiveX gitHub = RxJavaFeign.builder()
.target(GitHub.class, "https://api.github.com");
List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign")
.collect(Collectors.toList())
.block();
}
}