Spring AsyncRestTemplate
类说明
类图
类图中省略了一些参数类型及重载的方法,在不影响理解的情况下,保证各要素在一幅图中展现。

private String result = "";
@Test
public void testAsyncPost() throws Exception {
String posturl = "http://xxxxxx";
String params = "xxxxxx";
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
HttpEntity<Object> hpEntity = new HttpEntity<Object>(params, headers);
AsyncRestTemplate asyncRt = new AsyncRestTemplate();
ListenableFuture<ResponseEntity<String>> future = asyncRt.postForEntity(posturl, hpEntity, String.class);
future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
public void onSuccess(ResponseEntity<String> resp) {
result = resp.getBody();
}
public void onFailure(Throwable t) {
System.out.println(t.getMessage());
}
});
System.out.println(result);
}
package com.mlxs.common.server.asyncrest;
import org.apache.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.RestTemplate;
/**
* Asyncrest: AsyncRestTemplate 异步发生请求测试
*
* @author mlxs
* @since 2016/8/4
*/
@Controller
@RequestMapping("/async")
public class AsyncrestController {
Logger logger = Logger.getLogger(AsyncrestController.class);
@RequestMapping("/fivetime")
@ResponseBody
public String tenTime(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "5秒...";
}
/**
* 同步调用
* @return
*/
@RequestMapping("/sync")
@ResponseBody
public String sync(){
//同步调用
RestTemplate template = new RestTemplate();
String url = "http://localhost:8080/async/fivetime";//休眠5秒的服务
String forObject = template.getForObject(url, String.class);
return "同步调用结束, 结果:" + forObject;
}
/**
* 异步调用
* @return
*/
@RequestMapping("/async")
@ResponseBody
public String async(){
AsyncRestTemplate template = new AsyncRestTemplate();
String url = "http://localhost:8080/async/fivetime";//休眠5秒的服务
//调用完后立即返回(没有阻塞)
ListenableFuture<ResponseEntity<String>> forEntity = template.getForEntity(url, String.class);
//异步调用后的回调函数
forEntity.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
//调用失败
@Override
public void onFailure(Throwable ex) {
logger.error("=====rest response faliure======");
}
//调用成功
@Override
public void onSuccess(ResponseEntity<String> result) {
logger.info("--->async rest response success----, result = "+result.getBody());
}
});
return "异步调用结束";
}
}
同步请求:
异步请求:
Spring AsyncRestTemplate的更多相关文章
- 使用Spring AsyncRestTemplate对象进行异步请求调用
直接上代码: package com.mlxs.common.server.asyncrest; import org.apache.log4j.Logger; import org.springfr ...
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
{ "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...
- Spring4.1新特性——Spring MVC增强
目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ...
- 【Spring-web】AsyncRestTemplate源码学习
2017-01-23 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6343347.html 本篇概要 类说明 类图 简单例子 精辟的内部类 类 ...
- Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)
很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...
- Spring官方文档翻译
随笔:有人曾这样评价spring,说它是Java语言的一个巅峰之作,称呼它为Java之美,今天,小编就领大家一起来领略一下spring之美! Spring官方文档:http://docs.spring ...
- 撸一撸Spring Cloud Ribbon的原理
说起负载均衡一般都会想到服务端的负载均衡,常用产品包括LBS硬件或云服务.Nginx等,都是耳熟能详的产品. 而Spring Cloud提供了让服务调用端具备负载均衡能力的Ribbon,通过和Eure ...
- 转:Spring历史版本变迁和如今的生态帝国
Spring历史版本变迁和如今的生态帝国 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/bntX2jSQfEHy7/article/deta ...
- Spring官方文档翻译(1~6章)
Spring官方文档翻译(1~6章) 转载至 http://blog.csdn.net/tangtong1/article/details/51326887 Spring官方文档.参考中文文档 一.S ...
随机推荐
- 如何判断事务是否完成,SqlTransaction
SqlConnection sconn = null; SqlCommand scmd = null; SqlTransaction strans = null; try { string sqlIn ...
- gdb 调试(查看运行时数据) 四
在使用GDB调试程序时,触发断点后,可以使用print命令(简写为p),或是同义命令inspect来查看当前程序的运行数据.print命令的格式是: print <expr> pri ...
- android 开源项目列表【持续整理中。。。】
Android完整的开源项目,不包括各种组件的项目 社区客户端 oschina客户端:oschina网站的客户端,wp版,iOS版都有开源,一个社区型客户端,包括登录刷新各类视线 四次元新浪微博客户端 ...
- asp.net web api 授权功能
1.重写授权方法 using System; using System.Collections.Generic; using System.Linq; using System.Net; using ...
- 胡乱摸的NOIP2017游记和总结
来自YZK的总结 本篇总结主要分成两部分:NOI Professional游记和平日的刷题训练. 今年的NOI Professional TG的难度在洛谷上标记为:二黄一绿三紫.恭喜NOIP今年全面脱 ...
- C/C++基础----顺序容器
通常没有特别的原因,用vector. list和forward_list有额外的内存开销,如果有很多小元素,不要使用. 如果只在读取输入时需要在容器中间位置插入元素,随后需要随机访问. 1确定是否真正 ...
- NB-IOT/LoRa/Zigbee无线组网方案对比
物联网设备节点组网存在2种组网方式, 无线组网和有线组网. 无线组网我们常见到的有Zigbee,LoRa, NB-IOT等,其中Lora/NB-IOT属于LPWAN技术,LPWAN技术有覆盖广.连接多 ...
- select count(*) as total from(select count(*) from tab_cb_casim group by `card_no`) as cai;
子查询必须加一个别名才能执行!!
- Oracle创建数据库链接
**********创建数据库链接******************create public database link link_gzzl connect to system identifie ...
- javascript创建对象之工厂模式(一)
工厂模式在软件工程里面是一种比较常见的设计模式了.这种模式抽象了创建具体对象的过程. 上代码: function createHuman(name,sex) { var obj = new Objec ...