zoukankan      html  css  js  c++  java
  • 使用Spring AsyncRestTemplate对象进行异步请求调用

    直接上代码:

    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 "异步调用结束";
        }
    
    
    }

    同步请求:

    异步请求:

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    ORA-01536: 超出表空间 'tablespace_name' 的空间限额
    Oracle数据库表索引失效,解决办法:修改Oracle数据库优化器模式
    解决response.setHeader("Content-disposition" 中文乱码问题
    MyBatis配置:在控制台打印SQL语句
    JS 字符串转日期格式 日期格式化字符串
    远程桌面管理工具Remote Desktop Connection Manager
    调整Windows操作系统下时间同步的频率
    Oracle数据库中字符型字段按数字排序
    “Error:(1, 1) java: 非法字符: 'ufeff'”错误解决办法
    本次孩子流感总结
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/5737991.html
Copyright © 2011-2022 走看看