0

I am using Spring 3.2.2.RELEASE version . The @Async anotation is not working as expected .

applicationContext.xml

<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy proxy-target-class="true"/>



    <context:component-scan base-package="com.pratik" />



    <tx:annotation-driven /> 

import.java

    public String async() {
    final String asssetImportId = ObjectId.get().toHexString();

      process.asyncTest();

    logger.info("Ongoing");
    return asssetImportId;
  }

process.java

@Async
  public void asyncTest() {
    try {
      Thread.sleep(1000);

    } catch (InterruptedException e) {

      e.printStackTrace();
    }
    logger.info("After Sleep");

  }

The After Sleep log should print last but it is getting printed before ongoing

Pratik Jaiswal
  • 390
  • 1
  • 5
  • 21

1 Answers1

0

Thread.sleep(000) seems a typo to me. in case apo allows you to do ste a 0 sleep, this would be a good reason for the print to happen before what you expected (maybe not always, since you are talking about Async any "order" of prints might happen)

Edit: tx:annotation-driven is for transactional support. Add context:annotation-config

Edit2: Correction: use "task:annotation-driven (not to confuse with tx namespace)

albert_nil
  • 1,648
  • 7
  • 9
  • I have changed code and made Thread.sleep(5000) . Still it works like single threaded model only . – Pratik Jaiswal Aug 06 '17 at 17:37
  • how are you retrieving "process" bean? you don't show that part of the code – albert_nil Aug 06 '17 at 17:40
  • Process bean is autowired . I have used spring @component anotation – Pratik Jaiswal Aug 06 '17 at 17:42
  • are you sure "tx:annotation-driven" is the lnly thing you need? that would seem related mainly to transactional stuff annotations, and async might be left aside. better add the context:annotation-config one also – albert_nil Aug 06 '17 at 17:52
  • can you post your edited xml config (including new namespace declarations)? also, just in case, your classes are under "com.patril" package isn't it? – albert_nil Aug 07 '17 at 07:06