org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate Java Examples

The following examples show how to use org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Test3rdPartyInvocation.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncInvoke_RestTemplate() throws ExecutionException, InterruptedException {
  CseAsyncRestTemplate cseAsyncRestTemplate = new CseAsyncRestTemplate();
  ListenableFuture<ResponseEntity<Integer>> responseFuture = cseAsyncRestTemplate
      .getForEntity(
          "cse://" + ASYNC_THIRD_PARTY_MICROSERVICE_NAME
              + "/v1/dataTypeJaxrs/intAdd?num1=11&num2=22",
          Integer.class);
  ResponseEntity<Integer> responseEntity = responseFuture.get();
  Assert.assertEquals(200, responseEntity.getStatusCodeValue());
  Assert.assertEquals(33, responseEntity.getBody().intValue());

  ListenableFuture<ResponseEntity<String>> stringBodyFuture = cseAsyncRestTemplate
      .exchange("cse://" + ASYNC_THIRD_PARTY_MICROSERVICE_NAME + "/v1/dataTypeJaxrs/stringBody",
          HttpMethod.POST,
          new HttpEntity<>("abc"), String.class);
  ResponseEntity<String> stringBodyResponse = stringBodyFuture.get();
  Assert.assertEquals(200, stringBodyResponse.getStatusCodeValue());
  Assert.assertEquals("abc", stringBodyResponse.getBody());
}
 
Example #2
Source File: SpringMvcIntegrationTestBase.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void ableToUploadFileFromConsumer() throws Exception {
  String file1Content = "hello world";
  String file2Content = "bonjour";
  String username = "mike";

  Map<String, Object> map = new HashMap<>();
  map.put("file1", new FileSystemResource(newFile(file1Content).getAbsolutePath()));
  map.put("someFile", new FileSystemResource(newFile(file2Content).getAbsolutePath()));
  map.put("name", username);

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  String result = RestTemplateBuilder.create().postForObject(
      "cse://springmvc-tests/codeFirstSpringmvc/upload",
      new HttpEntity<>(map, headers),
      String.class);

  assertThat(result, is(file1Content + file2Content + username));
  org.springframework.web.client.AsyncRestTemplate cseAsyncRestTemplate = new CseAsyncRestTemplate();
  ListenableFuture<ResponseEntity<String>> listenableFuture = cseAsyncRestTemplate
      .postForEntity("cse://springmvc-tests/codeFirstSpringmvc/upload",
          new HttpEntity<>(map, headers),
          String.class);
  ResponseEntity<String> responseEntity = listenableFuture.get();
  assertThat(responseEntity.getBody(), is(file1Content + file2Content + username));
}
 
Example #3
Source File: SpringmvcHelloClient.java    From servicecomb-samples with Apache License 2.0 4 votes vote down vote up
public void run() throws Exception {
  Person person = new Person();
  person.setName("ServiceComb/Java Chassis");

  // RestTemplate Consumer or POJO Consumer. You can choose whatever you like
  // RestTemplate Consumer
  String sayHiResult =
      restTemplate.postForObject("cse://springmvc/springmvchello/sayhi?name=Java Chassis", null, String.class);
  Assertion.assertEquals("Hello Java Chassis", sayHiResult);

  String sayHiDefaultResult =
      restTemplate.postForObject("cse://springmvc/springmvchello/sayhi", null, String.class);
  Assertion.assertEquals("Hello test", sayHiDefaultResult);

  String sayHelloResult = restTemplate.postForObject("cse://springmvc/springmvchello/sayhello", person, String.class);
  Assertion.assertEquals("Hello person ServiceComb/Java Chassis", sayHelloResult);

  System.out.println("RestTemplate Consumer or POJO Consumer.  You can choose whatever you like.");
  System.out.println("RestTemplate consumer sayhi services: " + sayHiResult);
  System.out.println("RestTemplate consumer sayHiDefault services: " + sayHiDefaultResult);
  System.out.println("RestTemplate consumer sayhello services: " + sayHelloResult);

  // POJO Consumer
  String pojoSayHi = hello.sayHi("Java Chassis");
  Assertion.assertEquals("Hello Java Chassis", pojoSayHi);
  String pojoSayHello = hello.sayHello(person);
  Assertion.assertEquals("Hello person ServiceComb/Java Chassis", pojoSayHello);

  System.out.println("POJO consumer sayhi services: " + pojoSayHi);
  System.out.println("POJO consumer sayhello services: " + pojoSayHello);

  //AsyncRestTemplate Consumer
  // NOTICE: since 2.0.0, spring deprecated AsyncRestTemplate, user's can use CompletableFuture of RPC instead
  CseAsyncRestTemplate cseAsyncRestTemplate = new CseAsyncRestTemplate();
  ListenableFuture<ResponseEntity<String>> responseEntityListenableFuture = cseAsyncRestTemplate
      .postForEntity("cse://springmvc/springmvchello/sayhi?name=Java Chassis", null, String.class);
  ResponseEntity<String> responseEntity = responseEntityListenableFuture.get();
  Assertion.assertEquals("Hello Java Chassis", responseEntity.getBody());
  System.out.println("AsyncRestTemplate Consumer sayHi services: " + responseEntity.getBody());

  HttpEntity<Person> entity = new HttpEntity<>(person);
  ListenableFuture<ResponseEntity<String>> listenableFuture = cseAsyncRestTemplate
      .exchange("cse://springmvc/springmvchello/sayhello", HttpMethod.POST, entity, String.class);

  listenableFuture.addCallback(
      new ListenableFutureCallback<ResponseEntity<String>>() {
        @Override
        public void onFailure(Throwable ex) {
          LOG.error("AsyncResTemplate Consumer catched exception when sayHello, ", ex);
        }

        @Override
        public void onSuccess(ResponseEntity<String> result) {
          System.out.println("AsyncRestTemplate Consumer sayHello services: " + result.getBody());
        }
      });
}
 
Example #4
Source File: Consumers.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public CseAsyncRestTemplate getCseAsyncRestTemplate() {
  return cseAsyncRestTemplate;
}