Panggilan asinkron JerseyClient tampaknya meninggalkan rangkaian pesan yang menggantung

Saya menggunakan jersey-client-3.0-SNAPSHOT.

Saya melakukan sesuatu seperti:

 final Client client = createClient();

...

    Builder builder = target.request();
    for (final Entry<String, String> entry : getHeaders().entrySet()) {
        builder = builder.header(entry.getKey(), entry.getValue());
    }
    final Builder finalBuilder = builder;
    executor.submit(() -> {
        final Entity<?> entity = createPostEntity();
        futureResponse = finalBuilder.async().post(entity);
        try {
            response = futureResponse.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
            consumeResponse(response);
        } catch (ExecutionException | TimeoutException | InterruptedException | IOException e) {
            errorConsumer.accept(e);
        }
    });

    if (futureResponse != null) {
        try {
            futureResponse.cancel(true);
        } catch (final Exception e) {
            //does nothing, now we try keep closing resources
        }
    }
    if (response != null) {
        try {
            response.close();
        } catch (final Exception e) {
            //does nothing, now we try keep closing resources
        }
    }

... //menunggu tanggapan dan membaca atau apa pun

client.close();

Dan thread baru terus muncul setiap kali membuat dan menghancurkan salah satu klien tersebut.

Apakah ada cara yang aman untuk menghancurkan thread tersebut? Apakah ini perilaku yang diharapkan? Apakah kamu melakukan sesuatu yang salah?


person eduyayo    schedule 02.06.2017    source sumber


Jawaban (1)


Dalam panggilan asynchronous di Jersey client, setiap kali kita memanggil close() pada objek client, itu menghancurkan thread yang digunakan dalam panggilan async. Jadi, perilaku yang diharapkan adalah setiap kali pernyataan client.close() dieksekusi, itu akan menghancurkan thread dan di lain waktu, thread baru akan dibuat untuk panggilan async berikutnya.

Sekarang, salah satu cara aman untuk menutup objek client dan thread terkait dengan mempertimbangkan skenario kesalahan di bawah -

    Client client = ClientBuilder.newClient();

    WebTarget webTarget = client.target(SERVER_URL).path(API_PATH);

    Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
    // set headers and other stuff

    AsyncInvoker asyncInvoker = invocationBuilder.async();

    asyncInvoker.get(new InvocationCallback<Response>() {

        @Override
        public void completed(Response response) {
            if (response.getStatusInfo().equals(Status.OK)) {
               // parse the response in success scenario
            } else {
               // parse the response if error response is received from server
            }
            client.close();
        }

        @Override
        public void failed(Throwable throwable) {
            System.out.println("An error occurred while calling API");
            throwable.printStackTrace();
            client.close();
        }
    });
person Vikas Sachdeva    schedule 10.06.2017