gpt4 book ai didi

java - Spring 异步组件不工作

转载 作者:行者123 更新时间:2023-12-01 11:36:46 25 4
gpt4 key购买 nike

我上周五实现了这一目标,使其异步工作,但我不能再这样做了,我快疯了!!。

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

    <?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配置有关,或者是异步组件的注释。但问题是,当调用异步方法时,不会被其他线程执行。

请问有人看到这里有什么奇怪的吗?

问候。

最佳答案

尝试明确设置池大小:

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

关于java - Spring 异步组件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29894200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com