Visitar URL original
Example using streaming · Issue #1054 · scribejava/scribejava · GitHub
Skip to content

Example using streaming #1054

Open
Open

Description

@snicoll

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.

Activity

  1. snicoll commented on Mar 2, 2023

    @snicoll
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions