Java Code Examples for org.springframework.util.concurrent.ListenableFuture#isDone()

The following examples show how to use org.springframework.util.concurrent.ListenableFuture#isDone() . 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: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void putCallback() throws Exception  {
	HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld);
	ListenableFuture<?> responseEntityFuture = template.put(baseUrl + "/{method}", requestEntity, "put");
	responseEntityFuture.addCallback(new ListenableFutureCallback<Object>() {
		@Override
		public void onSuccess(Object result) {
			assertNull(result);
		}
		@Override
		public void onFailure(Throwable ex) {
			fail(ex.getMessage());
		}
	});
	while (!responseEntityFuture.isDone()) {
	}
}
 
Example 2
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void exchangeGetCallback() throws Exception {
	HttpHeaders requestHeaders = new HttpHeaders();
	requestHeaders.set("MyHeader", "MyValue");
	HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
	ListenableFuture<ResponseEntity<String>> responseFuture =
			template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
	responseFuture.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
		@Override
		public void onSuccess(ResponseEntity<String> result) {
			assertEquals("Invalid content", helloWorld, result.getBody());
		}
		@Override
		public void onFailure(Throwable ex) {
			fail(ex.getMessage());
		}
	});
	while (!responseFuture.isDone()) {
	}
}
 
Example 3
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void exchangePostCallbackWithLambdas() throws Exception {
	HttpHeaders requestHeaders = new HttpHeaders();
	requestHeaders.set("MyHeader", "MyValue");
	requestHeaders.setContentType(MediaType.TEXT_PLAIN);
	HttpEntity<String> requestEntity = new HttpEntity<String>(helloWorld, requestHeaders);
	ListenableFuture<ResponseEntity<Void>> resultFuture =
			template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
	final URI expected =new URI(baseUrl + "/post/1");
	resultFuture.addCallback(result -> {
		assertEquals("Invalid location", expected, result.getHeaders().getLocation());
		assertFalse(result.hasBody());
		}, ex -> fail(ex.getMessage()));
	while (!resultFuture.isDone()) {
	}
}
 
Example 4
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void serverErrorCallback() throws Exception {
	ListenableFuture<Void> future = template.execute(baseUrl + "/status/server", HttpMethod.GET, null, null);
	future.addCallback(new ListenableFutureCallback<Void>() {
		@Override
		public void onSuccess(Void result) {
			fail("onSuccess not expected");
		}
		@Override
		public void onFailure(Throwable ex) {
			assertTrue(ex instanceof HttpServerErrorException);
			HttpServerErrorException hsex = (HttpServerErrorException) ex;
			assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, hsex.getStatusCode());
			assertNotNull(hsex.getStatusText());
			assertNotNull(hsex.getResponseBodyAsString());
		}
	});
	while (!future.isDone()) {
	}
}
 
Example 5
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void notFoundCallback() throws Exception {
	ListenableFuture<?> future = template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null);
	future.addCallback(new ListenableFutureCallback<Object>() {
		@Override
		public void onSuccess(Object result) {
			fail("onSuccess not expected");
		}
		@Override
		public void onFailure(Throwable t) {
			assertTrue(t instanceof HttpClientErrorException);
			HttpClientErrorException ex = (HttpClientErrorException) t;
			assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode());
			assertNotNull(ex.getStatusText());
			assertNotNull(ex.getResponseBodyAsString());
		}
	});
	while (!future.isDone()) {
	}
}
 
Example 6
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteCallback() throws Exception  {
	ListenableFuture<?> deletedFuture = template.delete(new URI(baseUrl + "/delete"));
	deletedFuture.addCallback(new ListenableFutureCallback<Object>() {
		@Override
		public void onSuccess(Object result) {
			assertNull(result);
		}
		@Override
		public void onFailure(Throwable ex) {
			fail(ex.getMessage());
		}
	});
	while (!deletedFuture.isDone()) {
	}
}
 
Example 7
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void postForEntityCallback() throws Exception  {
	HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld);
	ListenableFuture<ResponseEntity<String>> responseEntityFuture =
			template.postForEntity(baseUrl + "/{method}", requestEntity, String.class, "post");
	responseEntityFuture.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
		@Override
		public void onSuccess(ResponseEntity<String> result) {
			assertEquals("Invalid content", helloWorld, result.getBody());
		}
		@Override
		public void onFailure(Throwable ex) {
			fail(ex.getMessage());
		}
	});
	while (!responseEntityFuture.isDone()) {
	}
}
 
Example 8
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void headForHeadersCallback() throws Exception {
	ListenableFuture<HttpHeaders> headersFuture = template.headForHeaders(baseUrl + "/get");
	headersFuture.addCallback(new ListenableFutureCallback<HttpHeaders>() {
		@Override
		public void onSuccess(HttpHeaders result) {
			assertTrue("No Content-Type header", result.containsKey("Content-Type"));
		}
		@Override
		public void onFailure(Throwable ex) {
			fail(ex.getMessage());
		}
	});
	while (!headersFuture.isDone()) {
	}
}
 
