如何在Spring4.x中使用@Async注释启动长时间运行的进程

如何在Spring4.x中使用@Async注释启动长时间运行的进程,spring,spring-annotations,Spring,Spring Annotations,我正在开发我的第一个Spring(4.x)服务,它使用WatchService.take()等待文件系统的更改。因此,这些服务的运行时间可能相当长。因此,我添加了包含WatchService.take()和@Async的方法 @Service public class ReceiveService() { @Async protected void startService() { LOG.info("Started service ReceiveService");

我正在开发我的第一个Spring(4.x)服务,它使用WatchService.take()等待文件系统的更改。因此,这些服务的运行时间可能相当长。因此,我添加了包含WatchService.take()和@Async的方法

@Service
public class ReceiveService() {

   @Async
    protected void startService() {
      LOG.info("Started service ReceiveService");
      ...
      WatchKey watchKey  = WatchService.take()
      ...
   }
}
该服务是从另一个具有@enablesync注释的组件的后构造中启动的

@Component
@EnableAsync
public class receiveServiceController{

    @Autowired
    private ReceiveService receiveService;


    @PostConstruct
    public void Init() {
        LOG.info("Starting service ReceiveService");
        ReceiveService.startService();
    }
}
我浏览了不少博客和文章,但找不到问题所在。部署应用程序时,会记录两个日志条目,但应用程序上下文的进一步加载似乎会暂停。我希望对ReceiveService.startService()的调用不是异步调用的,因此我犯了一些错误

==更新1==

我创建了一个@Configuration类作为

public class ReceiveServiceConfiguration {

    @Bean(name = "ReceiveServiceExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setMaxPoolSize(1);
        executor.setQueueCapacity(1);
        executor.setThreadNamePrefix("ReceiveService-");
        executor.initialize();
        return executor;
    }
}
已从receiveServiceController中删除@EnableAsync注释。并将startService方法的@Async注释替换为@Async(“ReceiveServiceExecutor”) 部署它时,再次调用ReceiveService.startService()方法时,初始化似乎挂起。

有两件事出错

receiveServiceController中ReceiveService的第一个自动连接应该自动连接接口,而不是实现


第二,@Async,虽然在中没有明确的文档记录,但只能应用于公共方法。因此,必须将
受保护的void startService()
更改为
公共void startService()

@EnableAsync
@组件上不做任何事情,它应该在
@配置
类上。此外,没有Spring 2.2,因此不确定您实际使用的是哪个版本(3.2?).根据Mvn,使用弹簧4.1.4。最初,我的版本基于外部库中的版本编号