Spring4异步不工作
我正在使用Spring4Web应用程序,遇到了@Async同步运行的问题。这是100%注释驱动的。没有XML文件 应用程序: 控制器:Spring4异步不工作,spring,asynchronous,Spring,Asynchronous,我正在使用Spring4Web应用程序,遇到了@Async同步运行的问题。这是100%注释驱动的。没有XML文件 应用程序: 控制器: package com.kc2112.app.controllers; import java.util.ArrayList; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import org.springframework.be
package com.kc2112.app.controllers;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyAsyncController {
@Autowired
MyStopWatch stopwatch;
public MySampleService mySampleService = new MySampleService();
@RequestMapping(value = "/go", produces = {MediaType.TEXT_HTML_VALUE}, method = RequestMethod.GET)
public String taskExecutor() throws InterruptedException, ExecutionException {
ArrayList<Future<Boolean>> asyncResults = new ArrayList();
for (int i = 0; i < 10; i++) {
asyncResults.add(mySampleService.callAsync(i));
}
return "time passed is " + stopwatch.getTime();
}
}
Component:
package com.kc2112.app.controllers;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
@Component
public class MySampleService {
@Async
public Future<Boolean> callAsync(int taskCall) throws InterruptedException {
System.out.println("starting thread" + taskCall);
for (int i = 0; i < 10; i++) {
System.out.println("thread " + taskCall + " count is " + i);
}
return new AsyncResult<Boolean>(true);
}
}
Stopwatch:
package com.kc2112.app.controllers;
import org.apache.commons.lang.time.StopWatch;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
@Service
@Qualifier("stopwatch")
public class MyStopWatch extends StopWatch {
public MyStopWatch(){
super();
this.start();
}
}
package com.kc2112.app.controllers;
导入java.util.ArrayList;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.Future;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.MediaType;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
@RestController
公共类MyAsyncController{
@自动连线
MyStopWatch秒表;
public MySampleService MySampleService=new MySampleService();
@RequestMapping(value=“/go”,products={MediaType.TEXT\u HTML\u value},method=RequestMethod.GET)
公共字符串taskExecutor()引发InterruptedException,ExecutionException{
ArrayList asyncResults=新建ArrayList();
对于(int i=0;i<10;i++){
add(mySampleService.callAsync(i));
}
return“time passed is”+stopwatch.getTime();
}
}
组成部分:
包com.kc2112.app.controllers;
导入java.util.concurrent.Future;
导入org.springframework.scheduling.annotation.Async;
导入org.springframework.scheduling.annotation.AsyncResult;
导入org.springframework.stereotype.Component;
@组成部分
公共类MySampleService{
@异步的
public Future callAsync(int taskCall)抛出InterruptedException{
System.out.println(“起始线程”+任务调用);
对于(int i=0;i<10;i++){
System.out.println(“线程”+任务调用+”计数为“+i”);
}
返回新的异步结果(true);
}
}
秒表:
包com.kc2112.app.controllers;
导入org.apache.commons.lang.time.StopWatch;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.stereotype.Component;
导入org.springframework.stereotype.Service;
@组成部分
@服务
@限定符(“秒表”)
公共类MyStopWatch扩展了秒表{
公共MyStopWatch(){
超级();
这个。start();
}
}
我尝试了很多方法,但它总是打印出明显不异步的结果
public MySampleService mySampleService = new MySampleService();
这就是你麻烦的原因。您必须将MySampleService自动连接到控制器中,而不是自己创建实例。这是Spring将组件包装到异步代理中的唯一方法,该代理将检测注释、拦截方法调用、创建任务并将其提交给执行者
这就是你麻烦的原因。您必须将MySampleService自动连接到控制器中,而不是自己创建实例。这是Spring将组件包装到异步代理中的唯一方法,该代理将检测注释、拦截方法调用、创建任务并将其提交给执行者。是!!!!读完这篇文章后: 我对我的应用程序类做了一些更改。关键问题似乎是设置核心池大小。这是一个有固定问题的行的类 下面是上面的spring文档:
package com.kc2112.app;
导入com.kc2112.app.controllers.MyStopWatch;
导入javax.annotation.Resource;
导入org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.core.task.TaskExecutor;
导入org.springframework.scheduling.annotation.AsyncConfigurer;
导入org.springframework.scheduling.annotation.EnableAsync;
导入org.springframework.scheduling.annotation.EnableScheduling;
导入org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
导入org.springframework.web.servlet.support.AbstractAnnotationConfigDispatchersServletInitializer;
@使能同步
@使能调度
@配置
公共类WebApp扩展AbstractAnnotationConfigDispatcherServletInitializer实现AsyncConfigurer{
@资源
@限定符(“秒表”)
公共MyStopWatch秒表;
公共WebApp(){
超级();
秒表=新的MyStopWatch();
}
@凌驾
受保护类[]getRootConfigClasses(){
返回新类[0];
}
@凌驾
受保护类[]getServletConfigClasses(){
返回新类[]{AppConfig.Class};
}
@凌驾
受保护的字符串[]getServletMappings(){
返回新字符串[]{”/“};
}
@凌驾
公共任务执行器getAsyncExecutor(){
ThreadPoolTaskExecutor te=新的ThreadPoolTaskExecutor();
te.setMaxPoolSize(25);
te.setThreadNamePrefix(“执行者-”);
te.setCorePoolSize(25);//这是一条临界线。。。
te.initialize();
返回te;
}
@凌驾
公共AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
返回null;
}
}
是!!!!读完这篇文章后:
我对我的应用程序类做了一些更改。关键问题似乎是设置核心池大小。这是一个有固定问题的行的类
下面是上面的spring文档:
package com.kc2112.app;
导入com.kc2112.app.controllers.MyStopWatch;
导入javax.annotation.Resource;
导入org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.core.task.TaskExecutor;
导入org.springframework.scheduling.annotation.AsyncConfigurer;
导入org.springframework.scheduling.annotation.EnableAsync;
导入org.springframework.scheduling.annotation.EnableScheduling;
导入org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
导入org.springframewo
package com.kc2112.app.controllers;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyAsyncController {
@Autowired
MyStopWatch stopwatch;
public MySampleService mySampleService = new MySampleService();
@RequestMapping(value = "/go", produces = {MediaType.TEXT_HTML_VALUE}, method = RequestMethod.GET)
public String taskExecutor() throws InterruptedException, ExecutionException {
ArrayList<Future<Boolean>> asyncResults = new ArrayList();
for (int i = 0; i < 10; i++) {
asyncResults.add(mySampleService.callAsync(i));
}
return "time passed is " + stopwatch.getTime();
}
}
Component:
package com.kc2112.app.controllers;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
@Component
public class MySampleService {
@Async
public Future<Boolean> callAsync(int taskCall) throws InterruptedException {
System.out.println("starting thread" + taskCall);
for (int i = 0; i < 10; i++) {
System.out.println("thread " + taskCall + " count is " + i);
}
return new AsyncResult<Boolean>(true);
}
}
Stopwatch:
package com.kc2112.app.controllers;
import org.apache.commons.lang.time.StopWatch;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
@Service
@Qualifier("stopwatch")
public class MyStopWatch extends StopWatch {
public MyStopWatch(){
super();
this.start();
}
}
public MySampleService mySampleService = new MySampleService();
package com.kc2112.app;
import com.kc2112.app.controllers.MyStopWatch;
import javax.annotation.Resource;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@EnableAsync
@EnableScheduling
@Configuration
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer implements AsyncConfigurer {
@Resource
@Qualifier("stopwatch")
public MyStopWatch stopwatch;
public WebApp() {
super();
stopwatch = new MyStopWatch();
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{AppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
public TaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor();
te.setMaxPoolSize(25);
te.setThreadNamePrefix("LULExecutor-");
te.setCorePoolSize(25); //This was the critical line...
te.initialize();
return te;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}