消息成功失败回调demo

/***/
 @Service
public class MessageTask {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageTask.class);

private static AsyncRestTemplate restTemplate = new AsyncRestTemplate();

static {
restTemplate.setMessageConverters(Arrays.asList(new FormHttpMessageConverter(), new StringHttpMessageConverter()));
}

@Scheduled(fixedDelay = 2000)
public void send() {
HttpEntity<MultiValueMap<String, Object>> request = MessageUtils.getMessage();
if (request == null) {
return;
}
final String finalMsg = (String) request.getBody().get("content").get(0);
final String userId = (String) request.getBody().get("userId").get(0);
try {
// ListenableFuture<ResponseEntity<String>> future = restTemplate.postForEntity("http://xxx.com/api/mail.aspx?name=newMail", request, String.class);
ListenableFuture<ResponseEntity<String>> future = restTemplate.postForEntity("http://xxx.com/api/mail.aspx?name=newMail", request, String.class);
future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
@Override
public void onSuccess(ResponseEntity<String> result) {
LOGGER.debug(result.getBody());
}

@Override
public void onFailure(Throwable t) {
LOGGER.error("userId:" + userId + ",msg:" + finalMsg + ",exception:" + t.getMessage(), t);
}
});
} catch (Exception e) {
LOGGER.error("userId:" + userId + ",msg:" + finalMsg, e);
}
}

}
posted @ 2017-02-09 12:34  托马斯布莱克  阅读(423)  评论(0编辑  收藏  举报