How to create an independent thread using spring @Async
I am using spring @Async with weblogic workmanager, Spring version 3.2.2
Web.xml:
<resource-ref>
<res-ref-name>ReportWorkmanager</res-ref-name>
<res-type>commonj.work.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
weblogic.xml:
<work-manager>
<name>ReportWorkmanager</name>
<fair-share-request-class>
<name>Priority</name>
<fair-share>100</fair-share>
</fair-share-request-class>
<min-threads-constraint>
<name>MinThreadCount</name>
<count>15</count>
</min-threads-constraint>
<max-threads-constraint>
<name>MaxThreadCount</name>
<count>25</count>
</max-threads-constraint>
<work-manager-shutdown-trigger>
<max-stuck-thread-time>120</max-stuck-thread-time>
<stuck-thread-count>25</stuck-thread-count>
</work-manager-shutdown-trigger>
</work-manager>
Spring Config xml:
<bean id="reportWorkManagerTaskExecutor"
class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
<property name="workManagerName" value="java:comp/env/ReportWorkmanager" />
</bean>
Controller:
@RequestMapping(value = "/publish", method = RequestMethod.POST)
public MarketingScenario publishReport(@RequestBody final PublishReportDTO reportDTO, final HttpServletRequest request) throws Exception {
reportDTO.setReqUserName(DominoWebUtil.getLoggedInUserName(request));
reportEngine.publishReport(reportDTO);
MarketingScenario scenario = scenarioService.findMarketingScenarioById(reportDTO.getScenarioId());
_LOGGER.debug("Job submitted , successfully came out for scenario id {}", reportDTO.getScenarioId());
return scenario;
}
Service:
@Async(value = "reportWorkManagerTaskExecutor")
public void publishReport(final PublishReportDTO reportDTO) {
//Some long run job
}
Whenever a request comes in from the end of the fornt, the controller must inject the job by calling the service method, and it must return a response without waiting for the service job.
When @Async is not applied everything goes fine, but when I apply the service thread gets killed due to my request thread.
Please let me know, using @Async, how to create a flow independent of the request flow.
The independent stream can run for 2 minutes or 20 minutes, depending on the input.
source to share
First, the Spring docs state that @Async
with a different than the default executor, it should be annotated with @Async("otherExecutor")
, not @Async(value="otherExecutor")
.
Also, I don't think I fully understand what you mean by this:
When @Async is not applied everything goes fine, but when I apply the service thread gets killed due to my request thread.
I want you to mean that it works fine, but in the same thread. It makes sense in this case, since you are using this method synchronously.
source to share