RestTemplate示例如下.
public class SimpleClient {
private final String URL;
private AsyncRestTemplate rest = new AsyncRestTemplate(new Netty4ClientHttpRequestFactory());
private RestTemplate restTemplate = new RestTemplate(new Netty4ClientHttpRequestFactory());
public SimpleClient(String url) {
this.URL = url;
Netty4ClientHttpRequestFactory nettyFactory = new Netty4ClientHttpRequestFactory();
try {
nettyFactory.setSslContext(SslContextBuilder.forClient().build());
} catch (SSLException e) {
e.printStackTrace();
}
rest = new AsyncRestTemplate(nettyFactory);
}
@Override
public ResponseEntity<ResponseData> doSendByPOST(RequestData data,Class<ResponseData> clazz) {
List<HttpMessageConverter<?>> messageConvertors = new ArrayList<>();
messageConvertors.add(new MappingJackson2HttpMessageConverter());
rest.setMessageConverters(messageConvertors);
restTemplate.setMessageConverters(messageConvertors);
HttpHeaders headers = new HttpHeaders();
ObjectMapper objectMapper = new ObjectMapper();
StringWriter writer = new …Run Code Online (Sandbox Code Playgroud) 我想同时执行3个调用,并在完成所有操作后处理结果.
我知道这可以使用AsyncRestTemplate实现,因为这里提到如何使用AsyncRestTemplate同时进行多个调用?
但是,不推荐使用AsyncRestTemplate而使用WebClient.我必须在项目中使用Spring MVC,但感兴趣的是我是否可以使用WebClient来执行同时调用.有人可以建议如何使用WebClient正确完成此操作吗?
我写了一个处理列表列表的 spring 批处理作业。
Reader 返回列表列表。Processor 处理每个 ListItem 并返回处理过的 List。Writer 从 List of List 向 DB 和 sftp 写入内容。
我有一个用例,我从 Spring 批处理器调用 Async REST api。在 ListenableFuture 响应中,我实现了 LitenableFutureCallback 来处理成功和失败,这按预期工作,但在异步调用返回某些内容之前,ItemProcessor 不会等待来自异步 API 的回调并将对象(列表)返回给编写器。
我不确定如何实现和处理来自 ItemProcessor 的异步调用。
我确实读过 AsyncItemProcessor 和 AsyncItemWriter,但我不确定在这种情况下是否应该使用它。
我还想过在 AsyncRestTemplate 的 ListenableFuture 响应上调用 get(),但根据文档,它会阻塞当前线程,直到它收到响应。
我正在寻求有关如何实现这一点的帮助。下面的代码片段:
处理器:
public class MailDocumentProcessor implements ItemProcessor<List<MailingDocsEntity>, List<MailingDocsEntity>> {
... Initialization code
@Override
public List<MailingDocsEntity> process(List<MailingDocsEntity> documentsList) throws Exception {
logger.info("Entering MailingDocsEntity processor");
List<MailingDocsEntity> synchronizedList = Collections.synchronizedList(documentsList);
for (MailingDocsEntity mailingDocsEntity : synchronizedList) {
System.out.println("Reading Mailing id: …Run Code Online (Sandbox Code Playgroud) 我必须使用RestTemplate多次使用不同的参数进行Rest API调用.API是相同的,但它是正在改变的参数.次数也是可变的.我想使用AsyncRestTemplate但我的主线程应该等到所有API调用都成功完成.我还想处理每个API调用返回的响应.目前我正在使用RestTemplate.基本形式如下.
List<String> listOfResponses = new ArrayList<String>();
for (Integer studentId : studentIdsList) {
String respBody;
try {
ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, requestEntity, String.class);
} catch (Exception ex) {
throw new ApplicationException("Exception while making Rest call.", ex);
}
respBody = requestEntity.getBody();
listOfResponses.add(respBody);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何实现AsyncRestTemplate?
我正在使用Spring 4.2.3 AsyncRestTemplate.exchange()来调用一些需要几秒钟的API,我期望listenableFuture.get(1,TimeUnit.SECONDS)将阻塞1秒然后抛出TimeOutException.
相反的是,listenableFuture.get()将在api调用的整个时间内阻塞(超过1秒)
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
ListenableFuture<ResponseEntity<String>> listenableFuture = restTemplate.exchange(URL, HttpMethod.GET, null, String.class);
log.debug("before callback");
//...add callbacks
try{
log.debug("before blocking");
listenableFuture.get(1, TimeUnit.SECONDS);
}catch (InterruptedException e) {
log.error(":GOT InterruptedException");
} catch (ExecutionException e) {
log.error(":GOT ExecutionException");
} catch (TimeoutException e) {
log.info(":GOT TimeoutException");
}
log.info("FINISHED");
Run Code Online (Sandbox Code Playgroud)
输出:
09:15:21.596 DEBUG [main] org.springframework.web.client.AsyncRestTemplate:78 - Created asynchronous GET request for "http://localhost:4567/oia/wait?seconds=5"
09:15:21.666 DEBUG [main] org.springframework.web.client.RestTemplate:720 - Setting request Accept header to [text/plain, application/xml, text/xml, application/json, application/*+xml, application/*+json, */*]
09:15:21.679 DEBUG [main] com.zazma.flow.utils.FutureTest:74 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring RestTemplate 并想调用另一个不返回任何响应正文的服务。所以,我不想等待回应。所以,这只是一劳永逸,然后继续剩下的代码。我正在考虑创建一个新线程来执行此操作,但真的不确定什么是正确的方法。
spring ×6
java ×5
future ×2
resttemplate ×2
concurrency ×1
netty ×1
rest ×1
spring-batch ×1
spring-boot ×1