Spring异步组件无法正常工作

时间:2015-04-27 11:15:17

标签: java spring

我上周五实现了这个目的,让它工作异步,但我不能了,我疯了!!。

这是我当前的应用程序上下文配置。

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:task="http://www.springframework.org/schema/task"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/task
   http://www.springframework.org/schema/task/spring-task-3.0.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-3.1.xsd">

        <context:component-scan base-package="com.greenvalley.etendering">
        <context:exclude-filter type="annotation" expression="com.greenvalley.etendering.annotation.NoAutoScan" />
    </context:component-scan>

    <task:annotation-driven/>

然后我有我的组件。

       @Component
       @Transactional
       @Configuration
       @EnableAsync
       public class AsyncDocumentGenerationImpl implements            AsyncDocumentGeneration {

private static final Logger LOGGER = LoggerFactory.getLogger(AsyncDocumentGenerationImpl.class);


private static final SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");

private final String mimeType = "application/pdf";
private final FileType fileType = FileType.PDF;

@Inject
private DocumentGenerationService documentGenerationService;

@Inject
private DocumentService documentService;


@Async
public Future<Integer> generateDocumentPerCandidate(final PublicProcurement publicProcurement, final DocumentType documentType, final Candidacy candidacy, final Assessment assessment)
        throws FileFormatException, DocumentServiceException, IOException, JAXBException, CmisDocumentException {

    return new AsyncResult<>(FeedbackActions.SUCCESS.getCode());
}

}

该组件是从其他服务调用的。

      private DeferredResult<Integer> generateDocumentsPerCandidates(DocumentGroup documentGroup, DocumentType documentType, PublicProcurement publicProcurement)
        throws FileFormatException, DocumentServiceException, IOException, JAXBException, CmisDocumentException {
    List<Future<Integer>> candidaciesResults = new ArrayList<>();
    for (Candidacy candidacy : publicProcurement.getCandidacies()) {
            Future<Integer> candidacyResult = asyncDocumentGeneration.generateDocumentPerCandidate(publicProcurement,
                    documentType, candidacy, documentGroup.getAssessment());
            candidaciesResults.add(candidacyResult);

    }
    DeferredResult<Integer> future = new DeferredResult<>();
    asyncDocumentGeneration.manageResults(candidaciesResults, future);
    return future;
}

我认为必须与applicationContext配置或async组件的注释相关。但是,当调用异步方法时,事情不是由其他线程执行的。

请有人在这里看到一些奇怪的东西吗?。

问候。

1 个答案:

答案 0 :(得分:1)

尝试设置明确的池大小:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>