Example 9
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getEntityCallback() throws Exception {
	ListenableFuture<ResponseEntity<String>> futureEntity =
			template.getForEntity(baseUrl + "/{method}", String.class, "get");
	futureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
		@Override
		public void onSuccess(ResponseEntity<String> entity) {
			assertEquals("Invalid content", helloWorld, entity.getBody());
			assertFalse("No headers", entity.getHeaders().isEmpty());
			assertEquals("Invalid content-type", textContentType, entity.getHeaders().getContentType());
			assertEquals("Invalid status code", HttpStatus.OK, entity.getStatusCode());
		}
		@Override
		public void onFailure(Throwable ex) {
			fail(ex.getMessage());
		}
	});
	// wait till done
	while (!futureEntity.isDone()) {
	}
}
 
Example 10
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void postForLocationCallbackWithLambdas() throws Exception  {
	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
	HttpEntity<String> entity = new HttpEntity<String>(helloWorld, entityHeaders);
	final URI expected = new URI(baseUrl + "/post/1");
	ListenableFuture<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post");
	locationFuture.addCallback(result -> assertEquals("Invalid location", expected, result),
			ex -> fail(ex.getMessage()));
	while (!locationFuture.isDone()) {
	}
}
 
Example 11
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void exchangeGetCallbackWithLambdas() throws Exception {
	HttpHeaders requestHeaders = new HttpHeaders();
	requestHeaders.set("MyHeader", "MyValue");
	HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
	ListenableFuture<ResponseEntity<String>> responseFuture =
			template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
	responseFuture.addCallback(result -> assertEquals("Invalid content", helloWorld,
			result.getBody()), ex -> fail(ex.getMessage()));
	while (!responseFuture.isDone()) {
	}
}
 
Example 12
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void postForEntityCallbackWithLambdas() throws Exception  {
	HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld);
	ListenableFuture<ResponseEntity<String>> responseEntityFuture =
			template.postForEntity(baseUrl + "/{method}", requestEntity, String.class, "post");
	responseEntityFuture.addCallback(result -> assertEquals("Invalid content", helloWorld, result.getBody()),
			ex -> fail(ex.getMessage()));
	while (!responseEntityFuture.isDone()) {
	}
}
 
Example 13
Source File: AsyncRestTemplateIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void waitTillDone(ListenableFuture<?> future) {
	while (!future.isDone()) {
		try {
			Thread.sleep(5);
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
	}
}
 
Example 14
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void headForHeadersCallbackWithLambdas() throws Exception {
	ListenableFuture<HttpHeaders> headersFuture = template.headForHeaders(baseUrl + "/get");
	headersFuture.addCallback(result -> assertTrue("No Content-Type header",
			result.containsKey("Content-Type")), ex -> fail(ex.getMessage()));
	while (!headersFuture.isDone()) {
	}
}
 
Example 15
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteCallbackWithLambdas() throws Exception  {
	ListenableFuture<?> deletedFuture = template.delete(new URI(baseUrl + "/delete"));
	deletedFuture.addCallback(result -> assertNull(result), ex -> fail(ex.getMessage()));
	while (!deletedFuture.isDone()) {
	}
}
 
Example 16
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void notFoundCallbackWithLambdas() throws Exception {
	ListenableFuture<?> future = template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null);
	future.addCallback(result -> fail("onSuccess not expected"), ex -> {
			assertTrue(ex instanceof HttpClientErrorException);
			HttpClientErrorException hcex = (HttpClientErrorException) ex;
			assertEquals(HttpStatus.NOT_FOUND, hcex.getStatusCode());
			assertNotNull(hcex.getStatusText());
			assertNotNull(hcex.getResponseBodyAsString());
	});
	while (!future.isDone()) {
	}
}
 
Example 17
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getEntityCallbackWithLambdas() throws Exception {
	ListenableFuture<ResponseEntity<String>> futureEntity =
			template.getForEntity(baseUrl + "/{method}", String.class, "get");
	futureEntity.addCallback((entity) -> {
		assertEquals("Invalid content", helloWorld, entity.getBody());
		assertFalse("No headers", entity.getHeaders().isEmpty());
		assertEquals("Invalid content-type", textContentType, entity.getHeaders().getContentType());
		assertEquals("Invalid status code", HttpStatus.OK, entity.getStatusCode());
	}, ex -> fail(ex.getMessage()));
	// wait till done
	while (!futureEntity.isDone()) {
	}
}
 
Example 18
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void serverErrorCallbackWithLambdas() throws Exception {
	ListenableFuture<Void> future = template.execute(baseUrl + "/status/server", HttpMethod.GET, null, null);
	future.addCallback(result -> fail("onSuccess not expected"), ex -> {
			assertTrue(ex instanceof HttpServerErrorException);
			HttpServerErrorException hsex = (HttpServerErrorException) ex;
			assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, hsex.getStatusCode());
			assertNotNull(hsex.getStatusText());
			assertNotNull(hsex.getResponseBodyAsString());
	});
	while (!future.isDone()) {
	}
}
 
Example 19
Source File: AsyncRestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void optionsForAllowCallbackWithLambdas() throws Exception{
	ListenableFuture<Set<HttpMethod>> allowedFuture = template.optionsForAllow(new URI(baseUrl + "/get"));
	allowedFuture.addCallback(result -> assertEquals("Invalid response",
			EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD,HttpMethod.TRACE), result),
			ex -> fail(ex.getMessage()));
	while (!allowedFuture.isDone()) {
	}
}
 
Example 20
Source File: AsyncRestTemplateIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void waitTillDone(ListenableFuture<?> future) {
	while (!future.isDone()) {
	}
}