title: @Async - In Spring independent thread closing before completing task
body: <p>I have a requirement where I do some operation on GUI and once I save the data in DB I need to send a http request to a webservice. But the response to GUI should not wait for result from webservice request.
  For this I am using @Async , annotation of Spring.
  here is my structure</p>
  
  <pre><code>MyConroller.java 
  </code></pre>
  
  <p>calls a method </p>
  
  <pre><code>goSaveAndCreate
  </code></pre>
  
  <p>(not Async) in </p>
  
  <pre><code>ServiceA.java
  </code></pre>
  
  <p><strong>ServiceA</strong> has a <strong>ServiceB</strong> bean injected in it. A method , </p>
  
  <pre><code>@Async
  create() 
  </code></pre>
  
  <p>in <strong>ServiceB</strong> is annotated with Async.
  Now ServiceA.goSaveAndCreate calls a method in itself , save and calls ServiceB.create() (which is Async).
  I can see in logs the a new thread is created which is executing create method. But all of a sudden logs after a particular point stop and that thread seems to have got killed or comlpeted.</p>
  
  <pre><code> @Service("MarginCalculationService")
   public class ServiceA implements ServiceAI {
  private static final String APPROVED = "APPROVED";
  
  public static final String SUCCESS = "SUCCESS";
  ....
  @Autowired
  ServiceB serviceB;
  
  public List&lt;VV&gt; goSaveAndCreate(String[] ids,List&lt;XX&gt; calList) throws Exception, DataAccessException {
          try {
              Pair&lt;List&lt;VG&gt;,List&lt;UU&gt;&gt; resultList = save(ids);
              vvList  = resultList.getFirst();
              /*
               * HKAPIL - Send Message to webService callingserviceB
               */
              if(resultList.getSecond() != null){
                  serviceB.create(resultList.getSecond());
              }
          } catch (DataAccessException e) {
              e.printStackTrace();
              logger.error("Data Access Exception thrown during - " , e);
              throw e;
          } catch (Exception e){
              e.printStackTrace();
              logger.error("Exception thrown during " , e);
              throw e;
          }
          System.out.println("Exiting the method in MCSERVICE");
          return vvList;
  
  }
  
     private save(){
      ...
      ...
     }
  }
  </code></pre>
  
  <p>Second service</p>
  
  <pre><code> @Service("wireInstructionMessageService")
   public class ServiceB implements ServiceBI {
   @Async 
      @Override
      public void create(List&lt;Ralc&gt; calcList) {
          String threadName = Thread.currentThread().getName();
      logger.info("Inside a different thread [" + threadName + " ] to send message ." );
      ..
      ...
      otherMethod(Obj);
      }
  
      private otherMethod(Obj obj){
      ...
      ...
      ..
  
      //tills this point logs are getting printed  nothing after this
      ..
      ...
      }
  </code></pre>
  
  <p>}</p>
  
  <p>applciationContext.xml entry</p>
  
  <pre><code>&lt;!--  Defines a ThreadPoolTaskExecutor instance with configurable pool size, queue-capacity, keep-alive, 
        and rejection-policy values. The id becomes the default thread name prefix --&gt;
  &lt;task:executor id="MyMessageExecutor"
  pool-size="5-25"
  queue-capacity="100"/&gt;
  
  &lt;task:annotation-driven executor="MyMessageExecutor"/&gt;
  </code></pre>
  
  <p>Now I have two question</p>
  
  <p>1) is there a way I can add some logs in some method which tell taht the new thread from MyExecutor is getting killed or MyExecutor is getting closed (the way we have in normal Java ExecutorSerrvice)
  2) Am I using the Asyn in wrong way? Is it possible that as soon as method returns from Controller or ServiceA , ServiceB instance also is getting cleaned?</p>
  
  <p>Thanks
  HKapil</p>