zoukankan      html  css  js  c++  java
  • Springmvc 异步处理

    package com.lookcoder.haircutmember.controller.login.page.async;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.concurrent.*;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.context.request.async.DeferredResult;
    import org.springframework.web.context.request.async.WebAsyncTask;
    
    import java.util.concurrent.*;
    
    @RequestMapping("/async/")
    @Controller
    public class AsyncController {
    
        private static Logger log = LoggerFactory.getLogger(AsyncController.class);
        private static ExecutorService executor = Executors.newSingleThreadExecutor();
    
    
        @ResponseBody
        @RequestMapping("listenableFuture")
        public ListenableFuture<String> testdelistenableFuture() {
            log.info("in main thread.");
    //        Runnable r = () -> {
    //            for (int i = 0; i < 10; i++) {
    //                log.info("sub thread run.... " + (i + 1));
    //            }
    //            try {
    //                Thread.sleep(5 * 1000);
    //            } catch (InterruptedException e) {
    //                e.printStackTrace();
    //            }
    //        };
            Callable<String> r = () -> {
                int j = 0;
                for (int i = 0; i < 10; i++) {
                    j += i;
                    log.info("sub thread run.... " + (i + 1));
                }
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return String.valueOf(j);
            };
            ListenableFutureTask lf = new ListenableFutureTask(r);
    
            lf.addCallback(new ListenableFutureCallback() {
                @Override
                public void onFailure(Throwable ex) {
                    log.info("调用失败!");
                }
    
                @Override
                public void onSuccess(Object result) {
                    log.info("调用成功!" + result);
                }
            });
    
            executor.submit(lf);
    
            return lf;
        }
    
        @ResponseBody
        @RequestMapping("deferred")
        public DeferredResult<String> testdeferred() {
            DeferredResult<String> dr = new DeferredResult<>();
            executor.submit(() -> {
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int j = 0;
                for (int i = 0; i < 10; i++) {
                    log.info("children thread is run ...");
                    j+= i;
                }
                log.info("get value");
                boolean b = dr.setResult(String.valueOf(j));
                log.info("set value is ok!" + b);
            });
            return dr;
        }
    
        @ResponseBody
        @RequestMapping("runnable")
        public String testRunnable() {
            log.info("in main thread!");
            log.info("create sub thread!");
            Runnable c = () -> {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i;
                for (i = 0; i < 10; i++) {
                    log.info("sub thread task run...." + (i + 1));
                }
            };
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.submit(c);
            log.info("again in main thread!");
            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 10; i++) {
                log.info("main thread task run...." + (i + 1));
            }
            log.info("main thread is complete!");
            return "main thread return!";
        }
    
        @ResponseBody
        @RequestMapping("callable")
        public Callable<Integer> testCallable() {
            log.info("in main thread!");
            log.info("create sub thread!");
            Callable<Integer> c = () -> {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i;
                for (i = 0; i < 10; i++) {
                    log.info("sub thread task run...." + (i + 1));
                }
                return i;
            };
            log.info("again in main thread!");
            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 10; i++) {
                log.info("main thread task run...." + (i + 1));
            }
            log.info("main thread is complete!");
            return c;
        }
    
        @ResponseBody
        @RequestMapping("webAsyncTask")
        public WebAsyncTask<String> testWebAsyncTask() {
            log.info("in main thread!");
            log.info("create sub thread!");
            WebAsyncTask wat = new WebAsyncTask(() -> {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 10; i++) {
                    log.info("sub thread task run...." + (i + 1));
                }
                return "ok";
            });
            log.info("again in main thread!");
            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 10; i++) {
                log.info("main thread task run...." + (i + 1));
            }
            log.info("main thread is complete!");
            return wat;
        }
    }
  • 相关阅读:
    从请假日期列表中取得请假起止日期
    存储过程编写经验和优化措施
    安装IE8不能调试VS2003的解决办法
    javascript驗證若干DropDownList是否有選择
    欢迎光临C/S框架网 www.csframework.com
    C#开发框架钢铁贸易进销存系统演示视频
    基于.Net C/S结构系统开发框架V2.2正式发布!
    C#.NET Winform+WebService开发框架完整版本
    C#.Net C/S快速开发框架V2.2版本介绍
    专注C# .Net C/S结构系统开发框架,C/S框架网
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/11896695.html
Copyright © 2011-2022 走看看