Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example using streaming #1054

Open
snicoll opened this issue Mar 2, 2023 · 1 comment
Open

Example using streaming #1054

snicoll opened this issue Mar 2, 2023 · 1 comment

Comments

@snicoll
Copy link

snicoll commented Mar 2, 2023

I am trying to get my head around why my code does not work when I try to use an async client (Apache) while the JDK works fine.

My use case is connecting to the Twitter stream endpoint. There's already such a client and it doesn't work with an async client (https://github.com/redouane59/twittered) either.

The code is here https://github.com/redouane59/twittered/blob/19134de9dc9ae82be1b61ac551c6a482a04017ab/src/main/java/io/github/redouane59/twitter/helpers/RequestHelperV2.java#L99

With the default JDK client, all is fine. With the Apache one, the callback is never invoked (neither the onCompleted or the onError). However, if I enable the logging of the client, I can see the connection is established and the stream is being consumed.

Example usage with Twittered:

ApacheHttpClient httpClient = new ApacheHttpClient(HttpAsyncClientBuilder.create()
        .setDefaultRequestConfig(RequestConfig.custom()
                .setCookieSpec(CookieSpecs.STANDARD)
                .setSocketTimeout((int) Duration.ofSeconds(30).toMillis()).build()));
TwitterClient twitterClient = new TwitterClient(credentials, httpClient);
System.out.println("Initiate stream");
twitterClient.startFilteredStream((tweet) -> System.out.println(tweet.getText()));

If I use the default (JDK) client, the callback is invoked.

@snicoll
Copy link
Author

snicoll commented Mar 2, 2023

The problem is that the request is "never" completed. I can reproduce this using the raw HttpClient, something like

CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
        .setDefaultRequestConfig(RequestConfig.custom()
                .setCookieSpec(CookieSpecs.STANDARD)
                .setSocketTimeout((int) Duration.ofSeconds(30).toMillis()).build()).build();
client.start();
HttpGet request = new HttpGet(createFilteredStreamUrl());
request.addHeader(OAuthConstants.HEADER, "Bearer " + this.twitterClient.getBearerToken());
client.execute(request, new FutureCallback<HttpResponse>() {
    @Override
    public void completed(HttpResponse httpResponse) {
        System.out.println("completed:" + httpResponse);
    }

    @Override
    public void failed(Exception e) {
        System.out.println("failed");
        e.printStackTrace();
    }

    @Override
    public void cancelled() {

    }
});

The completed callback is not invoked. And that makes sense since it's not flagged as completed:

2023-03-02 13:26:52,023 [I/O dispatcher 10] DEBUG org.apache.http.impl.nio.client.InternalIODispatch - http-outgoing-1 [ACTIVE] [chunk-coded; completed: false]

However, using the HttpAsyncResponseConsumer contract, I am able to do what I want. Have I missed something in the httpclient abstraction that allows this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant