Мне нужно время от времени отправлять уведомления, я выполняю эту задачу асинхронно. Я использую 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: Причина добавления сыщика в теги заключается в том, что выполнение этого в новом потоке не распространяет заголовки (багаж-*), поэтому попробуйте это, надеясь, что команда Hystrix добьется цели.
TraceRunnable
? - person Marcin Grzejszczak   schedule 14.06.2017I had the entire method wrapped in a runnable and was calling start()
‹-- Я имею в виду это. - person Marcin Grzejszczak   schedule 14.06.2017