Provide Non-blocking REST API:
The benefit of non-blocking API will benefit a lot for big scale concurrency calls.
We will add @EnableAsync to ResourceServerConfig
and add new async method to demoController:
add refactor test case to support async call with mvcmock
The benefit of non-blocking API will benefit a lot for big scale concurrency calls.
We will add @EnableAsync to ResourceServerConfig
@Configuration@EnableAsync@EnableResourceServer
/*@EnableResourceServer enables a Spring Security filter that authenticates requests using an incoming OAuth2 token.*/
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
and add new async method to demoController:
@GetMapping("/async-hello") public DeferredResult<ResponseEntity<?>> helloAsync() { DeferredResult<ResponseEntity<?>> output = new DeferredResult<>(); ForkJoinPool.commonPool().submit(() -> { try { Thread.sleep(6000); } catch (InterruptedException e) { } output.setResult(ResponseEntity.ok(HELLOWORLD));
}); return output;
}
add refactor test case to support async call with mvcmock
@Testpublic void callHelloWithOAuth() throws Exception { callHelloWithOAuthTemplate(API_DEMO_HELLO, true);
} private void callHelloWithOAuthTemplate(String endpoint, boolean isSynced) throws Exception { String resultString = fetchOAuthToken();
JacksonJsonParser jsonParser = new JacksonJsonParser();
String token = jsonParser.parseMap(resultString).get(ACCESS_TOKEN_TEXT).toString(); var result = this.mvc.perform(get(endpoint) .header(AUTHORIZATION_TEXT, BEARER_TEXT + token)); if (!isSynced) { this.mvc
.perform(asyncDispatch(result.andReturn())) .andExpect(status().isOk()) .andExpect(content().string(HELLO_API_EXPECTED));
} else { result.andExpect(status().isOk()) .andExpect(content().string(HELLO_API_EXPECTED));
} } @Testpublic void callHelloAsyncWithOAuth() throws Exception { callHelloWithOAuthTemplate(API_DEMO_HELLO_ASYNC, false);
} private String fetchOAuthToken() throws Exception { ResultActions result = this.mvc.perform(post(OAUTH_END_POINT) .contentType(MediaType.MULTIPART_FORM_DATA) .params(getOAuthFormData()) .with(httpBasic(OAUTH_LZHENG_CLIENT, OAUTH_LZHENG_SECRET))) .andExpect(status().isOk()) .andExpect(content().string(containsString("access_token"))); return result.andReturn().getResponse().getContentAsString();
}
Comments
Post a Comment