我需要不时发送通知,我异步执行此任务。我正在使用 HystrixCommand 如下执行异步 RestTemplate 调用,但该调用不起作用:
@HystrixCommand
public Future<String> notify(final Query query) {
return new AsyncResult<String>() {
@Override
public String invoke() {
String result = null;
try {
ResponseEntity<HashMap> restExchange = restTemplate.exchange(url,
HttpMethod.POST,
new HttpEntity<String>(mapper.writeValueAsString(queryMap), httpHeaders),
HashMap.class);
LOGGER.info("Response code from " + url + " = " + restExchange.getStatusCodeValue());
result = ""+ restExchange.getStatusCodeValue();
} catch(Exception e) {
LOGGER.error("Exception while sending notification! Message = " + e.getMessage(), e);
}
return result;
}
};
}
这是我之前尝试做的(也没有工作):
@HystrixCommand
public String notify(final Query query) {
new Thread(new Runnable() {
@Override
public void run() {
try {
ResponseEntity<HashMap> restExchange = restTemplate.exchange(url, HttpMethod.POST,
new HttpEntity<String>(mapper.writeValueAsString(queryMap), httpHeaders), HashMap.class);
LOGGER.info("Response code from " + url + " = " + restExchange.getStatusCodeValue());
} catch (Exception e) {
LOGGER.error("Exception while sending notification! Message = " + e.getMessage(), e);
}
}
}).start();
}
PS:将侦探添加到标签的原因是,在新线程中执行此操作不会传播标头(baggage-*),因此尝试此操作希望 Hystrix 命令可以解决问题