创建多个线程后无法处理请求

时间:2019-02-07 01:44:57

标签: java spring spring-boot threadpool

创建一个静态ExecutorService对象,并使用它在spring boot rest控制器中使用输入执行异步任务。调用异步任务3次后,无法处理任何请求。

TaskManager.java

 public class AsyncTaskWrapper implements Callable {
    Object input,output;
    Callable callback;
    AsyncTask task;
    public Object getInput() {
        return input;
    }

    public void setInput(Object input) {
        this.input = input;
    }

    public Object getOutput() {
        return output;
    }

    public AsyncTask getTask() {
        return task;
    }

    public void setTask(AsyncTask task) {
        this.task = task;
    }

    public Callable getCallback() {
        return callback;
    }

    public void setCallback(Callable callback) {
        this.callback = callback;
    }

    public AsyncTaskWrapper(AsyncTask task, Callable callback){
        this.callback = callback;
        this.task = task;
    }

    @Override
    public Object call() throws Exception {
        System.out.println("Started");
        output = task.execute(input);
        callback.call();
        System.out.println("call completed");
        return output;
    }
}

AsyncTaskWrapper task = new AsyncTaskWrapper(
    (list) -> {
        List l = (List) list;
        try {
                MailingUtils.sendRegistrationMail((String) l.get(0), (String) l.get(1));
        } catch (MessagingException e) {
                e.printStackTrace();
        }
        return null;
    },
    () -> {
        System.out.println("Done");
        return null;
    });

task.setInput(objects);
TaskManager.addAsyncTask(task);

RESTControllers.java -需要与RESTEndpoint输入异步执行任务。

test

预先感谢

0 个答案:

没有答案