Example usage for org.springframework.util.concurrent ListenableFuture get

List of usage examples for org.springframework.util.concurrent ListenableFuture get

Introduction

In this page you can find the example usage for org.springframework.util.concurrent ListenableFuture get.

Prototype

V get() throws InterruptedException, ExecutionException;

Source Link

Document

Waits if necessary for the computation to complete, and then retrieves its result.

Usage

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void ableToPostObjectAsJson() throws Exception {
    Map<String, String> personFieldMap = new HashMap<>();
    personFieldMap.put("name", "person name from map");

    Person person = restTemplate.postForObject(codeFirstUrl + "sayhello", jsonRequest(personFieldMap),
            Person.class);
    assertThat(person.toString(), is("hello person name from map"));

    Person input = new Person();
    input.setName("person name from Object");
    person = restTemplate.postForObject(codeFirstUrl + "sayhello", jsonRequest(input), Person.class);

    assertThat(person.toString(), is("hello person name from Object"));

    ListenableFuture<ResponseEntity<Person>> listenableFuture = asyncRestTemplate
            .postForEntity(codeFirstUrl + "sayhello", jsonRequest(personFieldMap), Person.class);
    ResponseEntity<Person> futureResponse = listenableFuture.get();
    person = futureResponse.getBody();/*from   w  ww .ja  va2  s .  c  o  m*/
    assertThat(person.toString(), is("hello person name from map"));

    listenableFuture = asyncRestTemplate.postForEntity(codeFirstUrl + "sayhello", jsonRequest(input),
            Person.class);
    futureResponse = listenableFuture.get();
    person = futureResponse.getBody();
    assertThat(person.toString(), is("hello person name from Object"));
}

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void ableToPostForm() throws Exception {
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("a", "5");
    params.add("b", "3");

    HttpHeaders headers = new HttpHeaders();
    headers.add(CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE);
    int result = restTemplate.postForObject(codeFirstUrl + "add", new HttpEntity<>(params, headers),
            Integer.class);

    assertThat(result, is(8));//from   w  w  w.j  a v a2  s .  c o  m

    ListenableFuture<ResponseEntity<Integer>> listenableFuture = asyncRestTemplate
            .postForEntity(codeFirstUrl + "add", new HttpEntity<>(params, headers), Integer.class);
    ResponseEntity<Integer> futureResponse = listenableFuture.get();
    assertThat(futureResponse.getBody(), is(8));
}

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void ableToExchangeCookie() throws Exception {
    Map<String, String> params = new HashMap<>();
    params.put("a", "5");

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.COOKIE, "b=3");

    HttpEntity<?> requestEntity = new HttpEntity<>(headers);
    ResponseEntity<Integer> result = restTemplate.exchange(codeFirstUrl + "reduce?a={a}", GET, requestEntity,
            Integer.class, params);

    assertThat(result.getBody(), is(2));

    ListenableFuture<ResponseEntity<Integer>> listenableFuture = asyncRestTemplate
            .exchange(codeFirstUrl + "reduce?a={a}", GET, requestEntity, Integer.class, params);
    result = listenableFuture.get();
    assertThat(result.getBody(), is(2));
}

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void getsEndWithRequestVariables() throws Exception {
    int result = restTemplate.getForObject(controllerUrl + "add?a={a}&b={b}", Integer.class, 3, 4);

    assertThat(result, is(7));//from  w  ww .  j  av  a 2 s  . c om
    ListenableFuture<ResponseEntity<Integer>> listenableFuture = asyncRestTemplate
            .getForEntity(controllerUrl + "add?a={a}&b={b}", Integer.class, 3, 4);
    ResponseEntity<Integer> futureResponse = listenableFuture.get();
    assertThat(futureResponse.getBody(), is(7));
}

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void postsEndWithPathParam() throws Exception {
    String result = restTemplate.postForObject(controllerUrl + "sayhello/{name}", null, String.class, "world");

    assertThat(jsonOf(result, String.class), is("hello world"));

    List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(converters);
    result = restTemplate.postForObject(controllerUrl + "sayhello/{name}", null, String.class, " ");

    assertThat(result, is("hello  "));
    restTemplate.setMessageConverters(convertersOld);

    ListenableFuture<ResponseEntity<String>> listenableFuture = asyncRestTemplate
            .postForEntity(controllerUrl + "sayhello/{name}", null, String.class, "world");
    ResponseEntity<String> futureResonse = listenableFuture.get();
    assertThat(jsonOf(futureResonse.getBody(), String.class), is("hello world"));
    asyncRestTemplate.setMessageConverters(converters);
    listenableFuture = asyncRestTemplate.postForEntity(controllerUrl + "sayhello/{name}", null, String.class,
            " ");
    futureResonse = listenableFuture.get();
    assertThat(futureResonse.getBody(), is("hello  "));
    asyncRestTemplate.setMessageConverters(convertersOld);
}

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void ableToPostObjectAsJsonWithRequestVariable() throws Exception {
    Person input = new Person();
    input.setName("world");

    String result = restTemplate.postForObject(controllerUrl + "saysomething?prefix={prefix}",
            jsonRequest(input), String.class, "hello");

    assertThat(jsonOf(result, String.class), is("hello world"));

    List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(converters);
    input = new Person();
    input.setName("");

    result = restTemplate.postForObject(controllerUrl + "saysomething?prefix={prefix}", jsonRequest(input),
            String.class, "hello");

    assertThat(result, is("hello "));
    restTemplate.setMessageConverters(convertersOld);

    input.setName("world");
    ListenableFuture<ResponseEntity<String>> listenableFuture = asyncRestTemplate.postForEntity(
            controllerUrl + "saysomething?prefix={prefix}", jsonRequest(input), String.class, "hello");
    ResponseEntity<String> futureResponse = listenableFuture.get();
    assertThat(jsonOf(futureResponse.getBody(), String.class), is("hello world"));

    asyncRestTemplate.setMessageConverters(converters);
    input.setName("");
    listenableFuture = asyncRestTemplate.postForEntity(controllerUrl + "saysomething?prefix={prefix}",
            jsonRequest(input), String.class, "hello");
    futureResponse = listenableFuture.get();
    assertThat(futureResponse.getBody(), is("hello "));
    asyncRestTemplate.setMessageConverters(convertersOld);
}

