Springmvc 异步处理
阅读原文时间:2023年07月10日阅读:1

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 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;  
}  

}