Java @spring中的异步行为是同步的

Java @spring中的异步行为是同步的,java,spring,spring-mvc,asynchronous,Java,Spring,Spring Mvc,Asynchronous,我已经编写了一段代码,用于检查Spring框架中的@Async注释行为 @RequestMapping( value="/async" , method = RequestMethod.GET) public ModelAndView AsyncCall(HttpServletRequest request) { async1(); async2(); return new ModelAndView("firstpage"); } @As

我已经编写了一段代码,用于检查Spring框架中的
@Async
注释行为

@RequestMapping( value="/async" , method = RequestMethod.GET)
  public ModelAndView AsyncCall(HttpServletRequest request)
  {
        async1();
        async2();
    return new ModelAndView("firstpage");
  }

  @Async
  private void async1(){
      System.out.println("Thread 1 enter");
      try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      System.out.println("Thread 1 exit");
  }

  @Async
  private void async2(){
      System.out.println("Thread 2 enter");
      try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      System.out.println("Thread 2 exit");
  }
此代码的输出如下所示

Thread 1 enter
Thread 1 exit
Thread 2 enter 
Thread 2 exit
通过查看此输出,这两个
@Async
函数调用本身似乎是同步的

到目前为止,我知道这两个线程是不同的,应该自己异步运行

根据正在打印的spring代理调用日志更改代码后,如下所示

主线程名称:http-apr-8080-exec-8

Thread 1 enter
Async-1 Thread Name: neutrinoThreadPoolExecutor-1
Thread 1 exit
Thread 2 enter
Async-2 Thread Name: neutrinoThreadPoolExecutor-1
Thread 2 exit

两个异步调用的线程名称相同,但似乎并没有显示异步行为

您在Spring应用程序中启用了异步吗?在SpringBoot中,您可以执行以下操作

@SpringBootApplication
@EnableAsync
public class Application extends AsyncConfigurerSupport {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("GithubLookup-");
        executor.initialize();
        return executor;
    }

}

@EnableAsync注释将启用Spring在后台线程池中运行@Async方法的功能。

您必须从当前类外部调用这些方法。否则,春天的魔法将无法执行

因此,请尝试以下方法:

@Inject
MyAsyncService asyncService;

@RequestMapping( value="/async" , method = RequestMethod.GET)
public ModelAndView AsyncCall(HttpServletRequest request) {
    asyncService.async1();
    return new ModelAndView("firstpage");
}
MyAsyncService

@Component
public MyAsyncService {
    @Async
    public void async1() {
         //code
    } 
}

在这种情况下,
@Async
对我不起作用

  • 缺少
    @EnableAsync
  • @Async
    方法不是公共的
  • @Async
    注释的方法是从同一类的另一个方法调用的。可能绕过异步代理代码,只调用普通方法

  • Spring的
    @Async
    注释有两条规则需要遵循

  • 它只能应用于
    public
    方法
  • 自调用——从同一个类中调用异步方法——将不起作用
  • 原因很简单——该方法需要是公共的,以便可以代理。
    自调用不起作用,因为它绕过了代理,直接调用底层方法。

    添加启用异步的配置

    @Configuration
    @EnableAsync
    public class AsyncWorkerConfig {
    
        @Bean(name = "abcExecutors")
        public ExecutorService ch3Executors() {
            int corePoolSize = 6;
            int maxPoolSize = 6;
            int keepAliveTime = 100000;
    
            return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(),
                    new ThreadFactoryBuilder()
                            .setDaemon(false)
                            .setNameFormat("abc-parser-t-%d").build());
        }
    
    }
    

    代理建议不适用于自调用…我已使用spring代理建议更改了代码。是否有任何选项可以通过web.xml启用它?方法可以是包私有的
     @Service
    public class AsyncFileProcessor {
    
        @Async("abcExecutors")
        public void process(final String filePath){
          //your code logic
      }
    
    }