How to create independent thread using spring @Async

1.3k views Asked by At

I am using spring @Async with weblogic workmanager, Spring version is 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 configuration 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 request is coming from fornt end , controller should intiate job by calling service method and it should returns response with out waiting for service job.

When @Async is not applied everything goes perfectly but when I applied, service thread is getting killed because of my request thread is done.

Please let me know, Using @Async how to create a thread independent of request thread.

That independent thread may run for 2 minutes or 20 minutes depends on input.

1

There are 1 answers

0
maaw On

Firstly, Spring's docs state that @Async with an other than default executor should be annotated with @Async("otherExecutor") rather than @Async(value="otherExecutor").

Also, I don't think I quite understand what you mean by this:

When @Async is not applied everything goes perfectly but when I applied, service thread is getting killed because of my request thread is done.

I'll asume you mean that it runs ok but on the same thread. In that case, it makes perfect sense since you're running that method synchronously.