Search code examples
javaspringweb-servicesasynchronousspring-ws

Spring Async method not working with EndPoint


My Async implementation is not working though it seems I have done everything correctly.

I am calling my asynchronous method from my EndPoint class.

Please have a look at my code below and assist me in resolving the problem.

My EndPoint class is as follows:

@Endpoint
public class EndpointDummy {

    private static final String TARGET_NAMESPACE = "http://www.abc.xyz.com/GetAcctInfo";

    @Autowired
    private DummyService service;

    @PayloadRoot(localPart = "GetAcctInfoRq", namespace = TARGET_NAMESPACE)
    public @ResponsePayload GetAcctInfoRs handleRequest(@RequestPayload GetAcctInfoRq request, MessageContext messageContext) throws JAXBException, TransformerException {

        /*****************************************************************
        * Call the handler function
        * This handler function is asynchronous
        *****************************************************************/
        service.handleRequest();

        /*****************************************************************
        * Send response back
        *****************************************************************/
        SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
        SoapHeader respheader = soapResponse.getSoapHeader(); 

        MsgHdrRs msgHdrRs = new MsgHdrRs();
        JAXBContext jaxbContext = JAXBContext.newInstance(MsgHdrRs.class);
        jaxbContext.createMarshaller().marshal(msgHdrRs, respheader.getResult());

        GetAcctInfoRs response = new GetAcctInfoRs();
        return response;
    }
}

As you can see above, my EndPoint class calls the DummyService method handleRequest.

The Dummy Service class is annonatated with @Service and method handRequest is annotated with @Async as can be seen below:

@Service
public class DummyService {

    private Logger logger = Logger.getLogger(DummyService.class);

    @Async("taskExecutorServiceImpl")
    public void handleRequest() {

        logger.info("DummyService: 1");

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        logger.info("DummyService: 2");
    }

}

I have also defined my configuration class as below:

@Configuration
@EnableAsync
public class ThreadConfig {

    @Bean(name = "taskExecutorServiceImpl")
    public ThreadPoolTaskExecutor specificTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.initialize();
        return executor;
    }
}

Kindly help my in resolving the issue. I am new to Spring framework and would appreciate any help in this.

Thanks


Solution

  • Problem is solved and unrelated to the above code. Problem was in configuration file and not related to the code above which is correct.