Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@异步在spring中不起作用_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

@异步在spring中不起作用

@异步在spring中不起作用,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我有一个spring boot应用程序,希望通过使用@Async注释来运行一个简单的异步后台任务,但是,这似乎不起作用(没有创建新线程,我在每次调用函数后等待睡眠结束)。。 *****我的配置类:** @Configuration @EnableAsync public class AsyncConfig { } ****我的控制器:** @GetMapping(value = "/testAsync") @ResponseBody public String testAs

我有一个spring boot应用程序,希望通过使用@Async注释来运行一个简单的异步后台任务,但是,这似乎不起作用(没有创建新线程,我在每次调用函数后等待睡眠结束)。。 *****我的配置类:**

@Configuration
@EnableAsync
public class AsyncConfig {

}
****我的控制器:**

 @GetMapping(value = "/testAsync")
    @ResponseBody
    public String testAsync() {

        TestClass testClass = new TestClass();
        System.out.println("begin of test async dates is : " + new Date());

        for (int i = 0; i < 5; i++) {

            testClass.asyncMethod();
        }
        System.out.println("end of test async  dates is : " + new Date());

        return "end!";
    }
****运行应用程序后的输出:**

begin of test async dates is : Sat Jul 14 19:35:43 EET 2018
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
2018-07-14 19:36:22.299  INFO 3120 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats    : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
end of test async  dates is : Sat Jul 14 19:36:33 EET 2018
****注:** -我尝试过用@Component注释TestClass,但它不起作用 -我尝试在应用程序级别和方法级别添加执行器,但得到的结果相同


请帮帮我,我已经花了5个小时在这上面,但运气不好

将TestClass自动关联到控制器中,使用TestClass类型创建控制器的实例成员,并为其提供Autowire注释


当您自己实例化它时,Spring不知道异步注释,对它的调用将在调用线程中运行。

非常感谢,它工作得很疯狂!感谢您无数次!
begin of test async dates is : Sat Jul 14 19:35:43 EET 2018
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
2018-07-14 19:36:22.299  INFO 3120 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats    : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
end of test async  dates is : Sat Jul 14 19:36:33 EET 2018