From source file:org.apache.servicecomb.demo.springmvc.tests.SpringMvcIntegrationTestBase.java

@Test
public void ensureServerWorksFine() throws Exception {
    String result = restTemplate.getForObject(controllerUrl + "sayhi?name=world", String.class);

    assertThat(jsonOf(result, String.class), is("hi world [world]"));
    ListenableFuture<ResponseEntity<String>> listenableFuture = asyncRestTemplate
            .getForEntity(controllerUrl + "sayhi?name=world", String.class);
    ResponseEntity<String> futureResponse = listenableFuture.get();
    assertThat(jsonOf(futureResponse.getBody(), String.class), is("hi world [world]"));
}

From source file:org.springframework.cloud.sleuth.instrument.web.client.TraceAsyncRestTemplate.java

@Override
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, AsyncRequestCallback requestCallback,
        ResponseExtractor<T> responseExtractor) throws RestClientException {
    final ListenableFuture<T> future = super.doExecute(url, method, requestCallback, responseExtractor);
    final Span span = this.tracer.getCurrentSpan();
    future.addCallback(new TraceListenableFutureCallback<>(this.tracer, span));
    // potential race can happen here
    if (span != null && span.equals(this.tracer.getCurrentSpan())) {
        this.tracer.detach(span);
    }//from  w  w  w .  ja va 2 s  .c o m
    return new ListenableFuture<T>() {

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return future.cancel(mayInterruptIfRunning);
        }

        @Override
        public boolean isCancelled() {
            return future.isCancelled();
        }

        @Override
        public boolean isDone() {
            return future.isDone();
        }

        @Override
        public T get() throws InterruptedException, ExecutionException {
            return future.get();
        }

        @Override
        public T get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            return future.get(timeout, unit);
        }

        @Override
        public void addCallback(ListenableFutureCallback<? super T> callback) {
            future.addCallback(new TraceListenableFutureCallbackWrapper<>(TraceAsyncRestTemplate.this.tracer,
                    span, callback));
        }

        @Override
        public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) {
            future.addCallback(
                    new TraceSuccessCallback<>(TraceAsyncRestTemplate.this.tracer, span, successCallback),
                    new TraceFailureCallback(TraceAsyncRestTemplate.this.tracer, span, failureCallback));
        }
    };
}

From source file:org.springframework.messaging.simp.stomp.Reactor11TcpStompClientTests.java

@Test
public void publishSubscribe() throws Exception {

    String destination = "/topic/foo";
    ConsumingHandler consumingHandler1 = new ConsumingHandler(destination);
    ListenableFuture<StompSession> consumerFuture1 = this.client.connect(consumingHandler1);

    ConsumingHandler consumingHandler2 = new ConsumingHandler(destination);
    ListenableFuture<StompSession> consumerFuture2 = this.client.connect(consumingHandler2);

    assertTrue(consumingHandler1.awaitForSubscriptions(5000));
    assertTrue(consumingHandler2.awaitForSubscriptions(5000));

    ProducingHandler producingHandler = new ProducingHandler();
    producingHandler.addToSend(destination, "foo1");
    producingHandler.addToSend(destination, "foo2");
    ListenableFuture<StompSession> producerFuture = this.client.connect(producingHandler);

    assertTrue(consumingHandler1.awaitForMessageCount(2, 5000));
    assertThat(consumingHandler1.getReceived(), containsInAnyOrder("foo1", "foo2"));

    assertTrue(consumingHandler2.awaitForMessageCount(2, 5000));
    assertThat(consumingHandler2.getReceived(), containsInAnyOrder("foo1", "foo2"));

    consumerFuture1.get().disconnect();
    consumerFuture2.get().disconnect();//from   w w w. j ava 2  s .c  o m
    producerFuture.get().disconnect();
}

From source file:org.springframework.messaging.simp.stomp.Reactor2TcpStompClientTests.java

@Test
public void publishSubscribe() throws Exception {
    String destination = "/topic/foo";
    ConsumingHandler consumingHandler1 = new ConsumingHandler(destination);
    ListenableFuture<StompSession> consumerFuture1 = this.client.connect(consumingHandler1);

    ConsumingHandler consumingHandler2 = new ConsumingHandler(destination);
    ListenableFuture<StompSession> consumerFuture2 = this.client.connect(consumingHandler2);

    assertTrue(consumingHandler1.awaitForSubscriptions(5000));
    assertTrue(consumingHandler2.awaitForSubscriptions(5000));

    ProducingHandler producingHandler = new ProducingHandler();
    producingHandler.addToSend(destination, "foo1");
    producingHandler.addToSend(destination, "foo2");
    ListenableFuture<StompSession> producerFuture = this.client.connect(producingHandler);

    assertTrue(consumingHandler1.awaitForMessageCount(2, 5000));
    assertThat(consumingHandler1.getReceived(), containsInAnyOrder("foo1", "foo2"));

    assertTrue(consumingHandler2.awaitForMessageCount(2, 5000));
    assertThat(consumingHandler2.getReceived(), containsInAnyOrder("foo1", "foo2"));

    consumerFuture1.get().disconnect();
    consumerFuture2.get().disconnect();/*  ww w . j  av a  2 s.c  om*/
    producerFuture.get().disconnect();
}