Giter Site home page Giter Site logo

Comments (2)

ribasco avatar ribasco commented on June 2, 2024

Sorry for the late response.

In your while loop, you have already closed the client after the first iteration, since you are using it inside a try-witch-resources block. You cannot re-use the same instance after closing it.

The rule of thumb is to only instantiate the client once (as it can be expensive) and close it once you are done using it.

Moving the client reference out of your while loop should fix it.

@Test
public void check() throws InterruptedException {
    int count = 1;
    Server server = new Server();
    server.setAddress("192.168.3.3:27005");

    try (SourceQueryClient sourceQueryClient = getClient()) {
        while (true) {
            int finalCount = count;
            final CountDownLatch latch = new CountDownLatch(1);
            System.out.printf("[%d] Request\n", finalCount);
            sourceQueryClient.getInfo(server.getInetAddress()).whenComplete((info, error) -> {
                latch.countDown();
                if (error != null) {
                    server.SetDownStatus();
                    return;
                }
                System.out.printf("[%d] Player count: %d\n", finalCount, info.getResult().getNumOfPlayers());
            });
            latch.await();
            count++;
            Thread.sleep(500);
        }
    } catch (CompletionException | IOException err) {
        err.printStackTrace();
    }
}

public SourceQueryClient getClient() {
    if (sourceQueryClient == null) sourceQueryClient = new SourceQueryClient();
    return sourceQueryClient;
}

More example:

from async-gamequery-lib.

ribasco avatar ribasco commented on June 2, 2024

No further feedback. Closing..

from async-gamequery-lib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.